使用webpack进行打字稿编译时出错

时间:2018-05-31 23:36:16

标签: typescript webpack

编译ts文件时遇到以下错误(运行webpack命令)。 此代码首先用es6编写,然后转换为typescript。

ERROR in ./src/index.ts
Module build failed: Error: Debug Failure. False expression.
    at forEachIdentifierInEntityName (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:24094:26)
    at bindPropertyAssignment (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:24044:17)
    at bindStaticPropertyAssignment (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:24029:13)
    at bindSpecialPropertyAssignment (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:24020:17)
    at bindWorker (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:23669:29)
    at bind (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:23560:13)
    at visitNode (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:14777:24)
    at Object.forEachChild (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:15021:24)
    at bindEachChild (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22352:16)
    at bindChildrenWorker (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22434:21)
    at bindChildren (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22322:17)
    at bind (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:23571:21)
    at /Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22328:94
    at bindEach (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22344:21)
    at bindEachFunctionsFirst (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22328:13)
    at bindChildrenWorker (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22426:21)
    at bindChildren (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22322:17)
    at bindContainer (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:22270:17)
    at bind (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:23574:21)
    at bindSourceFile (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:21935:17)
    at Object.bindSourceFile (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:21876:9)
    at initializeTypeChecker (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:51156:20)
    at Object.createTypeChecker (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:27045:9)
    at Object.getTypeChecker (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:78949:79)
    at synchronizeHostData (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:107271:21)
    at Object.getEmitOutput (/Users/dali/code/xxxx-sdk/node_modules/typescript/lib/typescript.js:107671:13)
    at Object.getEmitOutput (/Users/dali/code/xxxx-sdk/node_modules/ts-loader/dist/instances.js:184:41)
    at getEmit (/Users/dali/code/xxxx-sdk/node_modules/ts-loader/dist/index.js:192:37)
    at successLoader (/Users/dali/code/xxxx-sdk/node_modules/ts-loader/dist/index.js:34:11)
    at Object.loader (/Users/dali/code/xxxx-sdk/node_modules/ts-loader/dist/index.js:21:12)

如果修复了所有编译错误和ts文件正确编译为js文件。

这是我的代码:

index.ts:

import Customer from '../src/customer'

customer.ts:

class Customer{
    first_name: string;
    last_name: string;
    id: number;
    constructor(first_name: string, last_name: string, id: number) {
        this.first_name = first_name;
        this.last_name = last_name;
        this.id = id;
    }
}

导出默认客户;

package.js:
{
  "dependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-preset-env": "^1.7.0",
    "lodash": "^4.17.10",
    "npx": "^10.2.0",
    "request": "^2.87.0",
    "request-promise": "^4.2.2"
  },
  "devDependencies": {
    "@types/es6-promise": "^3.3.0",
    "babel-cli": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "ts-loader": "^4.3.0",
    "typescript": "^2.9.1",
    "webpack": "^4.10.1",
    "webpack-cli": "^2.1.4",
    "webpack-dev-server": "^3.1.4"
  },
  "name": "xxxx-sdk",
  "version": "1.0.0",
  "description": "Yusofleet SDK (c)",
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode=development",
    "start:dev": "webpack-dev-server",
    "watch": "webpack --watch & webpack-dev-server"
  },
  "author": "medali@xxxxt.com",
  "license": "ISC"
}

webpack config:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: './src/index.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  optimization: {
        // We no not want to minimize our code.
        minimize: false
    },
  node: {
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  },
  watch: true,
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }

    ]
  }
}

和tsconfig.json

{
    "compilerOptions": {
      "outDir": "./dist/",
      "module": "es6",
      "target": "es2015",
      "allowJs": true
    }
  }

1 个答案:

答案 0 :(得分:2)

错误是由于:tsconfig文件中的"target": "es2015"。 我最初添加此内容是为了支持Promise.resolve(); 解决方案是删除此目标并从bluebird导入Promise