@@ -10,6 +10,7 @@ import { parse as parseAst } from 'acorn'
1010import fastGlob from 'fast-glob'
1111import { DEFAULT_EXTENSIONS } from 'vite-plugin-utils/constant'
1212import {
13+ COLOURS ,
1314 MagicString ,
1415 cleanUrl ,
1516 relativeify ,
@@ -40,6 +41,8 @@ export {
4041 globFiles ,
4142}
4243
44+ export const TAG = '[vite-plugin-dynamic-import]'
45+
4346export interface Options {
4447 filter ?: ( id : string ) => boolean | void
4548 /**
@@ -216,43 +219,53 @@ async function transformDynamicImport({
216219 if ( ! globResult ) continue
217220
218221 let { files, resolved, normally } = globResult
219- // skip itself
220- files = files ! . filter ( f => path . posix . join ( path . dirname ( id ) , f ) !== id )
221- // execute the Options.onFiles
222- options . onFiles && ( files = options . onFiles ( files , id ) || files )
223222
224223 if ( normally ) {
225- // normally importee (🚧-③ After `expressiontoglob()` processing)
224+ // normally importee (🚧-③ After `import('./dynamic-import-to-glob'). expressiontoglob()` processing)
226225 ms . overwrite ( expStart , expEnd , `import('${ normally } ')` )
227- } else {
228- if ( ! files ?. length ) continue
229- const mapAlias = resolved
230- ? { [ resolved . alias . relative ] : resolved . alias . findString }
231- : undefined
232-
233- const maps = mappingPath ( files , mapAlias )
234- const runtimeName = `__variableDynamicImportRuntime${ dynamicImportIndex ++ } __`
235- const runtimeFn = generateDynamicImportRuntime ( maps , runtimeName )
236-
237- // extension should be removed, because if the "index" file is in the directory, an error will occur
238- //
239- // e.g.
240- // ├─┬ views
241- // │ ├─┬ foo
242- // │ │ └── index.js
243- // │ └── bar.js
244- //
245- // the './views/*.js' should be matched ['./views/foo/index.js', './views/bar.js'], this may not be rigorous
246- ms . overwrite ( expStart , expEnd , `${ runtimeName } (${ rawImportee } )` )
247- runtimeFunctions . push ( runtimeFn )
226+ continue
227+ }
228+
229+ if ( ! files ?. length ) {
230+ console . log (
231+ TAG ,
232+ COLOURS . yellow ( `no files matched: ${ importExpression } \n` ) ,
233+ ` file: ${ id } ` ,
234+ )
235+ continue
248236 }
237+
238+ // skip itself
239+ files = files . filter ( f => path . posix . join ( path . dirname ( id ) , f ) !== id )
240+ // execute the Options.onFiles
241+ options . onFiles && ( files = options . onFiles ( files , id ) || files )
242+
243+ const mapAlias = resolved
244+ ? { [ resolved . alias . relative ] : resolved . alias . findString }
245+ : undefined
246+
247+ const maps = mappingPath ( files , mapAlias )
248+ const runtimeName = `__variableDynamicImportRuntime${ dynamicImportIndex ++ } __`
249+ const runtimeFn = generateDynamicImportRuntime ( maps , runtimeName )
250+
251+ // extension should be removed, because if the "index" file is in the directory, an error will occur
252+ //
253+ // e.g.
254+ // ├─┬ views
255+ // │ ├─┬ foo
256+ // │ │ └── index.js
257+ // │ └── bar.js
258+ //
259+ // the './views/*.js' should be matched ['./views/foo/index.js', './views/bar.js'], this may not be rigorous
260+ ms . overwrite ( expStart , expEnd , `${ runtimeName } (${ rawImportee } )` )
261+ runtimeFunctions . push ( runtimeFn )
249262 }
250263
251264 if ( runtimeFunctions . length ) {
252265 ms . append ( [
253- ' // [vite-plugin-dynamic-import] runtime -S-' ,
266+ ` // ${ TAG } runtime -S-` ,
254267 ...runtimeFunctions ,
255- ' // [vite-plugin-dynamic-import] runtime -E-' ,
268+ ` // ${ TAG } runtime -E-` ,
256269 ] . join ( '\n' ) )
257270 }
258271
0 commit comments