Babel 7 + Inversify 4 + WebPack 4 - @inject处出现意外字符'@'

11

我有一个使用TypeScript编写的Vue SPA项目,其中使用了Inversify

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

模块解析失败:意外字符“@”(38:25) 你可能需要适当的加载器来处理这种文件类型。| _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
                }
            }
        }
    }
}

如果我使用awesome-typescript-loader链接到babel-loader

{
    test: /\.ts$/,
    exclude: /node_modules/,
    use: ['babel-loader', 'awesome-typescript-loader']
}

去除不必要的 Babel 插件和预设,它就能正常工作。


1
类、方法和属性装饰器是否有效?如果是,也许 Babel 还不支持在函数参数上转译装饰器,但我不确定。 - kingdaro
1
是的,装饰器可以使用。谢谢。 - Max
2
我遇到了同样的问题,我通过添加额外的插件“babel-plugin-transform-function-parameter-decorators”来解决它,但在我的情况下,这只部分地解决了问题,因为即使转译成功,当使用构造函数注入时,我仍然会收到“缺少必需的inject或multiInject注释:参数0”。 - antanas_sepikas
1个回答

12
这是一个非常令人烦恼的问题,涉及到@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"
  ]
}

我花了一整天的时间尝试让inversify工作。你的答案救了我。 - Abdou Ouahib
@AbdouOuahib 没关系。是的,那确实是一个相当绝望的情况 :) - am0wa
谢谢!我整天都在玩弄Babel设置,这个简单的更改让一切都完美地运作了! - corinnaerin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接