vite-plugin-dynamic-import
Advanced tools
Comparing version 0.7.1 to 0.8.0
@@ -8,2 +8,3 @@ "use strict"; | ||
const path_1 = __importDefault(require("path")); | ||
const vite_1 = require("vite"); | ||
const utils_1 = require("./utils"); | ||
@@ -48,2 +49,3 @@ class AliasContext { | ||
if (_find) { | ||
// 🚨 The path processed with `normalizePath` is required | ||
if (path_1.default.isAbsolute(replacement)) { | ||
@@ -93,6 +95,15 @@ // Compatible with vite restrictions | ||
const { find, replacement } = alias; | ||
if (path_1.default.isAbsolute(replacement)) { | ||
if (replacement.startsWith('.')) { | ||
// Considered a relative path | ||
ipte = ipte.replace(find, replacement); | ||
} | ||
else { | ||
const normalId = (0, vite_1.normalizePath)(id); | ||
const normalReplacement = (0, vite_1.normalizePath)(replacement); | ||
// Compatible with vite restrictions | ||
// https://github.com/vitejs/vite/blob/1e9615d8614458947a81e0d4753fe61f3a277cb3/packages/vite/src/node/plugins/importAnalysis.ts#L672 | ||
let relativePath = path_1.default.posix.relative(/* 🚧 */ path_1.default.dirname(id), replacement); | ||
let relativePath = path_1.default.relative( | ||
// Usually, the `replacement` we use is the directory path | ||
// So we also use the `path.dirname` path for calculation | ||
path_1.default.dirname(/* 🚧-① */ normalId), normalReplacement); | ||
if (relativePath === '') { | ||
@@ -107,5 +118,2 @@ relativePath = '.'; | ||
} | ||
else { | ||
ipte = ipte.replace(find, replacement); | ||
} | ||
return { | ||
@@ -112,0 +120,0 @@ alias, |
@@ -23,3 +23,3 @@ import type { AliasContext, AliasReplaced } from './alias'; | ||
*/ | ||
export declare function tryFixGlobSlash(glob: string, deep?: boolean): string | void; | ||
export declare function tryFixGlobSlash(glob: string, depth?: boolean): string | void; | ||
/** | ||
@@ -38,1 +38,2 @@ * ``` | ||
} | void; | ||
export declare function toDepthGlob(glob: string): string; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.tryFixGlobExtension = exports.tryFixGlobSlash = exports.DynamicImportVars = void 0; | ||
exports.toDepthGlob = exports.tryFixGlobExtension = exports.tryFixGlobSlash = exports.DynamicImportVars = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
@@ -120,3 +120,3 @@ class DynamicImportVars { | ||
*/ | ||
function tryFixGlobSlash(glob, deep = true) { | ||
function tryFixGlobSlash(glob, depth = true) { | ||
const extname = path_1.default.extname(glob); | ||
@@ -130,7 +130,3 @@ // It could be `./views*.js`, which needs to be repaired to `./views/*.js` | ||
let fixedGlob = glob.replace(importPath, importPath + '/'); | ||
fixedGlob = deep | ||
// match as far as possible | ||
// `./views/*` -> `./views/**/*` | ||
? fixedGlob.replace(/^(.*)\/\*$/, '$1/**/*') | ||
: fixedGlob; | ||
fixedGlob = depth ? toDepthGlob(fixedGlob) : fixedGlob; | ||
// if it has a '.js' extension | ||
@@ -165,1 +161,10 @@ // `./views/*` -> `./views/*.js` | ||
exports.tryFixGlobExtension = tryFixGlobExtension; | ||
// Match as far as possible | ||
// `./views/*` -> `./views/**/*` | ||
function toDepthGlob(glob) { | ||
const extname = path_1.default.extname(glob); | ||
return glob | ||
.replace(extname, '') | ||
.replace(/^(.*)(?<!\*\*)\/\*$/, '$1/**/*') + extname; | ||
} | ||
exports.toDepthGlob = toDepthGlob; |
@@ -60,3 +60,3 @@ "use strict"; | ||
return; | ||
const globResult = await globFiles(dynamicImport, node, code, pureId, globExtensions); | ||
const globResult = await globFiles(dynamicImport, node, code, pureId, globExtensions, options.depth !== false); | ||
if (!globResult) | ||
@@ -137,3 +137,3 @@ return; | ||
exports.default = dynamicImport; | ||
async function globFiles(dynamicImport, ImportExpressionNode, sourceString, pureId, extensions) { | ||
async function globFiles(dynamicImport, ImportExpressionNode, sourceString, pureId, extensions, depth) { | ||
const node = ImportExpressionNode; | ||
@@ -152,2 +152,3 @@ const code = sourceString; | ||
glob = (0, dynamic_import_vars_1.tryFixGlobSlash)(glob) || glob; | ||
depth && (glob = (0, dynamic_import_vars_1.toDepthGlob)(glob)); | ||
const tmp = (0, dynamic_import_vars_1.tryFixGlobExtension)(glob, extensions); | ||
@@ -158,3 +159,3 @@ if (tmp) { | ||
} | ||
const files = fast_glob_1.default.sync(globWithIndex ? [glob, globWithIndex] : glob, { cwd: /* 🚧 */ path_1.default.dirname(pureId) }); | ||
const files = fast_glob_1.default.sync(globWithIndex ? [glob, globWithIndex] : glob, { cwd: path_1.default.dirname(/* 🚧-① */ pureId) }); | ||
let aliasWithFiles; | ||
@@ -161,0 +162,0 @@ if (alias) { |
@@ -6,2 +6,7 @@ import type { AcornNode as AcornNode2 } from 'rollup'; | ||
filter?: (...args: Parameters<Plugin['transform']>) => false | void | Promise<false | void>; | ||
/** | ||
* This option will change `./*` to `./** /*` | ||
* @default true | ||
*/ | ||
depth?: boolean; | ||
} |
{ | ||
"name": "vite-plugin-dynamic-import", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "Enhance Vite builtin dynamic import", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -39,2 +39,7 @@ # vite-plugin-dynamic-import [![NPM version](https://img.shields.io/npm/v/vite-plugin-dynamic-import.svg)](https://npmjs.org/package/vite-plugin-dynamic-import) [![awesome-vite](https://awesome.re/badge.svg)](https://github.com/vitejs/awesome-vite) | ||
filter?: (...args: Parameters<Plugin['transform']>) => false | void | Promise<false | void> | ||
/** | ||
* This option will change `./*` to `./** /*` | ||
* @default true | ||
*/ | ||
depth?: boolean | ||
} | ||
@@ -41,0 +46,0 @@ ``` |
@@ -39,3 +39,9 @@ # vite-plugin-dynamic-import [![NPM version](https://img.shields.io/npm/v/vite-plugin-dynamic-import.svg)](https://npmjs.org/package/vite-plugin-dynamic-import) [![awesome-vite](https://awesome.re/badge.svg)](https://github.com/vitejs/awesome-vite) | ||
filter?: (...args: Parameters<Plugin['transform']>) => false | void | Promise<false | void> | ||
/** | ||
* 这个选项将会把 `./*` 变成 `./** /*` | ||
* @default true | ||
*/ | ||
depth?: boolean | ||
} | ||
``` | ||
@@ -42,0 +48,0 @@ |
import path from 'path' | ||
import type { Alias, ResolvedConfig } from 'vite' | ||
import { normalizePath } from 'vite' | ||
import { importeeRawRegex } from './utils' | ||
@@ -57,2 +58,3 @@ | ||
if (_find) { | ||
// 🚨 The path processed with `normalizePath` is required | ||
if (path.isAbsolute(replacement)) { | ||
@@ -106,6 +108,17 @@ // Compatible with vite restrictions | ||
if (path.isAbsolute(replacement)) { | ||
if (replacement.startsWith('.')) { | ||
// Considered a relative path | ||
ipte = ipte.replace(find, replacement) | ||
} else { | ||
const normalId = normalizePath(id) | ||
const normalReplacement = normalizePath(replacement) | ||
// Compatible with vite restrictions | ||
// https://github.com/vitejs/vite/blob/1e9615d8614458947a81e0d4753fe61f3a277cb3/packages/vite/src/node/plugins/importAnalysis.ts#L672 | ||
let relativePath = path.posix.relative(/* 🚧 */path.dirname(id), replacement) | ||
let relativePath = path.relative( | ||
// Usually, the `replacement` we use is the directory path | ||
// So we also use the `path.dirname` path for calculation | ||
path.dirname(/* 🚧-① */normalId), | ||
normalReplacement, | ||
) | ||
if (relativePath === '') { | ||
@@ -119,4 +132,2 @@ relativePath = '.' | ||
ipte = relativeImportee | ||
} else { | ||
ipte = ipte.replace(find, replacement) | ||
} | ||
@@ -123,0 +134,0 @@ |
@@ -158,3 +158,3 @@ import path from 'path' | ||
*/ | ||
export function tryFixGlobSlash(glob: string, deep = true): string | void { | ||
export function tryFixGlobSlash(glob: string, depth = true): string | void { | ||
const extname = path.extname(glob) | ||
@@ -170,7 +170,3 @@ // It could be `./views*.js`, which needs to be repaired to `./views/*.js` | ||
fixedGlob = deep | ||
// match as far as possible | ||
// `./views/*` -> `./views/**/*` | ||
? fixedGlob.replace(/^(.*)\/\*$/, '$1/**/*') | ||
: fixedGlob | ||
fixedGlob = depth ? toDepthGlob(fixedGlob) : fixedGlob | ||
@@ -206,1 +202,11 @@ // if it has a '.js' extension | ||
} | ||
// Match as far as possible | ||
// `./views/*` -> `./views/**/*` | ||
export function toDepthGlob(glob: string): string { | ||
const extname = path.extname(glob) | ||
return glob | ||
.replace(extname, '') | ||
.replace(/^(.*)(?<!\*\*)\/\*$/, '$1/**/*') + extname | ||
} |
@@ -20,3 +20,8 @@ import path from 'path' | ||
import { AliasContext, AliasReplaced } from './alias' | ||
import { DynamicImportVars, tryFixGlobExtension, tryFixGlobSlash } from './dynamic-import-vars' | ||
import { | ||
DynamicImportVars, | ||
tryFixGlobExtension, | ||
tryFixGlobSlash, | ||
toDepthGlob, | ||
} from './dynamic-import-vars' | ||
import { DynamicImportRuntime, generateDynamicImportRuntime } from './dynamic-import-helper' | ||
@@ -78,2 +83,3 @@ | ||
globExtensions, | ||
options.depth !== false, | ||
) | ||
@@ -196,2 +202,3 @@ if (!globResult) return | ||
extensions: string[], | ||
depth: boolean, | ||
): Promise<GlobFilesResult> { | ||
@@ -217,2 +224,3 @@ const node = ImportExpressionNode | ||
glob = tryFixGlobSlash(glob) || glob | ||
depth && (glob = toDepthGlob(glob)) | ||
const tmp = tryFixGlobExtension(glob, extensions) | ||
@@ -226,3 +234,3 @@ if (tmp) { | ||
globWithIndex ? [glob, globWithIndex] : glob, | ||
{ cwd: /* 🚧 */path.dirname(pureId) }, | ||
{ cwd: path.dirname(/* 🚧-① */pureId) }, | ||
) | ||
@@ -229,0 +237,0 @@ |
@@ -8,2 +8,7 @@ import type { AcornNode as AcornNode2 } from 'rollup' | ||
filter?: (...args: Parameters<Plugin['transform']>) => false | void | Promise<false | void> | ||
/** | ||
* This option will change `./*` to `./** /*` | ||
* @default true | ||
*/ | ||
depth?: boolean | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55027
1262
136