@tarojs/mini-runner
Version:
Mini app runner for taro
89 lines • 5.1 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const TaroSingleEntryDependency_1 = require("../dependencies/TaroSingleEntryDependency");
const component_1 = require("../template/component");
const TaroNormalModule_1 = require("./TaroNormalModule");
const walk = require('acorn-walk');
const PLUGIN_NAME = 'TaroNormalModulesPlugin';
function isRenderNode(node, ancestors = []) {
let renderFn;
const hasRenderMethod = ancestors.some((ancestor) => {
var _a, _b;
if (ancestor.type === 'FunctionExpression' && ((_a = ancestor === null || ancestor === void 0 ? void 0 : ancestor.id) === null || _a === void 0 ? void 0 : _a.name) === 'render') {
renderFn = (_b = ancestor.params[0]) === null || _b === void 0 ? void 0 : _b.name;
return true;
}
else {
return false;
}
});
// @ts-ignore
return hasRenderMethod && node.callee.name === renderFn;
}
class TaroNormalModulesPlugin {
constructor(onParseCreateElement) {
this.onParseCreateElement = onParseCreateElement;
}
apply(compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (_, { normalModuleFactory }) => {
normalModuleFactory.hooks.createModule.tap(PLUGIN_NAME, data => {
const dependency = data.dependencies[0];
if (dependency.constructor === TaroSingleEntryDependency_1.default) {
return new TaroNormalModule_1.default(Object.assign(data, { miniType: dependency.miniType, name: dependency.name }));
}
});
// react 的第三方组件支持
normalModuleFactory.hooks.parser.for('javascript/auto').tap(PLUGIN_NAME, (parser) => {
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
walk.ancestor(ast, {
CallExpression: (node, ancestors) => {
var _a;
// @ts-ignore
const callee = node.callee;
if (callee.type === 'MemberExpression') {
if (callee.property.name !== 'createElement') {
return;
}
}
else {
const nameOfCallee = callee.name;
if (
// 兼容 react17 new jsx transtrom 以及esbuild-loader的ast兼容问题
!/^_?jsxs?$/.test(nameOfCallee) &&
// 兼容 Vue 3.0 渲染函数及 JSX
!(nameOfCallee && nameOfCallee.includes('createVNode')) &&
!(nameOfCallee && nameOfCallee.includes('createBlock')) &&
!(nameOfCallee && nameOfCallee.includes('createElementVNode')) &&
!(nameOfCallee && nameOfCallee.includes('createElementBlock')) &&
!(nameOfCallee && nameOfCallee.includes('resolveComponent')) && // 收集使用解析函数的组件名称
// 兼容 Vue 2.0 渲染函数及 JSX
!isRenderNode(node, ancestors)) {
return;
}
}
// @ts-ignore
const [type, prop] = node.arguments;
const componentName = type.name;
type.value && ((_a = this.onParseCreateElement) === null || _a === void 0 ? void 0 : _a.call(this, type.value, component_1.componentConfig));
if (componentName === 'CustomWrapper' && !component_1.componentConfig.thirdPartyComponents.get('custom-wrapper')) {
component_1.componentConfig.thirdPartyComponents.set('custom-wrapper', new Set());
}
if (component_1.componentConfig.thirdPartyComponents.size === 0) {
return;
}
const attrs = component_1.componentConfig.thirdPartyComponents.get(type.value);
if (attrs == null || !prop || prop.type !== 'ObjectExpression') {
return;
}
prop.properties
.filter(p => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name !== 'children' && p.key.name !== 'id')
.forEach(p => attrs.add(p.key.name));
}
}, Object.assign(Object.assign({}, walk.base), { Import: walk.base.Import || (() => { }) }));
});
});
});
}
}
exports.default = TaroNormalModulesPlugin;
//# sourceMappingURL=TaroNormalModulesPlugin.js.map
;