UNPKG

@tarojs/webpack-runner

Version:

webpack runner for taro

111 lines 7.43 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _TaroComponentsExportsPlugin_componentsExports; Object.defineProperty(exports, "__esModule", { value: true }); const helper_1 = require("@tarojs/helper"); const shared_1 = require("@tarojs/shared"); const component_1 = require("../utils/component"); const walk = require('acorn-walk'); const NullDependency = require('webpack/lib/dependencies/NullDependency'); const PLUGIN_NAME = 'TaroComponentsExportsPlugin'; class TaroComponentsExportsPlugin { constructor(options) { this.options = options; _TaroComponentsExportsPlugin_componentsExports.set(this, void 0); __classPrivateFieldSet(this, _TaroComponentsExportsPlugin_componentsExports, new Set(), "f"); this.onParseCreateElement = options === null || options === void 0 ? void 0 : options.onParseCreateElement; } apply(compiler) { compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation, { normalModuleFactory }) => { // react 的第三方组件支持 normalModuleFactory.hooks.parser.for('javascript/auto').tap(PLUGIN_NAME, (parser) => { parser.hooks.program.tap(PLUGIN_NAME, (program) => { walk.simple(program, { CallExpression: node => { var _a; const callee = node.callee; if (callee.type === 'MemberExpression') { if (callee.property.name !== 'createElement') { return; } } else { const nameOfCallee = callee.name; if ( // 兼容 react17 new jsx transtrom nameOfCallee !== '_jsx' && nameOfCallee !== '_jsxs' && // 兼容 Vue 3.0 渲染函数及 JSX !(nameOfCallee && nameOfCallee.includes('createVNode')) && !(nameOfCallee && nameOfCallee.includes('createBlock')) && !(nameOfCallee && nameOfCallee.includes('createElementVNode')) && !(nameOfCallee && nameOfCallee.includes('createElementBlock')) && !(nameOfCallee && nameOfCallee.includes('resolveComponent')) // 收集使用解析函数的组件名称 // TODO: 兼容 vue 2.0 渲染函数及 JSX,函数名 h 与 _c 在压缩后太常见,需要做更多限制后才能兼容 // nameOfCallee !== 'h' && nameOfCallee !== '_c' ) { return; } } const type = node.arguments[0]; if (type.value) { (_a = this.onParseCreateElement) === null || _a === void 0 ? void 0 : _a.call(this, type.value, component_1.componentConfig); __classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").add(type.value); } } }, Object.assign(Object.assign({}, walk.base), { Import: walk.base.Import || (() => { }) })); }); }); compilation.hooks.finishModules.tap(PLUGIN_NAME, (modules) => { const module = Array.from(modules).find((e) => e.rawRequest === helper_1.taroJsComponents); if (!module) return; // Note: 仅在生产环境使用 if (compiler.options.mode === 'production') { if (__classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").size > 0) { compilation.dependencyTemplates.set(NullDependency, new NullDependency.Template()); module.dependencies = module.dependencies.map((dependency) => { var _a; if (!(dependency === null || dependency === void 0 ? void 0 : dependency.name)) return dependency; const name = (0, shared_1.toDashed)(dependency.name); const taroName = `taro-${name}`; // Note: Vue2 目前无法解析,需要考虑借助 componentConfig.includes 优化 if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.framework) === helper_1.FRAMEWORK_MAP.VUE ? !component_1.componentConfig.includes.has(name) : !__classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").has(taroName)) { // Note: 使用 Null 依赖替换不需要的依赖,如果使用 `dependency.disconnect` 移除会抛出 `MODULE_NOT_FOUND` 错误 return new NullDependency(); } return dependency; }); } } }); normalModuleFactory.hooks.afterResolve.tap(PLUGIN_NAME, (resolveData) => { if (resolveData.rawRequest === helper_1.taroJsComponents) { resolveData.dependencies.forEach((dependency) => { if (dependency.directImport && dependency.id) { const item = dependency.id; component_1.componentConfig.includes.add((0, shared_1.toDashed)(item)); __classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").add(`taro-${(0, shared_1.toDashed)(item)}`); } else if (dependency.type === 'harmony import specifier') { component_1.componentConfig.includeAll = true; } }); } }); }); } } exports.default = TaroComponentsExportsPlugin; _TaroComponentsExportsPlugin_componentsExports = new WeakMap(); //# sourceMappingURL=TaroComponentsExportsPlugin.js.map