自定义变压器不使用TS节点
我有一个我试图在ts节点
的开发环境中运行的项目,但是,它不断破坏我的变压器:
import * as ts from "typescript";
import { convertSystem } from "./system";
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
return (sourceFile) => {
console.log(sourceFile.getChildrenCount()); // <== This errors with:
// "Error: Debug Failure. Did not expect SourceFile to have an Identifier in its trivia"
const visitor = (node: ts.Node): ts.Node => {
// Normally I would do stuff here, but every node is of kind 305: "UnparsedSource"
return ts.visitEachChild(node, visitor, context);
};
return ts.visitNode(sourceFile, visitor);
};
};
export default (program: ts.Program) => transformer;
我应该在我的文件夹结构中指出一些奇数:
builder
|_transfomer.ts
|_transfomer_helpers
| |_...
|_package.json: I need this package.json to set "type":"module", or node will complain while running the transfomer
src
|_index.ts: Entry point
|_...
package.json (below)
tsconfig.json (also below)
我的TS -config:
{
"compilerOptions": {
/* Language and Environment */
"target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
/* Modules */
"module": "ESNext" /* Specify what module code is generated. */,
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"outDir": "./build" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"plugins": [
{
"transform": "./builder/transformer.ts",
"type": "raw"
}
]
},
"ts-node": {
"compiler": "ttypescript",
"esm": true,
"experimentalSpecifierResolution": "node",
"compilerOptions": {
"plugins": [
{
"transform": "./builder/transformer.ts",
"after": true
}
]
}
},
"include": ["src"]
}
and package.json:
{
"name": "Project",
"version": "0.0.0",
"description": "Proj description",
"type": "module",
"scripts": {
"start": "ts-node src/test/test",
},
"dependencies": {
"typescript": "^4.6.4"
},
"devDependencies": {
"ts-node": "^10.8.1",
"ttypescript": "^1.5.13"
}
}
使用普通的ttypescript进行了工作,据我所知,在网上没有任何东西。
I have a project that I am trying to run in a dev environment with ts-node
, however, it keeps breaking my transformer:
import * as ts from "typescript";
import { convertSystem } from "./system";
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
return (sourceFile) => {
console.log(sourceFile.getChildrenCount()); // <== This errors with:
// "Error: Debug Failure. Did not expect SourceFile to have an Identifier in its trivia"
const visitor = (node: ts.Node): ts.Node => {
// Normally I would do stuff here, but every node is of kind 305: "UnparsedSource"
return ts.visitEachChild(node, visitor, context);
};
return ts.visitNode(sourceFile, visitor);
};
};
export default (program: ts.Program) => transformer;
There are a few oddities I should point out in my folder structure:
builder
|_transfomer.ts
|_transfomer_helpers
| |_...
|_package.json: I need this package.json to set "type":"module", or node will complain while running the transfomer
src
|_index.ts: Entry point
|_...
package.json (below)
tsconfig.json (also below)
My ts-config:
{
"compilerOptions": {
/* Language and Environment */
"target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
/* Modules */
"module": "ESNext" /* Specify what module code is generated. */,
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"outDir": "./build" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"plugins": [
{
"transform": "./builder/transformer.ts",
"type": "raw"
}
]
},
"ts-node": {
"compiler": "ttypescript",
"esm": true,
"experimentalSpecifierResolution": "node",
"compilerOptions": {
"plugins": [
{
"transform": "./builder/transformer.ts",
"after": true
}
]
}
},
"include": ["src"]
}
And package.json:
{
"name": "Project",
"version": "0.0.0",
"description": "Proj description",
"type": "module",
"scripts": {
"start": "ts-node src/test/test",
},
"dependencies": {
"typescript": "^4.6.4"
},
"devDependencies": {
"ts-node": "^10.8.1",
"ttypescript": "^1.5.13"
}
}
This worked using normal ttypescript, and there has not been anything online so far as I can see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论