vite-plugin-dynamic-import
Advanced tools
+1
-0
@@ -6,2 +6,3 @@ import type { Plugin } from 'vite'; | ||
| export { hasDynamicImport, normallyImporteeRE, mappingPath, toLooseGlob, type Resolved, Resolve, dynamicImportToGlob, globFiles, }; | ||
| export declare const TAG = "[vite-plugin-dynamic-import]"; | ||
| export interface Options { | ||
@@ -8,0 +9,0 @@ filter?: (id: string) => boolean | void; |
+35
-18
@@ -60,10 +60,10 @@ "use strict"; | ||
| } | ||
| function relativeify(relative) { | ||
| if (relative === "") { | ||
| function relativeify(relativePath) { | ||
| if (relativePath === "") { | ||
| return "."; | ||
| } | ||
| if (!(relative.startsWith("./") || relative.startsWith(".\\"))) { | ||
| return "./" + relative; | ||
| if (!/^\.{1,2}[/\\]/.test(relativePath)) { | ||
| return "./" + relativePath; | ||
| } | ||
| return relative; | ||
| return relativePath; | ||
| } | ||
@@ -77,2 +77,10 @@ const isWindows = os.platform() === "win32"; | ||
| } | ||
| const COLOURS = { | ||
| $: (c) => (str) => `\x1B[${c}m` + str + "\x1B[0m", | ||
| gary: (str) => COLOURS.$(90)(str), | ||
| cyan: (str) => COLOURS.$(36)(str), | ||
| yellow: (str) => COLOURS.$(33)(str), | ||
| green: (str) => COLOURS.$(32)(str), | ||
| red: (str) => COLOURS.$(31)(str) | ||
| }; | ||
| const VOLUME_RE = /^[A-Z]:/i; | ||
@@ -334,2 +342,3 @@ function node_modules(root, paths = []) { | ||
| } | ||
| const TAG = "[vite-plugin-dynamic-import]"; | ||
| function dynamicImport(options = {}) { | ||
@@ -457,22 +466,29 @@ let config; | ||
| let { files, resolved, normally } = globResult; | ||
| files = files.filter((f) => path.posix.join(path.dirname(id), f) !== id); | ||
| options.onFiles && (files = options.onFiles(files, id) || files); | ||
| if (normally) { | ||
| ms.overwrite(expStart, expEnd, `import('${normally}')`); | ||
| } else { | ||
| if (!(files == null ? void 0 : files.length)) | ||
| continue; | ||
| const mapAlias = resolved ? { [resolved.alias.relative]: resolved.alias.findString } : void 0; | ||
| const maps = mappingPath(files, mapAlias); | ||
| const runtimeName = `__variableDynamicImportRuntime${dynamicImportIndex++}__`; | ||
| const runtimeFn = generateDynamicImportRuntime(maps, runtimeName); | ||
| ms.overwrite(expStart, expEnd, `${runtimeName}(${rawImportee})`); | ||
| runtimeFunctions.push(runtimeFn); | ||
| continue; | ||
| } | ||
| if (!(files == null ? void 0 : files.length)) { | ||
| console.log( | ||
| TAG, | ||
| COLOURS.yellow(`no files matched: ${importExpression} | ||
| `), | ||
| ` file: ${id}` | ||
| ); | ||
| continue; | ||
| } | ||
| files = files.filter((f) => path.posix.join(path.dirname(id), f) !== id); | ||
| options.onFiles && (files = options.onFiles(files, id) || files); | ||
| const mapAlias = resolved ? { [resolved.alias.relative]: resolved.alias.findString } : void 0; | ||
| const maps = mappingPath(files, mapAlias); | ||
| const runtimeName = `__variableDynamicImportRuntime${dynamicImportIndex++}__`; | ||
| const runtimeFn = generateDynamicImportRuntime(maps, runtimeName); | ||
| ms.overwrite(expStart, expEnd, `${runtimeName}(${rawImportee})`); | ||
| runtimeFunctions.push(runtimeFn); | ||
| } | ||
| if (runtimeFunctions.length) { | ||
| ms.append([ | ||
| "// [vite-plugin-dynamic-import] runtime -S-", | ||
| `// ${TAG} runtime -S-`, | ||
| ...runtimeFunctions, | ||
| "// [vite-plugin-dynamic-import] runtime -E-" | ||
| `// ${TAG} runtime -E-` | ||
| ].join("\n")); | ||
@@ -553,2 +569,3 @@ } | ||
| exports.Resolve = Resolve; | ||
| exports.TAG = TAG; | ||
| exports.default = dynamicImport; | ||
@@ -555,0 +572,0 @@ exports.dynamicImportToGlob = dynamicImportToGlob; |
+35
-18
@@ -58,10 +58,10 @@ import fs from "node:fs"; | ||
| } | ||
| function relativeify(relative) { | ||
| if (relative === "") { | ||
| function relativeify(relativePath) { | ||
| if (relativePath === "") { | ||
| return "."; | ||
| } | ||
| if (!(relative.startsWith("./") || relative.startsWith(".\\"))) { | ||
| return "./" + relative; | ||
| if (!/^\.{1,2}[/\\]/.test(relativePath)) { | ||
| return "./" + relativePath; | ||
| } | ||
| return relative; | ||
| return relativePath; | ||
| } | ||
@@ -75,2 +75,10 @@ const isWindows = os.platform() === "win32"; | ||
| } | ||
| const COLOURS = { | ||
| $: (c) => (str) => `\x1B[${c}m` + str + "\x1B[0m", | ||
| gary: (str) => COLOURS.$(90)(str), | ||
| cyan: (str) => COLOURS.$(36)(str), | ||
| yellow: (str) => COLOURS.$(33)(str), | ||
| green: (str) => COLOURS.$(32)(str), | ||
| red: (str) => COLOURS.$(31)(str) | ||
| }; | ||
| const VOLUME_RE = /^[A-Z]:/i; | ||
@@ -332,2 +340,3 @@ function node_modules(root, paths = []) { | ||
| } | ||
| const TAG = "[vite-plugin-dynamic-import]"; | ||
| function dynamicImport(options = {}) { | ||
@@ -455,22 +464,29 @@ let config; | ||
| let { files, resolved, normally } = globResult; | ||
| files = files.filter((f) => path.posix.join(path.dirname(id), f) !== id); | ||
| options.onFiles && (files = options.onFiles(files, id) || files); | ||
| if (normally) { | ||
| ms.overwrite(expStart, expEnd, `import('${normally}')`); | ||
| } else { | ||
| if (!(files == null ? void 0 : files.length)) | ||
| continue; | ||
| const mapAlias = resolved ? { [resolved.alias.relative]: resolved.alias.findString } : void 0; | ||
| const maps = mappingPath(files, mapAlias); | ||
| const runtimeName = `__variableDynamicImportRuntime${dynamicImportIndex++}__`; | ||
| const runtimeFn = generateDynamicImportRuntime(maps, runtimeName); | ||
| ms.overwrite(expStart, expEnd, `${runtimeName}(${rawImportee})`); | ||
| runtimeFunctions.push(runtimeFn); | ||
| continue; | ||
| } | ||
| if (!(files == null ? void 0 : files.length)) { | ||
| console.log( | ||
| TAG, | ||
| COLOURS.yellow(`no files matched: ${importExpression} | ||
| `), | ||
| ` file: ${id}` | ||
| ); | ||
| continue; | ||
| } | ||
| files = files.filter((f) => path.posix.join(path.dirname(id), f) !== id); | ||
| options.onFiles && (files = options.onFiles(files, id) || files); | ||
| const mapAlias = resolved ? { [resolved.alias.relative]: resolved.alias.findString } : void 0; | ||
| const maps = mappingPath(files, mapAlias); | ||
| const runtimeName = `__variableDynamicImportRuntime${dynamicImportIndex++}__`; | ||
| const runtimeFn = generateDynamicImportRuntime(maps, runtimeName); | ||
| ms.overwrite(expStart, expEnd, `${runtimeName}(${rawImportee})`); | ||
| runtimeFunctions.push(runtimeFn); | ||
| } | ||
| if (runtimeFunctions.length) { | ||
| ms.append([ | ||
| "// [vite-plugin-dynamic-import] runtime -S-", | ||
| `// ${TAG} runtime -S-`, | ||
| ...runtimeFunctions, | ||
| "// [vite-plugin-dynamic-import] runtime -E-" | ||
| `// ${TAG} runtime -E-` | ||
| ].join("\n")); | ||
@@ -552,2 +568,3 @@ } | ||
| Resolve, | ||
| TAG, | ||
| dynamicImport as default, | ||
@@ -554,0 +571,0 @@ dynamicImportToGlob, |
+3
-3
| { | ||
| "name": "vite-plugin-dynamic-import", | ||
| "version": "1.3.1", | ||
| "version": "1.3.2", | ||
| "description": "Enhance Vite builtin dynamic import", | ||
@@ -26,3 +26,3 @@ "main": "./dist/index.js", | ||
| "types": "tsc", | ||
| "prepublishOnly": "npm run build" | ||
| "prepublishOnly": "npm run test && npm run build" | ||
| }, | ||
@@ -40,3 +40,3 @@ "dependencies": { | ||
| "vite": "^4.3.2", | ||
| "vite-plugin-utils": "^0.4.2", | ||
| "vite-plugin-utils": "^0.4.3", | ||
| "vitest": "^0.30.1" | ||
@@ -43,0 +43,0 @@ }, |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
49434
1.77%1288
2.79%