调整微信插件,支持node_modules引用
This commit is contained in:
parent
40c8a4f069
commit
d3413a47ff
|
|
@ -19,6 +19,11 @@ const pluginName = 'OakWeChatMpPlugin';
|
|||
const OakPagePrefix = '@oak-general-business';
|
||||
const OakPagePath = 'node_modules/oak-general-business/wechatMp/';
|
||||
|
||||
const MODE = {
|
||||
oak: 'oak', // 引用oak公用库
|
||||
external: 'external', // 引用node_modules里面的库
|
||||
};
|
||||
|
||||
function getIsOak(str) {
|
||||
return str.indexOf(OakPagePrefix) === 0;
|
||||
}
|
||||
|
|
@ -210,7 +215,7 @@ class OakWeChatMpPlugin {
|
|||
instance = path.resolve(this.basePath, page);
|
||||
}
|
||||
|
||||
await this.getComponents(components, instance, isOak);
|
||||
await this.getComponents(components, instance, MODE.oak);
|
||||
}
|
||||
|
||||
components = Array.from(components) || [];
|
||||
|
|
@ -239,7 +244,7 @@ class OakWeChatMpPlugin {
|
|||
}
|
||||
|
||||
// parse components
|
||||
async getComponents(components, instance, isOak) {
|
||||
async getComponents(components, instance, mode) {
|
||||
try {
|
||||
const { usingComponents = {} } = fsExtra.readJSONSync(
|
||||
`${instance}.json`
|
||||
|
|
@ -249,9 +254,9 @@ class OakWeChatMpPlugin {
|
|||
if (c.indexOf('plugin://') === 0) {
|
||||
break;
|
||||
}
|
||||
if (c.indexOf('/miniprogram_npm') === 0) {
|
||||
if (c.indexOf('/npm_components') === 0) {
|
||||
const component = c.replace(
|
||||
/\/miniprogram_npm/,
|
||||
/\/npm_components/,
|
||||
'node_modules'
|
||||
);
|
||||
if (!this.npmComponents.has(component)) {
|
||||
|
|
@ -259,12 +264,13 @@ class OakWeChatMpPlugin {
|
|||
components.add(component);
|
||||
this.getComponents(
|
||||
components,
|
||||
path.resolve(process.cwd(), component)
|
||||
path.resolve(process.cwd(), component),
|
||||
MODE.external
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (isOak) {
|
||||
if (mode === MODE.oak) {
|
||||
const component = path
|
||||
.resolve(instanceDir, c)
|
||||
.replace(/\\/g, '/');
|
||||
|
|
@ -278,7 +284,24 @@ class OakWeChatMpPlugin {
|
|||
await this.getComponents(
|
||||
components,
|
||||
path.resolve(process.cwd(), component2),
|
||||
isOak
|
||||
mode
|
||||
);
|
||||
}
|
||||
} else if (mode === MODE.external) {
|
||||
const component = path
|
||||
.resolve(instanceDir, c)
|
||||
.replace(/\\/g, '/');
|
||||
const component2 = component.replace(
|
||||
process.cwd().replace(/\\/g, '/') + '/',
|
||||
''
|
||||
);
|
||||
if (!this.npmComponents.has(component2)) {
|
||||
this.npmComponents.add(component2);
|
||||
components.add(component2);
|
||||
await this.getComponents(
|
||||
components,
|
||||
path.resolve(process.cwd(), component2),
|
||||
mode
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -310,7 +333,7 @@ class OakWeChatMpPlugin {
|
|||
new EntryPlugin(
|
||||
this.basePath,
|
||||
path.join(process.cwd(), resource),
|
||||
resource.replace(/node_modules/, 'miniprogram_npm')
|
||||
resource.replace(/node_modules/, 'npm_components')
|
||||
).apply(compiler);
|
||||
} else if (this.oakPages.has(resource)) {
|
||||
new EntryPlugin(
|
||||
|
|
@ -421,7 +444,7 @@ class OakWeChatMpPlugin {
|
|||
),
|
||||
to: resource.replace(
|
||||
/node_modules/,
|
||||
'miniprogram_npm'
|
||||
'npm_components'
|
||||
),
|
||||
globOptions: {
|
||||
ignore: [
|
||||
|
|
|
|||
Loading…
Reference in New Issue