【问题标题】:gulp / rollup / typescript — __decorate is not definedgulp / rollup / typescript — __decorate 未定义
【发布时间】:2020-12-01 08:22:44
【问题描述】:

我有这个gulpfile.js

import gulp from 'gulp';
import { rollup } from 'rollup';
import rollupTypescript from '@rollup/plugin-typescript'

const compileTs = async (src, dest) => {
  const bundle = await rollup({
    input: src,
    plugins: [
      rollupTypescript()
    ]
  });

  await bundle.write({
    file: dest,
    format: 'es',
    name: 'library',
    sourcemap: 'inline'
  });
}

export const ts = async _ => await Promise.all([
    compileTs('./src/ts/main.ts', './bin/js/bin.js')
  ]);

tsconfig.json(同一目录)内我有:

compilerOptions: {
  "target": "ESNEXT",
  "module": "ESNext",
  "importHelpers": true, 
  "experimentalDecorators": true
}

src/ts/main.ts 内有:

const Foo = (fx: Function) => {
  console.log(fx);
}

@Foo
class Bar {

}

$ npx gulp ts 创建所需的文件。但是当我运行它时,控制台会告诉我:

Uncaught ReferenceError: __decorate is not defined

因为不包括来自tslib 的所有助手。

我能做些什么来解决这个问题?我尝试使用external: ['tslib'],但没有成功。

顺便说一句: 仅运行 npx tsc(加上 outdir 集)包括 __decorate

【问题讨论】:

    标签: typescript gulp rollupjs typescript-decorator


    【解决方案1】:

    我以前也遇到过完全相同的情况。问题很可能是importHelper 选项被排除在https://github.com/rollup/plugins/tree/master/packages/typescript#ignored-options 之外。

    我确实通过切换到使用 rollup-plugin-typescript2 来解决问题,默认情况下启用此选项。

    // Install `npm i -D rollup-plugin-typescript2`
    import typescript from 'rollup-plugin-typescript2';
    

    【讨论】:

    • 感谢您的指导!只是出于兴趣:在@rollup/plugin-typescript 的文档中说:»必须始终使用 tslib 辅助模块。« - 这是否意味着可以以某种方式包含 tslib
    • 我试过但没有运气 :) 这就是为什么我改用分叉的
    • 谢谢!我会试一试v2
    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2018-07-19
    • 2019-12-15
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多