@eljs/utils
Version:
Collection of nodejs utility.
97 lines (95 loc) • 3.09 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/file/transformer.ts
var transformer_exports = {};
__export(transformer_exports, {
Transformer: () => Transformer
});
module.exports = __toCommonJS(transformer_exports);
var import_node_path = __toESM(require("node:path"));
var import_pirates = require("pirates");
var Transformer = class {
/**
* 构造函数选项
*/
constructorOptions;
_revert = () => {
};
constructor(options) {
this.constructorOptions = options;
}
/**
* 执行文件劫持
* @param options 文件转换函数选项
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
apply(options) {
const { exts = [".ts"], ignoreNodeModules } = this.constructorOptions;
this._revert = (0, import_pirates.addHook)(
(code, filename) => this._transform(code, filename, options || {}),
{
exts,
ignoreNodeModules
}
);
}
/**
* 恢复文件劫持
*/
revert() {
this._revert();
}
/**
* 转换文件
* @param input 源文件内容
* @param filename 源文件名
* @param options 文件转换函数选项
*/
_transform(input, filename, options) {
const ext = import_node_path.default.extname(filename);
try {
const { code } = this.constructorOptions.transform(input, {
sourcefile: filename,
loader: ext.slice(1),
target: "es2019",
format: "cjs",
logLevel: "error",
...options
});
return code;
} catch (error) {
const err = error;
err.message = `Transform ${filename} failed: ${err.message}`;
throw err;
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Transformer
});