UNPKG

@tarojs/mini-runner

Version:

Mini app runner for taro

152 lines 6.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addRequireToSource = exports.getIdOrName = void 0; const helper_1 = require("@tarojs/helper"); const shared_1 = require("@tarojs/shared"); const path = require("path"); const webpack_sources_1 = require("webpack-sources"); const component_1 = require("../template/component"); const PLUGIN_NAME = 'TaroLoadChunksPlugin'; class TaroLoadChunksPlugin { constructor(options) { this.commonChunks = options.commonChunks; this.isBuildPlugin = options.isBuildPlugin; this.framework = options.framework; this.addChunkPages = options.addChunkPages; this.pages = options.pages; this.isBuildQuickapp = options.isBuildQuickapp; this.needAddCommon = options.needAddCommon || []; } apply(compiler) { const pagesList = this.pages; const addChunkPagesList = new Map(); compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { let commonChunks; const fileChunks = new Map(); compilation.hooks.afterOptimizeChunks.tap(PLUGIN_NAME, (chunks) => { /** * 收集 common chunks 中使用到 @tarojs/components 中的组件 */ commonChunks = chunks.filter(chunk => this.commonChunks.includes(chunk.name)).reverse(); this.isCompDepsFound = false; for (const chunk of commonChunks) { this.collectComponents(chunk); } if (!this.isCompDepsFound) { // common chunks 找不到再去别的 chunk 中找 chunks .filter(chunk => !this.commonChunks.includes(chunk.name)) .some(chunk => { this.collectComponents(chunk); return this.isCompDepsFound; }); } /** * 收集开发者在 addChunkPages 中配置的页面及其需要引用的公共文件 */ if (typeof this.addChunkPages === 'function') { this.addChunkPages(addChunkPagesList, Array.from(pagesList).map(item => item.name)); chunks.forEach(chunk => { const id = getIdOrName(chunk); addChunkPagesList.forEach((deps, pageName) => { if (pageName === id) { const depChunks = deps.map(dep => ({ name: dep })); fileChunks.set(id, depChunks); } }); }); } }); compilation.chunkTemplate.hooks.renderWithEntry.tap(PLUGIN_NAME, (modules, chunk) => { if (chunk.entryModule) { const entryModule = chunk.entryModule.rootModule ? chunk.entryModule.rootModule : chunk.entryModule; if (entryModule.miniType === helper_1.META_TYPE.EXPORTS) { const source = new webpack_sources_1.ConcatSource(); source.add('module.exports='); source.add(modules); return source; } } }); /** * 在每个 chunk 文本刚生成后,按判断条件在文本头部插入 require 语句 */ compilation.chunkTemplate.hooks.renderWithEntry.tap(PLUGIN_NAME, (modules, chunk) => { if (chunk.entryModule) { if (this.isBuildPlugin) { return addRequireToSource(getIdOrName(chunk), modules, commonChunks); } const entryModule = chunk.entryModule.rootModule ? chunk.entryModule.rootModule : chunk.entryModule; const { miniType } = entryModule; if (this.needAddCommon.length) { for (const item of this.needAddCommon) { if (getIdOrName(chunk) === item) { return addRequireToSource(item, modules, commonChunks); } } } if (miniType === helper_1.META_TYPE.ENTRY) { return addRequireToSource(getIdOrName(chunk), modules, commonChunks); } if (this.isBuildQuickapp && (miniType === helper_1.META_TYPE.PAGE || miniType === helper_1.META_TYPE.COMPONENT)) { return addRequireToSource(getIdOrName(chunk), modules, commonChunks); } // addChunkPages if (fileChunks.size && (miniType === helper_1.META_TYPE.PAGE || miniType === helper_1.META_TYPE.COMPONENT)) { let source; const id = getIdOrName(chunk); fileChunks.forEach((v, k) => { if (k === id) { source = addRequireToSource(id, modules, v); } }); return source; } } }); }); } collectComponents(chunk) { Array.from(chunk.modulesIterable).some(m => { if (m.rawRequest === helper_1.taroJsComponents) { this.isCompDepsFound = true; const includes = component_1.componentConfig.includes; if (Array.isArray(m.usedExports)) { m.usedExports.map(shared_1.toDashed).map(includes.add.bind(includes)); } else { component_1.componentConfig.includeAll = true; } return true; } }); } } exports.default = TaroLoadChunksPlugin; /** * @returns chunk.id || chunk.name */ function getIdOrName(chunk) { if (typeof chunk.id === 'string') { return chunk.id; } return chunk.name; } exports.getIdOrName = getIdOrName; /** * 在文本头部加入一些 require 语句 */ function addRequireToSource(id, modules, commonChunks) { const source = new webpack_sources_1.ConcatSource(); commonChunks.forEach(chunkItem => { source.add(`require(${JSON.stringify((0, helper_1.promoteRelativePath)(path.relative(id, chunkItem.name)))});\n`); }); source.add('\n'); source.add(modules); source.add(';'); return source; } exports.addRequireToSource = addRequireToSource; //# sourceMappingURL=TaroLoadChunksPlugin.js.map