UNPKG

@alilc/build-plugin-alt

Version:

build-scripts plugin template for developers

94 lines (93 loc) 4.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const BabelTypes = __importStar(require("@babel/types")); const template_1 = __importDefault(require("@babel/template")); ; const addInfoComment = (node) => { BabelTypes.addComment(node, 'leading', ' Generated By Ali Lowcode Tools '); }; const injectMetaVisitor = { AssignmentExpression(path, pass) { const left = path.node.left; if (BabelTypes.isMemberExpression(left) && BabelTypes.isIdentifier(left.object) && left.object.name === pass.declarationName) { const property = left.property; if ((BabelTypes.isIdentifier(property) && property.name === 'pluginName') || (BabelTypes.isStringLiteral(property) && property.value === 'pluginName')) { const meta = pass.meta; pass.cache.pluginNameAssignmentExists = true; if (meta === null || meta === void 0 ? void 0 : meta.pluginName) { path.node.right = BabelTypes.stringLiteral(meta.pluginName); addInfoComment(path.node); } } if ((BabelTypes.isIdentifier(property) && property.name === 'meta') || (BabelTypes.isStringLiteral(property) && property.value === 'meta')) { const meta = pass.meta; pass.cache.metaAssignmentExists = true; if (meta === null || meta === void 0 ? void 0 : meta.meta) { const generateAst = (0, template_1.default)(`const a = ${JSON.stringify(meta.meta)}`)(); path.node.right = generateAst.declarations[0].init; addInfoComment(path.node); } } } } }; function default_1(babel) { const { types: t } = babel; const cache = { pluginNameAssignmentExists: false, metaAssignmentExists: false, }; return { visitor: { ExportDefaultDeclaration(path, state) { var _a, _b, _c, _d, _e, _f, _g, _h; // 如果不是入口文件,则不做任何处理 if (state.opts.filename !== state.filename) return; if (t.isIdentifier(path.node.declaration)) { const declarationName = path.node.declaration.name; path.parentPath.traverse(injectMetaVisitor, Object.assign(Object.assign({}, state.opts), { declarationName, cache })); if (!cache.pluginNameAssignmentExists && ((_b = (_a = state.opts) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b.pluginName)) { const generatedNode = (0, template_1.default)(`${declarationName}.pluginName = "${(_d = (_c = state.opts) === null || _c === void 0 ? void 0 : _c.meta) === null || _d === void 0 ? void 0 : _d.pluginName}"`)(); addInfoComment(generatedNode); path.insertBefore(generatedNode); } if (!cache.metaAssignmentExists && ((_f = (_e = state.opts) === null || _e === void 0 ? void 0 : _e.meta) === null || _f === void 0 ? void 0 : _f.meta)) { const generatedNode = (0, template_1.default)(`${declarationName}.meta = ${JSON.stringify((_h = (_g = state.opts) === null || _g === void 0 ? void 0 : _g.meta) === null || _h === void 0 ? void 0 : _h.meta)}`)(); addInfoComment(generatedNode); path.insertBefore(generatedNode); } } }, } }; } exports.default = default_1;