我正在使用CustomTransformer更新以下项目的导入:
import { global_spacer_form_element } from '@patternfly/react-tokens';
export const disabledLabelClassNameEx = global_spacer_form_element.var;
到
import global_spacer_form_element from '@patternfly/react-tokens/dist/js/global_spacer_form_element';
export const disabledLabelClassNameEx = global_spacer_form_element.var;
但是,当与ts-loader
我一起使用时,将获得以下输出(直接从ts-loader):
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.disabledLabelClassNameEx = void 0;
const global_spacer_form_element_1 = __importDefault(require("@patternfly/react-tokens/dist/js/global_spacer_form_element"));
exports.disabledLabelClassNameEx = react_tokens_1.global_spacer_form_element.var;
//# sourceMappingURL=Recipient2.js.map
而不是global_spacer_form_element
直接使用,而是使用react_tokens_1.global_spacer_form_element
。
我想打字机编译器用来构建该react_tokens_1
变量的转换器中缺少一些东西。
转换器在其访问者中执行以下操作(为了简化说明,我正在简化转换器代码,完整的代码可以在此处查看):
const visitor: ts.Visitor = (node) => {
if (ts.isSourceFile(node)) {
return ts.visitEachChild(node, visitor, context)
}
if (!ts.isImportDeclaration(node) /* or if the lib name is not '@patternfly/react-tokens' */) {
return node
}
// for simplicity assume we take all NamedImports and the only found is...
const elements = ['global_spacer_form_element']
const importPath = '@patternfly/react-tokens/dist/js/global_spacer_form_element'
return elements.map((e) => {
return ts.factory.createImportDeclaration(
undefined,
undefined,
ts.factory.createImportClause(
false,
ts.factory.createIdentifier(e),
undefined,
),
ts.factory.createStringLiteral(importPath),
)
})
}
我的tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"allowJs": true,
"checkJs": false,
"jsx": "react",
"outDir": "./build",
"removeComments": true,
"pretty": true,
"skipLibCheck": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"noImplicitAny": false,
"sourceMap": true,
"resolveJsonModule" : true
},
"include": [
"./src/**/*"
],
"exclude": [
"./node_modules/*",
"**/*.js"
]
}
最后是ts-loader
配置:
{
test: /src\/.*\.tsx?$/,
loader: 'ts-loader',
exclude: /(node_modules)/i,
options: {
getCustomTransformers: () => ({
before: [
tsImportPluginFactory({
libraryName: '@patternfly/react-tokens',
libraryDirectory: 'dist/js',
camel2DashComponentName: false
})
]
})
}
对我还需要更新什么或可以检查什么以确保该变压器按预期工作的任何想法?
编辑:对旧导入的引用已消失,但是在新导入也得到转换之前,我没有注意到:例如从foobar
到foobar_1
。
TypeScript编译器具有四个主要阶段-解析,绑定,类型检查和发射。绑定是解析标识符之间的关系的地方,但是转换发生在“发射”阶段。因此,当您进行转换时,为时已晚,编译器已经弄清楚了要转换的标识符。
一种方法是遍历文件中的所有节点,找到与导入中的一个匹配的标识符,然后通过返回context.factory.createIdentifier(node.escapedText)
该节点的访问者来重新创建这些标识符。这将使编译器在发出时保持原样。
不过,问题可能出在文件中哪些标识符引用了已命名的导入标识符。通常,我不建议在转换中使用类型检查器,因为当文件上发生多个转换时,它可能导致意外的结果,但是您可能可以先检查标识符是否escapeText
匹配,然后再检查是否typeChecker.getSymbolAtLocation(node)?.declarations[0]
等于在原始导入声明中找到的命名的导出标识符。另外,我认为您将必须实施自己的范围分析。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句