Echarts为例: 目录结构: app.config.json中配置: echarts为什么会被打包进主包中?
- 虽然引入的ec-canvas组件(echarts-for-weixin)是在分包中使用,但echarts被echarts-for-weixin组件和外部业务组件所依赖,导致Taro认为echarts.js被多个模块所依赖,所以打包到common.js。
- 导致主包大于2M失效
在config/index.js中配置
mini: {
addChunkPages(pages) {
pages.set("packageA/pages/grades/index", ["packageA/common"]);
},
webpackChain(chain) {
chain.merge({
optimization: {
splitChunks: {
cacheGroups: {
subpackagesCommon: {
name: "packageA/common",
minChunks: 2,
test: (module, chunks) => {
const isNoOnlySubpackRequired = chunks.find(
(chunk) => !/\bpackageA\b/.test(chunk.name)
);
return !isNoOnlySubpackRequired;
},
priority: 200,
},
},
},
},
});
},
}
Taro3中只针对了taro的组件做了注入的处理,对于小程序原生组件没有做注入处理
config/index.js
plugins: ["taro-plugin-pinia", [path.resolve(__dirname, "plugin.js")]],
config/plugin.js
const fs = require("fs");
const path = require("path");
export default (ctx, options) => {
ctx.onBuildFinish(() => {
console.log("echarts构建hack注入");
const target = path.join(
ctx.paths.outputPath,
"packageA/ec-canvas/ec-canvas.js"
);
console.log("触发");
const data = fs.readFileSync(target, "utf8");
fs.writeFileSync(target, `require("../common");${data}`);
});
};
|