UNPKG

unbag

Version:

一个专门用来开发npm工具的包

156 lines (155 loc) 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransformActionTaskAlias = exports.DefaultTransformActionTaskAliasOptions = void 0; var _path = _interopRequireWildcard(require("./../../../../utils/path.cjs")); var _core = require("@babel/core"); var _nodeModule = require("node:module"); var _config = require("./../../../../utils/config.cjs"); var _common = require("./../../../../utils/common.cjs"); var _index = require("../index.cjs"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const require2 = (0, _nodeModule.createRequire)(import.meta.url); const DefaultTransformActionTaskAliasOptions = exports.DefaultTransformActionTaskAliasOptions = { paths: {}, pathResolver: params => { const { finalOptions, sourcePath, inputDir, filePath, finalUserConfig } = params; const { paths } = finalOptions; const rootPath = (0, _common.useRoot)({ finalUserConfig }); const matched = Object.entries(paths).find(([key2]) => sourcePath.startsWith(`${key2}/`)); if (!matched) { return sourcePath; } const [key, value] = matched; const absoluteSourcePath = new _path.AbsolutePath({ content: sourcePath.replace(`${key}/`, `${_path.default.resolve(rootPath.content, value)}/`) }); const absoluteFilePath = filePath.toAbsolutePath({ rel: inputDir }); const absoluteFileDir = new _path.AbsolutePath({ content: _path.default.dirname(absoluteFilePath.content) }); const relativePath = absoluteSourcePath.toRelativePath({ rel: absoluteFileDir }); const relativePathContent = "./" + relativePath.content; return relativePathContent; }, fileResolvers: { ".js": async ({ filePath, inputDir, finalOptions, finalUserConfig }) => { const { pathResolver } = finalOptions; const absoluteFilePath = filePath.toAbsolutePath({ rel: inputDir }); const jsFile = await (0, _core.transformFileAsync)(absoluteFilePath.content, { plugins: [[require2.resolve("babel-plugin-module-resolver"), { resolvePath: sourcePath => { return pathResolver({ sourcePath, finalOptions, finalUserConfig, inputDir, filePath }); } }]] }); return jsFile?.code || ""; }, ".ts": async ({ filePath, inputDir, finalOptions, finalUserConfig }) => { const { pathResolver } = finalOptions; const absoluteFilePath = filePath.toAbsolutePath({ rel: inputDir }); const jsFile = await (0, _core.transformFileAsync)(absoluteFilePath.content, { plugins: [[require2.resolve("@babel/plugin-syntax-typescript")], [require2.resolve("babel-plugin-module-resolver"), { resolvePath: sourcePath => { return pathResolver({ sourcePath, finalOptions, finalUserConfig, inputDir, filePath }); } }]] }); return jsFile?.code || ""; } }, getFileResolver: ({ filePath, finalOptions }) => { const { fileResolvers } = finalOptions; const extName = _path.default.extname(filePath.content); return fileResolvers[extName]; } }; const TransformActionTaskAlias = options => { const finalOptions = (0, _config.mergeConfig)(DefaultTransformActionTaskAliasOptions, options || {}); return (0, _index.defineTransformActionTask)(async params => { const { finalUserConfig, inputDir, filePaths } = params; return await Promise.all(filePaths.map(async filePath => { const resolver = await finalOptions.getFileResolver({ finalUserConfig, filePath, inputDir, finalOptions }); if (resolver) { return { from: filePath, to: filePath, type: _index.TransformActionTaskOutFileType.Transformed, content: await resolver({ finalUserConfig, filePath, inputDir, finalOptions }), sourcemap: void 0 }; } return { from: filePath, to: filePath, type: _index.TransformActionTaskOutFileType.Copy }; })); }); }; exports.TransformActionTaskAlias = TransformActionTaskAlias;