@@ -4,6 +4,26 @@ import { loadConfigFromFile, loadEnv, normalizePath } from 'vite';
4
4
import { runtime_directory } from '../../core/utils.js' ;
5
5
import { posixify } from '../../utils/filesystem.js' ;
6
6
7
+ const illegal_imports = new Set ( [
8
+ '/@id/__x00__$env/dynamic/private' , //dev
9
+ '\0$env/dynamic/private' , // prod
10
+ '/@id/__x00__$env/static/private' , // dev
11
+ '\0$env/static/private' // prod
12
+ ] ) ;
13
+
14
+ /** @param {string } id */
15
+ function is_illegal ( id ) {
16
+ if ( illegal_imports . has ( id ) ) return true ;
17
+
18
+ // files outside the project root are ignored
19
+ if ( ! id . startsWith ( normalizePath ( process . cwd ( ) ) ) ) return false ;
20
+
21
+ // so are files inside node_modules
22
+ if ( id . startsWith ( normalizePath ( node_modules_dir ) ) ) return false ;
23
+
24
+ return / .* \. s e r v e r \. .+ / . test ( path . basename ( id ) ) ;
25
+ }
26
+
7
27
/**
8
28
* @param {import('vite').ResolvedConfig } config
9
29
* @param {import('vite').ConfigEnv } config_env
@@ -183,8 +203,9 @@ function repeat(str, times) {
183
203
/**
184
204
* Create a formatted error for an illegal import.
185
205
* @param {Array<{name: string, dynamic: boolean}> } stack
206
+ * @param {string } lib_dir
186
207
*/
187
- function format_illegal_import_chain ( stack ) {
208
+ function format_illegal_import_chain ( stack , lib_dir ) {
188
209
const dev_virtual_prefix = '/@id/__x00__' ;
189
210
const prod_virtual_prefix = '\0' ;
190
211
@@ -195,6 +216,9 @@ function format_illegal_import_chain(stack) {
195
216
if ( file . name . startsWith ( prod_virtual_prefix ) ) {
196
217
return { ...file , name : file . name . replace ( prod_virtual_prefix , '' ) } ;
197
218
}
219
+ if ( file . name . startsWith ( lib_dir ) ) {
220
+ return { ...file , name : file . name . replace ( lib_dir , '$lib' ) } ;
221
+ }
198
222
199
223
return { ...file , name : path . relative ( process . cwd ( ) , file . name ) } ;
200
224
} ) ;
@@ -211,6 +235,8 @@ function format_illegal_import_chain(stack) {
211
235
return `Cannot import ${ stack . at ( - 1 ) ?. name } into client-side code:\n${ pyramid } ` ;
212
236
}
213
237
238
+ const node_modules_dir = path . resolve ( process . cwd ( ) , 'node_modules' ) ;
239
+
214
240
/**
215
241
* Load environment variables from process.env and .env files
216
242
* @param {import('types').ValidatedKitConfig['env'] } env_config
@@ -228,11 +254,11 @@ export function get_env(env_config, mode) {
228
254
/**
229
255
* @param {(id: string) => import('rollup').ModuleInfo | null } node_getter
230
256
* @param {import('rollup').ModuleInfo } node
231
- * @param {Set< string> } illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
257
+ * @param {string } lib_dir
232
258
*/
233
- export function prevent_illegal_rollup_imports ( node_getter , node , illegal_imports ) {
234
- const chain = find_illegal_rollup_imports ( node_getter , node , false , illegal_imports ) ;
235
- if ( chain ) throw new Error ( format_illegal_import_chain ( chain ) ) ;
259
+ export function prevent_illegal_rollup_imports ( node_getter , node , lib_dir ) {
260
+ const chain = find_illegal_rollup_imports ( node_getter , node , false ) ;
261
+ if ( chain ) throw new Error ( format_illegal_import_chain ( chain , lib_dir ) ) ;
236
262
}
237
263
238
264
const query_pattern = / \? .* $ / s;
@@ -246,36 +272,27 @@ function remove_query_from_path(path) {
246
272
* @param {(id: string) => import('rollup').ModuleInfo | null } node_getter
247
273
* @param {import('rollup').ModuleInfo } node
248
274
* @param {boolean } dynamic
249
- * @param {Set<string> } illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
250
275
* @param {Set<string> } seen
251
276
* @returns {Array<import('types').ImportNode> | null }
252
277
*/
253
- const find_illegal_rollup_imports = (
254
- node_getter ,
255
- node ,
256
- dynamic ,
257
- illegal_imports ,
258
- seen = new Set ( )
259
- ) => {
278
+ const find_illegal_rollup_imports = ( node_getter , node , dynamic , seen = new Set ( ) ) => {
260
279
const name = remove_query_from_path ( normalizePath ( node . id ) ) ;
261
280
if ( seen . has ( name ) ) return null ;
262
281
seen . add ( name ) ;
263
282
264
- if ( illegal_imports . has ( name ) ) {
283
+ if ( is_illegal ( name ) ) {
265
284
return [ { name, dynamic } ] ;
266
285
}
267
286
268
287
for ( const id of node . importedIds ) {
269
288
const child = node_getter ( id ) ;
270
- const chain =
271
- child && find_illegal_rollup_imports ( node_getter , child , false , illegal_imports , seen ) ;
289
+ const chain = child && find_illegal_rollup_imports ( node_getter , child , false , seen ) ;
272
290
if ( chain ) return [ { name, dynamic } , ...chain ] ;
273
291
}
274
292
275
293
for ( const id of node . dynamicallyImportedIds ) {
276
294
const child = node_getter ( id ) ;
277
- const chain =
278
- child && find_illegal_rollup_imports ( node_getter , child , true , illegal_imports , seen ) ;
295
+ const chain = child && find_illegal_rollup_imports ( node_getter , child , true , seen ) ;
279
296
if ( chain ) return [ { name, dynamic } , ...chain ] ;
280
297
}
281
298
@@ -308,22 +325,21 @@ const get_module_types = (config_module_types) => {
308
325
/**
309
326
* Throw an error if a private module is imported from a client-side node.
310
327
* @param {import('vite').ModuleNode } node
311
- * @param {Set< string> } illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
328
+ * @param {string } lib_dir
312
329
* @param {Iterable<string> } module_types File extensions to analyze in addition to the defaults: `.ts`, `.js`, etc.
313
330
*/
314
- export function prevent_illegal_vite_imports ( node , illegal_imports , module_types ) {
315
- const chain = find_illegal_vite_imports ( node , illegal_imports , get_module_types ( module_types ) ) ;
316
- if ( chain ) throw new Error ( format_illegal_import_chain ( chain ) ) ;
331
+ export function prevent_illegal_vite_imports ( node , lib_dir , module_types ) {
332
+ const chain = find_illegal_vite_imports ( node , get_module_types ( module_types ) ) ;
333
+ if ( chain ) throw new Error ( format_illegal_import_chain ( chain , lib_dir ) ) ;
317
334
}
318
335
319
336
/**
320
337
* @param {import('vite').ModuleNode } node
321
- * @param {Set<string> } illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
322
338
* @param {Set<string> } module_types File extensions to analyze: `.ts`, `.js`, etc.
323
339
* @param {Set<string> } seen
324
340
* @returns {Array<import('types').ImportNode> | null }
325
341
*/
326
- function find_illegal_vite_imports ( node , illegal_imports , module_types , seen = new Set ( ) ) {
342
+ function find_illegal_vite_imports ( node , module_types , seen = new Set ( ) ) {
327
343
if ( ! node . id ) return null ; // TODO when does this happen?
328
344
const name = remove_query_from_path ( normalizePath ( node . id ) ) ;
329
345
@@ -332,12 +348,12 @@ function find_illegal_vite_imports(node, illegal_imports, module_types, seen = n
332
348
}
333
349
seen . add ( name ) ;
334
350
335
- if ( name && illegal_imports . has ( name ) ) {
351
+ if ( is_illegal ( name ) ) {
336
352
return [ { name, dynamic : false } ] ;
337
353
}
338
354
339
355
for ( const child of node . importedModules ) {
340
- const chain = child && find_illegal_vite_imports ( child , illegal_imports , module_types , seen ) ;
356
+ const chain = child && find_illegal_vite_imports ( child , module_types , seen ) ;
341
357
if ( chain ) return [ { name, dynamic : false } , ...chain ] ;
342
358
}
343
359
0 commit comments