UNPKG

@ainc/script

Version:

Script compiler for typescript

118 lines 3.75 kB
/** ***************************************** * Created by edonet@163.com * Created on 2021-07-09 23:16:56 ***************************************** */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); /** ***************************************** * 加载依赖 ***************************************** */ const core_1 = require("@babel/core"); const helper_plugin_utils_1 = require("@babel/helper-plugin-utils"); const dirname_1 = require("@ainc/fs/dist/dirname"); const includePaths_1 = require("@ainc/fs/dist/includePaths"); const resolvePath_1 = require("@ainc/fs/dist/resolvePath"); /** ***************************************** * 转换的资源路径 ***************************************** */ function transformSourceNode(expr, state) { if (!expr || !state.resolveFile || !core_1.types.isStringLiteral(expr)) { return; } // 获取路径 const sourcePath = expr.node.value; const resolvedPath = state.resolveFile(sourcePath, state.dirname); // 无需处理 if (!resolvedPath || sourcePath === resolvedPath) { return; } // 替换路径 expr.replaceWith(core_1.types.stringLiteral(resolvedPath)); } /** ***************************************** * 匹配调用函数 ***************************************** */ function matchSourceCall(name, expr) { const { node } = expr; // 匹配成员节点 if (core_1.types.isMemberExpression(node)) { return expr.matchesPattern(name); } // 非标识府或匹配成员 if (!core_1.types.isIdentifier(node) || name.indexOf('.') > -1) { return false; } // 匹配函数名 return node.name === name; } /** ***************************************** * 访问调用表达式 ***************************************** */ function visitCallExpression(expr, state) { // 处理`import`调用 if (core_1.types.isImport(expr.node.callee)) { return transformSourceNode(expr.get('arguments.0'), state); } // 不存在转换函数 if (!state.calls) { return; } // 获取调用节点 const callee = expr.get('callee'); const isSourceCall = state.calls.some(value => matchSourceCall(value, callee)); // 处理函数调用 if (isSourceCall) { transformSourceNode(expr.get('arguments.0'), state); } } /** ***************************************** * 访问载入声明 ***************************************** */ function visitImportDeclaration(expr, state) { transformSourceNode(expr.get('source'), state); } /** ***************************************** * 定义插件 ***************************************** */ exports.default = (0, helper_plugin_utils_1.declare)((api, opts = {}) => { const include = (0, includePaths_1.includePaths)(opts.exclude || ['node_modules']); const calls = ['require', 'require.resolve']; const resolveFile = (0, resolvePath_1.resolvePath)(opts); // 校验版本 api.assertVersion(7); // 添加处理函数 opts.calls?.length && calls.push(...opts.calls); // 返回插件配置 return { name: 'module-paths-plugin', pre(file) { const { filename } = file.opts; // 过滤文件 if (!filename || !include(filename)) { this.calls = calls; this.resolveFile = resolveFile; this.dirname = filename ? (0, dirname_1.dirname)(filename) : process.cwd(); } }, visitor: { ImportDeclaration: visitImportDeclaration, ExportDeclaration: visitImportDeclaration, CallExpression: visitCallExpression, }, }; }); //# sourceMappingURL=module-paths-plugin.js.map