将打字稿编译为易于浏览器使用的格式会导致“未捕获的TypeError :(无效0)不是函数”

时间:2019-03-26 08:42:14

标签: typescript webpack

我在浏览器中遇到以下错误:

index.ts:1 Uncaught TypeError: (void 0) is not a function
    at eval (index.ts:1)
    at Object../src/index.ts (main.js:96)
    at __webpack_require__ (main.js:20)
    at main.js:84
    at main.js:87

我很难知道如何调查此错误。这里似乎是相同的问题:Getting error "Void 0 is not function" on browser when using webpack to bundle typescript file

已编译输出中的eval("(void 0)(...是什么?

我正在尝试将“简单”打字稿脚本编译成可以由浏览器运行的形式。

整个MCVE都位于此仓库中:https://github.com/chrissound/newCha

我有以下webpack和打字稿配置:

webpack.config.js

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ]
  },
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  }
};

tsconfig.json

{
    "compilerOptions": {
        "sourceMap": true,
        "target": "es5",
        "outFile": "compiled.js",
        "declaration": true,
        "removeComments": true,
        "module": "system"
    },
    "include": [
        "src/*"
    ],

    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

1 个答案:

答案 0 :(得分:0)

在构建步骤中出现了警告:

npm run-script build

> react-ts-hmzrag@0.0.0 build /home/chris/fromLaptopt/usbflash/christransferNixos/Xanadata/newCha
> webpack --config webpack.config.js

Hash: eb81bd5a31b10119a931
Version: webpack 4.29.6
Time: 1071ms
Built at: 2019-03-26 08:50:29
           Asset      Size  Chunks             Chunk Names
../compiled.d.ts  27 bytes          [emitted]  
         main.js   5.1 KiB    main  [emitted]  main
Entrypoint main = main.js
[./src/index.ts] 1.29 KiB {main} [built] [1 warning]

WARNING in ./src/index.ts 1:0-15
System.register is not supported by webpack.

我通过以下方式解决了该问题:

diff --git a/tsconfig.json b/tsconfig.json
index f2ef245..5617776 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -2,10 +2,9 @@
     "compilerOptions": {
         "sourceMap": true,
         "target": "es5",
-        "outFile": "compiled.js",
         "declaration": true,
         "removeComments": true,
-        "module": "system"
+        "module" : "commonjs"
     },
     "include": [
         "src/*"