小程序插件 分包处理

This commit is contained in:
Wang Kejun 2023-01-10 10:35:12 +08:00
parent 356578bed2
commit 707b717403
1 changed files with 47 additions and 54 deletions

View File

@ -188,7 +188,7 @@ class OakWeChatMpPlugin {
// resolve page components // resolve page components
for (const page of pages) { for (const page of pages) {
const { aliasPath } = this.getAliasAndPath(page); const aliasPath = this.getAliasPath(page);
if (aliasPath) { if (aliasPath) {
realPages.push(aliasPath); realPages.push(aliasPath);
await this.getComponents(components, aliasPath); await this.getComponents(components, aliasPath);
@ -257,7 +257,7 @@ class OakWeChatMpPlugin {
continue; continue;
} }
const { aliasPath } = this.getAliasAndPath(c); const aliasPath = this.getAliasPath(c);
if (aliasPath) { if (aliasPath) {
if (!components.has(aliasPath)) { if (!components.has(aliasPath)) {
components.add(aliasPath); components.add(aliasPath);
@ -293,7 +293,7 @@ class OakWeChatMpPlugin {
resource.replace(/node_modules/, 'npm_components') resource.replace(/node_modules/, 'npm_components')
).apply(compiler); ).apply(compiler);
} else { } else {
const { prefixPath, alias } = this.getPrefixPath(resource); const prefixPath = this.getPrefixPath(resource);
if (prefixPath) { if (prefixPath) {
new EntryPlugin( new EntryPlugin(
this.basePath, this.basePath,
@ -377,7 +377,7 @@ class OakWeChatMpPlugin {
// code splite // code splite
applyPlugin(compiler) { applyPlugin(compiler) {
const that = this; const _this = this;
const { runtimeChunkName, commonsChunkName, vendorChunkName } = const { runtimeChunkName, commonsChunkName, vendorChunkName } =
this.options; this.options;
const subpackRoots = this.appEntries.subPageRoots; const subpackRoots = this.appEntries.subPageRoots;
@ -386,13 +386,13 @@ class OakWeChatMpPlugin {
new optimize.RuntimeChunkPlugin({ new optimize.RuntimeChunkPlugin({
name({ name }) { name({ name }) {
const index = independentPageRoots.findIndex((item) => { const index = independentPageRoots.findIndex((item) => {
return name.includes(item); const item2 = _this.getReplaceAlias(item);
return name.includes(item2);
}); });
if (index !== -1) { if (index !== -1) {
return path.join( const item = independentPageRoots[index];
independentPageRoots[index], const item2 = _this.getReplaceAlias(item);
runtimeChunkName return path.join(item2, runtimeChunkName);
);
} }
return runtimeChunkName; return runtimeChunkName;
}, },
@ -467,19 +467,13 @@ class OakWeChatMpPlugin {
minChunks: 2, minChunks: 2,
name({ context }) { name({ context }) {
const index = subpackRoots.findIndex((item) => { const index = subpackRoots.findIndex((item) => {
const { aliasPath, alias } = const item2 = _this.getReplaceAlias(item);
that.getAliasAndPath(item);
let item2 = item;
if (aliasPath) {
item2 = item.replace(`${alias}/`, '');
}
return context.includes(item2); return context.includes(item2);
}); });
if (index !== -1) { if (index !== -1) {
return path.join( const item = subpackRoots[index];
subpackRoots[index], const item2 = _this.getReplaceAlias(item);
commonsChunkName return path.join(item2, commonsChunkName);
);
} }
return commonsChunkName; return commonsChunkName;
}, },
@ -492,9 +486,7 @@ class OakWeChatMpPlugin {
async emitAssetsFile(compilation) { async emitAssetsFile(compilation) {
const emitAssets = []; const emitAssets = [];
for (let entry of this.assetsEntry) { for (let entry of this.assetsEntry) {
const { prefixPath, alias } = this.getPrefixPath( const prefixPath = this.getPrefixPath(replaceDoubleSlash(entry));
replaceDoubleSlash(entry)
);
if (prefixPath) { if (prefixPath) {
const assets = entry; const assets = entry;
@ -532,10 +524,7 @@ class OakWeChatMpPlugin {
let pages = []; let pages = [];
if (appJson.pages) { if (appJson.pages) {
for (let page of appJson.pages) { for (let page of appJson.pages) {
const { aliasPath, alias } = this.getAliasAndPath(page); page = this.getReplaceAlias(page);
if (aliasPath) {
page = page.replace(`${alias}/`, '');
}
pages.push(page); pages.push(page);
} }
appJson.pages = pages; appJson.pages = pages;
@ -545,15 +534,10 @@ class OakWeChatMpPlugin {
if (appJson.usingComponents) { if (appJson.usingComponents) {
for (let ck of Object.keys(appJson.usingComponents)) { for (let ck of Object.keys(appJson.usingComponents)) {
let component = appJson.usingComponents[ck]; let component = appJson.usingComponents[ck];
if ( if (ck === debugPanel.name && !debugPanel.show) {
ck === debugPanel.name && !debugPanel.show
) {
continue; continue;
} }
const { aliasPath, alias } = this.getAliasAndPath(component); component = this.getReplaceAlias(component);
if (aliasPath) {
component = component.replace(`${alias}/`, '');
}
usingComponents[ck] = component; usingComponents[ck] = component;
} }
appJson.usingComponents = usingComponents; appJson.usingComponents = usingComponents;
@ -563,10 +547,7 @@ class OakWeChatMpPlugin {
const subPackages = appJson.subpackages; const subPackages = appJson.subpackages;
for (const subPage of subPackages) { for (const subPage of subPackages) {
const root = subPage.root; const root = subPage.root;
const { aliasPath, alias } = this.getAliasAndPath(root); subPage.root = this.getReplaceAlias(root);
if (aliasPath) {
subPage.root = root.replace(`${alias}/`, '');
}
} }
appJson.subpackages = subPackages; appJson.subpackages = subPackages;
} }
@ -580,9 +561,9 @@ class OakWeChatMpPlugin {
for (let ck of Object.keys(json.usingComponents)) { for (let ck of Object.keys(json.usingComponents)) {
let component = json.usingComponents[ck]; let component = json.usingComponents[ck];
const { aliasPath, alias } = this.getAliasAndPath(component); const alias = this.getAlias(component);
if (aliasPath) { if (alias) {
component = component.replace(`${alias}/`, ''); component = this.getReplaceAlias(component);
const parentPath = path.resolve(this.basePath, entry); const parentPath = path.resolve(this.basePath, entry);
const parentDir = path.parse(parentPath).dir; const parentDir = path.parse(parentPath).dir;
@ -615,39 +596,51 @@ class OakWeChatMpPlugin {
this.alias = compiler.options.resolve.alias || {}; this.alias = compiler.options.resolve.alias || {};
} }
getAliasAndPath(path) { getAlias(path) {
let aliasPath = '';
let alias = ''; let alias = '';
// 处理下别名 返回真实path // 返回 alias key
for (const k of Object.keys(this.alias)) { for (const k of Object.keys(this.alias)) {
if (path.includes(`${k}/`)) { if (path.includes(`${k}/`)) {
aliasPath = replaceDoubleSlash(path.replace(k, this.alias[k]));
alias = k; alias = k;
break; break;
} }
} }
return { return alias;
aliasPath, }
alias,
}; getReplaceAlias(path) {
// 处理 @project/xx/xx路径 转换成编译下路径
const alias = this.getAlias(path);
let path2 = path;
if (alias) {
path2 = path.replace(`${alias}/`, '');
}
return path2;
}
getAliasPath(path) {
let aliasPath = '';
const alias = this.getAlias(path);
// 处理下别名 返回真实path
if (alias) {
aliasPath = replaceDoubleSlash(
path.replace(alias, this.alias[alias])
);
}
return aliasPath;
} }
getPrefixPath(resource) { getPrefixPath(resource) {
let prefixPath = ''; let prefixPath = '';
let alias = '';
// 获取路径 别名 // 获取路径 别名
for (const k of Object.keys(this.alias)) { for (const k of Object.keys(this.alias)) {
const prefix = replaceDoubleSlash(this.alias[k]); const prefix = replaceDoubleSlash(this.alias[k]);
if (resource.includes(prefix)) { if (resource.includes(prefix)) {
alias = k;
prefixPath = prefix; prefixPath = prefix;
break; break;
} }
} }
return { return prefixPath;
prefixPath,
alias,
};
} }
// script full path // script full path