UNPKG

7.43 kBJavaScriptView Raw
1"use strict";
2var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3 if (kind === "m") throw new TypeError("Private method is not writable");
4 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5 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");
6 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7};
8var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10 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");
11 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12};
13var _TaroComponentsExportsPlugin_componentsExports;
14Object.defineProperty(exports, "__esModule", { value: true });
15const helper_1 = require("@tarojs/helper");
16const shared_1 = require("@tarojs/shared");
17const component_1 = require("../utils/component");
18const walk = require('acorn-walk');
19const NullDependency = require('webpack/lib/dependencies/NullDependency');
20const PLUGIN_NAME = 'TaroComponentsExportsPlugin';
21class TaroComponentsExportsPlugin {
22 constructor(options) {
23 this.options = options;
24 _TaroComponentsExportsPlugin_componentsExports.set(this, void 0);
25 __classPrivateFieldSet(this, _TaroComponentsExportsPlugin_componentsExports, new Set(), "f");
26 this.onParseCreateElement = options === null || options === void 0 ? void 0 : options.onParseCreateElement;
27 }
28 apply(compiler) {
29 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation, { normalModuleFactory }) => {
30 // react 的第三方组件支持
31 normalModuleFactory.hooks.parser.for('javascript/auto').tap(PLUGIN_NAME, (parser) => {
32 parser.hooks.program.tap(PLUGIN_NAME, (program) => {
33 walk.simple(program, {
34 CallExpression: node => {
35 var _a;
36 const callee = node.callee;
37 if (callee.type === 'MemberExpression') {
38 if (callee.property.name !== 'createElement') {
39 return;
40 }
41 }
42 else {
43 const nameOfCallee = callee.name;
44 if (
45 // 兼容 react17 new jsx transtrom
46 nameOfCallee !== '_jsx' && nameOfCallee !== '_jsxs' &&
47 // 兼容 Vue 3.0 渲染函数及 JSX
48 !(nameOfCallee && nameOfCallee.includes('createVNode')) &&
49 !(nameOfCallee && nameOfCallee.includes('createBlock')) &&
50 !(nameOfCallee && nameOfCallee.includes('createElementVNode')) &&
51 !(nameOfCallee && nameOfCallee.includes('createElementBlock')) &&
52 !(nameOfCallee && nameOfCallee.includes('resolveComponent')) // 收集使用解析函数的组件名称
53 // TODO: 兼容 vue 2.0 渲染函数及 JSX,函数名 h 与 _c 在压缩后太常见,需要做更多限制后才能兼容
54 // nameOfCallee !== 'h' && nameOfCallee !== '_c'
55 ) {
56 return;
57 }
58 }
59 const type = node.arguments[0];
60 if (type.value) {
61 (_a = this.onParseCreateElement) === null || _a === void 0 ? void 0 : _a.call(this, type.value, component_1.componentConfig);
62 __classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").add(type.value);
63 }
64 }
65 }, Object.assign(Object.assign({}, walk.base), { Import: walk.base.Import || (() => { }) }));
66 });
67 });
68 compilation.hooks.finishModules.tap(PLUGIN_NAME, (modules) => {
69 const module = Array.from(modules).find((e) => e.rawRequest === helper_1.taroJsComponents);
70 if (!module)
71 return;
72 // Note: 仅在生产环境使用
73 if (compiler.options.mode === 'production') {
74 if (__classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").size > 0) {
75 compilation.dependencyTemplates.set(NullDependency, new NullDependency.Template());
76 module.dependencies = module.dependencies.map((dependency) => {
77 var _a;
78 if (!(dependency === null || dependency === void 0 ? void 0 : dependency.name))
79 return dependency;
80 const name = (0, shared_1.toDashed)(dependency.name);
81 const taroName = `taro-${name}`;
82 // Note: Vue2 目前无法解析,需要考虑借助 componentConfig.includes 优化
83 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)) {
84 // Note: 使用 Null 依赖替换不需要的依赖,如果使用 `dependency.disconnect` 移除会抛出 `MODULE_NOT_FOUND` 错误
85 return new NullDependency();
86 }
87 return dependency;
88 });
89 }
90 }
91 });
92 normalModuleFactory.hooks.afterResolve.tap(PLUGIN_NAME, (resolveData) => {
93 if (resolveData.rawRequest === helper_1.taroJsComponents) {
94 resolveData.dependencies.forEach((dependency) => {
95 if (dependency.directImport && dependency.id) {
96 const item = dependency.id;
97 component_1.componentConfig.includes.add((0, shared_1.toDashed)(item));
98 __classPrivateFieldGet(this, _TaroComponentsExportsPlugin_componentsExports, "f").add(`taro-${(0, shared_1.toDashed)(item)}`);
99 }
100 else if (dependency.type === 'harmony import specifier') {
101 component_1.componentConfig.includeAll = true;
102 }
103 });
104 }
105 });
106 });
107 }
108}
109exports.default = TaroComponentsExportsPlugin;
110_TaroComponentsExportsPlugin_componentsExports = new WeakMap();
111//# sourceMappingURL=TaroComponentsExportsPlugin.js.map
\No newline at end of file