【问题标题】:Module parse failed: *.ts Unexpected character '@'模块解析失败:*.ts 意外字符“@”
【发布时间】:2017-09-17 02:20:57
【问题描述】:

我无法让 webpack 捆绑我的脚本。我从 app.module.ts 上的这一行得到一个错误:@NgModule({

我收到消息:您可能需要适当的加载程序来处理此文件类型。

这是我的 webpack 配置:

var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: ["./src/ts/main.ts","./src/ts/css.ts"],
  module: {
      rules: [
        {
          test: /\.tsx$/,
          loaders: [
            {
              loader: 'awesome-typescript-loader',
              options: { configFileName: 'tsconfig.json' }
            } , 'angular2-template-loader'
          ]
        },
        {
          test: /\.css$/,
          use: ExtractTextPlugin.extract({use: 'css-loader'})
        },
        {
          test: /\.(jpe?g|png|gif|svg)$/i,
          use: ['url-loader?limit=10000', 'img-loader']
        },
        {
          test: /\.(eot|woff2?|ttf)$/i,
          use: 'url-loader'
        }
      ]
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"]
  },
  plugins: [
    new ExtractTextPlugin("public/styles.css"),
  ],
  output: {
    filename: "public/bundle.js"
  }
}

这里是 tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "rootDir": "src/ts"
  },
  "angularCompilerOptions": {
    "genDir": "public/dist/js"
  },
  "exclude": [
    "node_modules",
    "**/*-aot.ts"
  ],
  "filesGlob": [
    "src/ts/**/*.ts"
  ]
}

通过main.ts到达app.module.ts。

【问题讨论】:

    标签: angular typescript webpack


    【解决方案1】:

    你的文件显然有一个 ts 扩展名,但是你的 webpack 配置中的规则只匹配扩展名 tsx。将模式 /\.tsx$\ 更改为 /\.tsx?$\ 以处理这两个扩展。

        {
          test: /\.tsx?$/,
          loaders: [
            {
              loader: 'awesome-typescript-loader',
              options: { configFileName: 'tsconfig.json' }
            } , 'angular2-template-loader'
          ]
        }
    

    【讨论】:

    • 解决了。我在另一个 ts 文件上遇到另一个错误,“描述”:TS2304:找不到名称“描述”。我之前能够让 tsc 参与这个项目,所以我不确定我错过了什么。
    • 看起来 jasmine 库丢失或未正确导入。这是一个完全不同的错误,您能否关闭此问题,并针对describe 问题发布一个适当的问题,以防您找不到解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 2017-04-04
    • 2019-07-18
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多