Babel 7 + Inversify 4 + WebPack 4 - อักขระที่ไม่คาดคิด '@' บน @inject

ฉันมีโปรเจ็กต์ typescript Vue SPA ที่ฉันใช้ Inversify

ฉันใช้ awesome-typescript-loader เพื่อรวบรวมซอร์สโค้ด typescript ของฉัน ตอนนี้ฉันต้องการเปลี่ยนไปใช้ Babel แต่เมื่อฉันคอมไพล์แอปพลิเคชัน webpack ทำให้เกิดข้อผิดพลาดนี้:

การแยกวิเคราะห์โมดูลล้มเหลว: อักขระที่ไม่คาดคิด '@' (38:25) คุณอาจต้องใช้
ตัวโหลดที่เหมาะสมเพื่อจัดการกับไฟล์ประเภทนี้
| _สืบทอด(ReportService, _BaseService);
| > ฟังก์ชั่น 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 ที่ไม่จำเป็นออกและตั้งค่าล่วงหน้าให้ใช้งานได้


person Max    schedule 28.09.2018    source แหล่งที่มา
comment
ผู้ตกแต่งคลาส วิธีการ และคุณสมบัติใช้งานได้หรือไม่? ถ้าเป็นเช่นนั้นบางที Babel อาจไม่สนับสนุนเครื่องมือตกแต่งแบบ transpiling ในอาร์กิวเมนต์ของฟังก์ชัน แต่ฉันไม่แน่ใจ   -  person kingdaro    schedule 28.09.2018
comment
ใช่ครับ ช่างตกแต่งทำงาน ขอบคุณ   -  person Max    schedule 28.09.2018
comment
ฉันกำลังประสบปัญหาเดียวกัน ซึ่งฉันแก้ไขได้โดยการเพิ่มปลั๊กอิน babel-plugin-transform-function-parameter-decorators เพิ่มเติม อย่างไรก็ตาม ในกรณีของฉัน วิธีนี้แก้ไขปัญหาได้เพียงบางส่วนเท่านั้น เพราะตอนนี้แม้ว่าการแปลงข้อมูลจะสำเร็จ แต่ฉันกลับพลาดการฉีดที่จำเป็นหรือ multiInject คำอธิบายประกอบใน: อาร์กิวเมนต์ 0 เมื่อใช้การฉีดคอนสตรัคเตอร์   -  person antanas_sepikas    schedule 03.01.2019


คำตอบ (1)


นั่นเป็นปัญหาที่น่ารำคาญมากในการใช้งาน @babel/preset-typescript:
โดยจะแยกประเภทออกและไม่ปล่อยข้อมูลเมตาที่เกี่ยวข้องในโค้ดเอาต์พุต

สิ่งเดียวที่ช่วยได้คือ babel-plugin-transform- typescript-ข้อมูลเมตา

{
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
  ],
  "presets": [
    "@babel/preset-typescript"
  ]
}
person am0wa    schedule 02.12.2020
comment
ฉันใช้เวลาทั้งวันพยายามทำงานแบบกลับด้าน คำตอบของคุณช่วยฉันไว้ - person Abdou Ouahib; 22.01.2021
comment
@AbdouOuahib ยินดีต้อนรับ ใช่ มันเป็นกรณีที่ค่อนข้างสิ้นหวัง :) - person am0wa; 27.01.2021