汇总打字稿装饰器支持

时间:2019-06-28 09:11:28

标签: typescript decorator rollup

我收到以下错误消息:

  

错误:解析错误:第29行:意外的令牌非法

相应的代码行是mobx观察器装饰器:

@observer
class Wrapper extends Component<IProps> {

我有以下rollup.config.js:

import typescript from 'rollup-plugin-typescript2'
import jsx from 'rollup-plugin-jsx'
import pkg from './package.json'
export default {
  input: 'src/index.ts',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
    },
    {
      file: pkg.module,
      format: 'es',
    },
  ],
  external: [
    ...Object.keys(pkg.dependencies || {}),
    ...Object.keys(pkg.peerDependencies || {}),
  ],
plugins: [
    jsx( {factory: 'React.createElement'} ),
    typescript({
      typescript: require('typescript'),
    }),
  ],
}

我的tsconfig.json:

{
  "compilerOptions": {
    "declaration": true,
    "declarationDir": "./dist",
    "module": "es6",
    "experimentalDecorators": true,
    "outDir": "./dist",
    "target": "es5",
    "jsx": "react",
    "types": ["reflect-metadata", "node"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "strictPropertyInitialization": false,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "noEmit": true,
    "emitDecoratorMetadata": true,
    "noEmitHelpers": true,
    "importHelpers": true,
    "sourceMap": true,
    "noImplicitReturns": false,
    "noImplicitThis": false,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "suppressImplicitAnyIndexErrors": false,
    "noUnusedLocals": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": ["node_modules"]
}

根据文档说明,https://github.com/ezolenko/rollup-plugin-typescript2插件应采用tsconfig.json文件中的所有选项,也应采用“ experimentalDecorators”:是的,但似乎不起作用。

1 个答案:

答案 0 :(得分:1)

由于我的错误略有不同(Error: Unexpected character '@'),因此不能100%地确定这是否适用。

但是与TypeScript装饰器一起解决汇总问题的是从outDir中删除了tsconfig.json选项。