Skip to content

Commit b09fb64

Browse files
authored
Merge pull request #51 from vite-plugin/v1.3.2
V1.3.2
2 parents d37ad08 + bf3ae53 commit b09fb64

3 files changed

Lines changed: 49 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [2023-04-30] v1.3.2
2+
3+
- 1b96e39 v1.3.2
4+
- 6f0fadf feat: `glob` files log
5+
16
## [2023-04-30] v1.3.1
27

38
- 9c81c19 docs: v1.3.1

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-dynamic-import",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Enhance Vite builtin dynamic import",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -23,7 +23,7 @@
2323
"build": "vite build",
2424
"test": "vitest run",
2525
"types": "tsc",
26-
"prepublishOnly": "npm run build"
26+
"prepublishOnly": "npm run test && npm run build"
2727
},
2828
"dependencies": {
2929
"acorn": "^8.8.2",
@@ -36,7 +36,7 @@
3636
"node-fetch": "^3.3.0",
3737
"typescript": "^4.9.4",
3838
"vite": "^4.3.2",
39-
"vite-plugin-utils": "^0.4.2",
39+
"vite-plugin-utils": "^0.4.3",
4040
"vitest": "^0.30.1"
4141
},
4242
"keywords": [

src/index.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { parse as parseAst } from 'acorn'
1010
import fastGlob from 'fast-glob'
1111
import { DEFAULT_EXTENSIONS } from 'vite-plugin-utils/constant'
1212
import {
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+
4346
export 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

Comments
 (0)