typescript - Babel 7 + Inversify 4 + WebPack 4-@inject上的意外字符 '@'

标签 typescript webpack babeljs babel-loader inversifyjs

我有一个 typescript Vue SPA项目,在其中使用Inversify

我使用了awesome-typescript-loader来编译我的 typescript 源代码。现在我想切换到Babel,但是当我编译我的应用程序webpack时会出现此错误:

Module parse failed: Unexpected character '@' (38:25) You may need an
appropriate loader to handle this file type.
| _inherits(ReportService, _BaseService);
| > function ReportService(@inject(TYPES.Urls)
| urls) {
| var _this;
@ ./app/Ioc/container.ts 6:0-54 14:39-52
@ ./app/startup.ts



.babelrc
{
    "presets": [
        "@babel/env",
        "@babel/preset-typescript"
    ],
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/proposal-class-properties",
        "@babel/proposal-object-rest-spread"
    ]
}

tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "test/*": [ "test/*" ],
      "@/*": [ "app/*" ]
    },
    "lib": [ "es6", "dom" ], // es6 minimum required for Promise
    "target": "es6", // Required for vuetify
    "strict": true,
    "module": "esNext",
    "moduleResolution": "node",
    "experimentalDecorators": true, // Required for @Component for Vue
    "strictPropertyInitialization": false,
    "noImplicitAny": false,
    "allowUnusedLabels": false,
    "noEmit": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "node_modules"
  ]
}

webpack.config.js
const config = {
    entry: {
        app: './app/startup.ts'
    },
    output: {
        path: path.resolve(__dirname, "wwwroot", "dist"),
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.(html)$/,
                use: {
                    loader: 'html-loader'
                }
            },
            {
                test: /\.ts$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                enforce: "pre",
                test: /\.js$/,
                loader: "source-map-loader",
                exclude: [
                    path.join(process.cwd(), 'node_modules')
                ]
            },

        ]
    },
    resolve: {
        alias: {
            'vue': 'vue/dist/vue.esm.js',
            '@': path.join(__dirname, 'app')
        },
        extensions: ['.vue', '.ts', '.js']
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendors",
                    chunks: "all",
                    enforce: true,
                    priority: -10
                }
            }
        }
    }
}

如果我使用链接到babel-loader的awesome-typescript-loader
{
    test: /\.ts$/,
    exclude: /node_modules/,
    use: ['babel-loader', 'awesome-typescript-loader']
}

并删除不必要的babel插件并对其进行预设。

最佳答案

那是@babel/preset-typescript实现的一个非常烦人的问题:
它会剥离类型,并且不会在输出代码中发出相对的元数据。
唯一有用的是babel-plugin-transform-typescript-metadata

{
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
  ],
  "presets": [
    "@babel/preset-typescript"
  ]
}

关于typescript - Babel 7 + Inversify 4 + WebPack 4-@inject上的意外字符 '@',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52557878/

相关文章:

javascript - 使用 webpack 在另一个中包含一个 Angular 模板

javascript - typescript 类型检查机制

reactjs - 使用 SVG 作为 React 组件,并将图像链接作为 prop?

reactjs - babel/polyfill 在 create-react-app 中被弃用警告

webpack - 如何通过 Webpack 4 获得动态导入和 SplitChunksPlugin 的好处?

javascript - 如何正确导出Webpack库?

javascript - Typescript 嵌套此不调用定义的函数

typescript - 使用 TypeScript 在 mongoose.Schema 中的 `required` 字段中使用函数

reactjs - 在 Laravel 5.7 React 应用程序中,出现错误 : 'classProperties' isn't currently enabled

javascript - 语法错误: Unexpected token import - reactjs