使用 webpack 编译打字稿时出错 [英] Error at typescript compilation with webpack

查看:30
本文介绍了使用 webpack 编译打字稿时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译 ts 文件时遇到以下错误(运行 webpack 命令).这段代码先是用es6写的,然后转成typescript.

I am facing the following error while compiling ts files (run webpack command). This code was first written in es6 and then converted to 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 文件.

If fixed all the compilation errors and ts files compiles correctly to js files.

这是我的代码:

索引.ts:

import Customer from '../src/customer'

customer.ts:

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 配置:

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

and tsconfig.json

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

推荐答案

错误是由于:"target": "es2015" in tsconfig file.我最初添加这个是为了能够支持 Promise.resolve();解决方案是删除此目标并从 bluebird 导入 Promise.

Error was due to : "target": "es2015" in tsconfig file. I originally added this to be able to support Promise.resolve(); The solution was to remove this target and import Promise from bluebird.

这篇关于使用 webpack 编译打字稿时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆