diff --git a/examples/vue/vite.config.ts b/examples/vue/vite.config.ts index 0119d56..548ea40 100644 --- a/examples/vue/vite.config.ts +++ b/examples/vue/vite.config.ts @@ -15,7 +15,7 @@ export default defineConfig({ cdn: { modules: ['vue', 'ant-design-vue', 'pinia', 'vue-router'], local: { - modules: ['vue'], + modules: true, copy: true, }, }, diff --git a/package.json b/package.json index 4bd082b..8ac7134 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "@tomjs/tsconfig": "^1.2.1", "@types/fs-extra": "^11.0.4", "@types/html-minifier-terser": "^7.0.2", + "@types/lodash": "^4.14.202", "@types/node": "^18.19.3", "chokidar": "^3.5.3", "cross-env": "^7.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5501b8..215af4c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -51,6 +51,9 @@ importers: '@types/html-minifier-terser': specifier: ^7.0.2 version: 7.0.2 + '@types/lodash': + specifier: ^4.14.202 + version: 4.14.202 '@types/node': specifier: ^18.19.3 version: 18.19.3 @@ -1580,6 +1583,10 @@ packages: '@types/node': 18.19.3 dev: true + /@types/lodash@4.14.202: + resolution: {integrity: sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==} + dev: true + /@types/minimist@1.2.2: resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==} dev: true diff --git a/src/cdn/util.ts b/src/cdn/util.ts index dae7cc6..a340bea 100644 --- a/src/cdn/util.ts +++ b/src/cdn/util.ts @@ -73,14 +73,28 @@ function getProjectPkgDeps() { return Object.keys(pkg.dependencies || {}); } -type PreHandleOptions = Omit & { local: HtmlCdnLocal }; +interface PreHandleOptions extends Omit { + local: HtmlCdnLocal; +} // Preprocess configuration options function preHandleOptions(options?: HtmlCdnOptions): PreHandleOptions { - const opts: PreHandleOptions = Object.assign( - { type: 'unpkg', local: false } as HtmlCdnOptions, - _.cloneDeep(Object.assign({}, options)), - ); + const { local, ...restOpts } = _.cloneDeep(options) || {}; + const opts = restOpts as PreHandleOptions; + + // const { local } = opts; + const localOpts: HtmlCdnLocal = { + modules: [], + copy: true, + }; + if (typeof local === 'boolean' || Array.isArray(local)) { + Object.assign(localOpts, { modules: local }); + } else { + Object.assign(localOpts, local); + } + localOpts.modules = localOpts.modules ?? false; + localOpts.path = localOpts.path || 'npm/{name}@{version}'; + opts.local = localOpts; // URL type let { type, url } = opts; @@ -97,21 +111,6 @@ function preHandleOptions(options?: HtmlCdnOptions): PreHandleOptions { opts.type = type; opts.url = url; - const { local } = opts; - const localOpts: HtmlCdnLocal = { - modules: [], - copy: true, - }; - - if (typeof local === 'boolean' || Array.isArray(local)) { - Object.assign(localOpts, { modules: local }); - } else { - Object.assign(localOpts, local); - } - localOpts.modules = localOpts.modules ?? false; - localOpts.path = localOpts.path || 'npm/{name}@{version}'; - opts.local = localOpts; - return opts; } @@ -183,7 +182,7 @@ export function getModuleConfig(options: HtmlCdnOptions, userConfig: UserConfig) // Local CDN if (typeof npm.local !== 'boolean') { - const localModules = opts.local.modules; + const localModules = opts.local?.modules; if (typeof localModules === 'boolean') { npm.local = localModules; } else if (Array.isArray(localModules) && localModules.length > 0) { @@ -220,7 +219,7 @@ export function getModuleConfig(options: HtmlCdnOptions, userConfig: UserConfig) const npm = PRESET_MODULES[npmName]; if (npm) { npm.name = npm.name || npmName; - mergeModuleConfig(_.cloneDeep(npm)); + mergeModuleConfig(_.cloneDeep(npm) as NpmModule); } else { mergeModuleConfig({ name: npmName,