9
9
isFunctionIsland ,
10
10
readSourceFile ,
11
11
} from '@dumbjs/preland'
12
+ import Unimport from 'unimport/unplugin'
12
13
import { addImportToAST , codeFromAST } from '@dumbjs/preland/ast'
13
14
import preact from '@preact/preset-vite'
14
15
import { mkdirSync , readFileSync , writeFileSync } from 'fs'
@@ -37,6 +38,7 @@ export function adex({
37
38
adapter : adapter = 'node' ,
38
39
} = { } ) {
39
40
return [
41
+ adexConfig ( ) ,
40
42
preactPages ( {
41
43
root : '/src/pages' ,
42
44
id : '~routes' ,
@@ -61,15 +63,15 @@ export function adex({
61
63
import { dirname, join } from 'node:path'
62
64
import { fileURLToPath } from 'node:url'
63
65
import { existsSync, readFileSync } from 'node:fs'
64
- import { env } from ' adex/env'
66
+ import {env} from " adex/env"
65
67
66
68
import 'virtual:adex:font.css'
67
69
import 'virtual:adex:global.css'
68
70
69
71
const __dirname = dirname(fileURLToPath(import.meta.url))
70
72
71
- const PORT = parseInt(env.get(' PORT', ' 3000'), 10 )
72
- const HOST = env.get(' HOST', ' localhost')
73
+ const PORT = parseInt(env.get(" PORT"," 3000") )
74
+ const HOST = env.get(" HOST"," localhost")
73
75
74
76
const paths = {
75
77
assets: join(__dirname, './assets'),
@@ -128,7 +130,6 @@ export function adex({
128
130
adexServerBuilder ( { islands } ) ,
129
131
! islands && adexClientBuilder ( ) ,
130
132
islands && adexIslandsBuilder ( ) ,
131
- ...adexGuards ( ) ,
132
133
]
133
134
}
134
135
@@ -212,6 +213,10 @@ function adexIslandsBuilder() {
212
213
await build ( {
213
214
configFile : false ,
214
215
plugins : [ preact ( ) ] ,
216
+ define : {
217
+ 'import.meta.server' : false ,
218
+ 'import.meta.client' : true ,
219
+ } ,
215
220
build : {
216
221
ssr : false ,
217
222
outDir : join ( outDir , 'islands' ) ,
@@ -346,6 +351,10 @@ function adexClientBuilder() {
346
351
) ,
347
352
preact ( { prefreshEnabled : false } ) ,
348
353
] ,
354
+ define : {
355
+ 'import.meta.client' : true ,
356
+ 'import.meta.server' : false ,
357
+ } ,
349
358
build : {
350
359
outDir : 'dist/client' ,
351
360
emptyOutDir : true ,
@@ -388,8 +397,12 @@ function adexServerBuilder({ islands = false } = {}) {
388
397
return {
389
398
appType : 'custom' ,
390
399
ssr : {
391
- external : [ 'preact' , 'adex' , 'preact-render-to-string' ] ,
392
- noExternal : Object . values ( adapterMap ) ,
400
+ external : [ 'preact' , 'preact-render-to-string' ] ,
401
+ noExternal : Object . values ( adapterMap ) . concat ( 'adex/env' ) ,
402
+ } ,
403
+ define : {
404
+ 'import.meta.server' : true ,
405
+ 'import.meta.client' : false ,
393
406
} ,
394
407
build : {
395
408
outDir : 'dist/server' ,
@@ -479,45 +492,32 @@ function adexServerBuilder({ islands = false } = {}) {
479
492
/**
480
493
* @returns {import("vite").Plugin[] }
481
494
*/
482
- function adexGuards ( ) {
495
+ function adexConfig ( ) {
483
496
return [
497
+ // @ts -expect-error something wrong wrong
498
+ Unimport . vite ( {
499
+ imports : [
500
+ {
501
+ name : '$fetch' ,
502
+ from : fileURLToPath ( new URL ( '../runtime/fetch.js' , import . meta. url ) ) ,
503
+ } ,
504
+ ] ,
505
+ } ) ,
484
506
{
485
- name : 'adex-guard-env ' ,
507
+ name : 'adex-config ' ,
486
508
enforce : 'pre' ,
487
- async transform ( code , id ) {
488
- // ignore usage of `process.env` in node_modules
489
- // Still risky but hard to do anything about
490
- const nodeMods = resolve ( cwd , 'node_modules' )
491
- if ( id . startsWith ( nodeMods ) ) return
492
-
493
- // ignore usage of `process.env` in `adex/env`
494
- const envLoadId = await this . resolve ( 'adex/env' )
495
- if ( id === envLoadId . id ) return
496
-
497
- if ( code . includes ( 'process.env' ) ) {
498
- this . error (
499
- 'Avoid using `process.env` to access environment variables and secrets. Use `adex/env` instead'
500
- )
501
- }
502
- } ,
503
- writeBundle ( ) {
504
- const pagesPath = resolve ( cwd , 'src/pages' )
505
- const info = this . getModuleInfo ( 'adex/env' )
506
- const viteRef = this
507
-
508
- function checkTree ( importPath , importStack = [ ] ) {
509
- if ( importPath . startsWith ( pagesPath ) ) {
510
- throw new Error (
511
- `Cannot use/import \`adex/env\` on the client side, importerStack: ${ importStack . join ( ' -> ' ) } `
512
- )
513
- }
514
- viteRef
515
- . getModuleInfo ( importPath )
516
- . importers . forEach ( d =>
517
- checkTree ( d , [ ...importStack , importPath , d ] )
518
- )
509
+ config ( ) {
510
+ return {
511
+ server : {
512
+ port : 3000 ,
513
+ } ,
514
+ define : {
515
+ 'import.meta.env.PORT' : JSON . stringify ( process . env . PORT ?? '3000' ) ,
516
+ 'import.meta.env.HOST' : JSON . stringify (
517
+ process . env . HOST ?? 'localhost'
518
+ ) ,
519
+ } ,
519
520
}
520
- info . importers . forEach ( i => checkTree ( i ) )
521
521
} ,
522
522
} ,
523
523
]
0 commit comments