Skip to content

Commit

Permalink
feat: Optimize the merging of local parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgao365 committed Dec 25, 2023
1 parent 6e65e13 commit f49fc1c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
cdn: {
modules: ['vue', 'ant-design-vue', 'pinia', 'vue-router'],
local: {
modules: ['vue'],
modules: true,
copy: true,
},
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 21 additions & 22 deletions src/cdn/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,28 @@ function getProjectPkgDeps() {
return Object.keys(pkg.dependencies || {});
}

type PreHandleOptions = Omit<HtmlCdnOptions, 'local'> & { local: HtmlCdnLocal };
interface PreHandleOptions extends Omit<HtmlCdnOptions, 'local'> {
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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f49fc1c

Please sign in to comment.