1 | "use strict";
|
2 | var __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 | };
|
8 | var __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 | };
|
13 | var _TaroComponentsExportsPlugin_componentsExports;
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const helper_1 = require("@tarojs/helper");
|
16 | const shared_1 = require("@tarojs/shared");
|
17 | const component_1 = require("../utils/component");
|
18 | const walk = require('acorn-walk');
|
19 | const NullDependency = require('webpack/lib/dependencies/NullDependency');
|
20 | const PLUGIN_NAME = 'TaroComponentsExportsPlugin';
|
21 | class 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 |
|
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 |
|
46 | nameOfCallee !== '_jsx' && nameOfCallee !== '_jsxs' &&
|
47 |
|
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 |
|
54 |
|
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 |
|
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 |
|
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 |
|
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 | }
|
109 | exports.default = TaroComponentsExportsPlugin;
|
110 | _TaroComponentsExportsPlugin_componentsExports = new WeakMap();
|
111 |
|
\ | No newline at end of file |