大家好,又见面了,我是你们的朋友全栈君。
PHP 当中有许多很有用的魔术变量, 比如__CLASS__
, __METHOD__
之类. 但是typescript中并没有. 因此我写了一个插件typescript-magic-variable-plugin
来使用它们, 源代码已经放到了GitHub上: https://github.com/acrazing/t….
npm install -D typescript-magic-variable-plugin
{
"compilerOptions": {
"plugins": [
{ "transform": "typescript-magic-variable-plugin" }
]
},
"include": [
// ...
"./node_modules/typescript/magic-variable-plugin/types/globals.d.ts"
]
}
export class Foo {
constructor() {
console.log(__CLASS__)
}
}
ttypescript
来编译你的项目, 注意这里不能用typescript
, 因为没有开放transform
接口, 需要全局安装一下ttypescript
: npm i -g ttypescript
, 然后调用ttsc
来编译.const { createMagicVariableTransformer } = require('typescript-magic-variable-plugin')
// ...
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
options: {
// ... other loader's options
getCustomTransformers: program => ({
before: [
createMagicVariableTransformer(program, {})
]
})
}
}
]
displayName
属性, 默认开启, 比如:export class Foo extends Component
Foo
增加静态属性: Foo.displayName = "Foo"
interface Options {
addDisplayName: boolean; // 是否给react组件添加displayName属性, 默认开启
rootDir: string; // __DIR__的相对路径, 默认为tscofnig.json中的rootDir或者当前文件夹
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/107352.html原文链接:https://javaforall.cn