如何将变压器传递给 ts-node?
How to pass in transformers to ts-node?
由于我需要使用转换器,因此我正在尝试为 Typescript 推出自己的编译器。
我们使用 ts-node 来 运行 一些文件(单独的测试等),我还需要将转换器传递给 ts-node 编译器。
这是我的代码
const ts = require('typescript');
const tsNode = require('ts-node').register;
const keysTransformer = require( 'ts-transformer-keys/transformer');
const tsConfig = require( './tsconfig.json');
const compileProject = () => {
const { options, fileNames } = ts.parseJsonConfigFileContent(
tsConfig,
ts.sys,
__dirname
);
const program = ts.createProgram(fileNames, options);
const transformers = {
before: [keysTransformer(program)],
after: []
};
program.emit(undefined, undefined, undefined, false, transformers);
}
const compileAndRun = (files) => {
tsNode({ files, compilerOptions: tsConfig.compilerOptions, transformers: ["ts-transformer-keys/transformer"] });
files.forEach(file => {
require(file);
});
}
module.export = main = (args) => {
if(args.length >= 2) {
const fileNames = args.splice(2);
compileAndRun(fileNames);
} else {
compileProject();
}
}
main(process.argv);
将转换器传递给 TypeScript 编译器(编译整个项目时)可以正常工作
const transformers = {
before: [keysTransformer(program)],
after: []
};
但是,我找不到足够的文档来说明如何使用 ts-node 执行相同的操作。
register()
的 transformers
选项属于 CustomTransformers
类型(不是您传递的数组):
interface CustomTransformers {
/** Custom transformers to evaluate before built-in .js transformations. */
before?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .js transformations. */
after?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .d.ts transformations. */
afterDeclarations?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
}
由于我需要使用转换器,因此我正在尝试为 Typescript 推出自己的编译器。
我们使用 ts-node 来 运行 一些文件(单独的测试等),我还需要将转换器传递给 ts-node 编译器。
这是我的代码
const ts = require('typescript');
const tsNode = require('ts-node').register;
const keysTransformer = require( 'ts-transformer-keys/transformer');
const tsConfig = require( './tsconfig.json');
const compileProject = () => {
const { options, fileNames } = ts.parseJsonConfigFileContent(
tsConfig,
ts.sys,
__dirname
);
const program = ts.createProgram(fileNames, options);
const transformers = {
before: [keysTransformer(program)],
after: []
};
program.emit(undefined, undefined, undefined, false, transformers);
}
const compileAndRun = (files) => {
tsNode({ files, compilerOptions: tsConfig.compilerOptions, transformers: ["ts-transformer-keys/transformer"] });
files.forEach(file => {
require(file);
});
}
module.export = main = (args) => {
if(args.length >= 2) {
const fileNames = args.splice(2);
compileAndRun(fileNames);
} else {
compileProject();
}
}
main(process.argv);
将转换器传递给 TypeScript 编译器(编译整个项目时)可以正常工作
const transformers = {
before: [keysTransformer(program)],
after: []
};
但是,我找不到足够的文档来说明如何使用 ts-node 执行相同的操作。
register()
的 transformers
选项属于 CustomTransformers
类型(不是您传递的数组):
interface CustomTransformers {
/** Custom transformers to evaluate before built-in .js transformations. */
before?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .js transformations. */
after?: (TransformerFactory<SourceFile> | CustomTransformerFactory)[];
/** Custom transformers to evaluate after built-in .d.ts transformations. */
afterDeclarations?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
}