Webpack 5 "dependOn" and target: "es5" appear to be incompatible
Asked Answered
T

1

5

I'm having trouble getting Webpack 5 to output es5 compatible code while also using the "dependOn" entry parameter.

I'm using Babel to transpile my code, which works fine, but unless I set the webpack target as "es5", webpack itself outputs incompatible code.

I'm using the entry parameter "dependOn", which behaves as expected with target: "web", but as soon as I change that to "es5" I get "Error in main.build.js from Terser" and "Unexpected token: punc(:) [main.build.js:3,9]".

Removing the "dependOn" parameter allows it to compile, but then I get my vendor libraries added to each entry.

Here is a minimal webpack config that reproduces the issue (commenting out either target: "es5" or dependOn: "vendor" fixes it):

const path = require('path');

module.exports = {
    mode: "production",
    target: "es5",

    entry: {
        vendor: "./src/test.js",
        main: {
            dependOn: "vendor",
            import: ['./src/main.js']
        }
    },
    output: {
        filename: '[name].build.js',
        path: path.resolve(__dirname, 'dist')
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    }
};

"test.js" and "main.js" can contain anything, the build still fails if they just have a "console.log" statement in each.

My package devDependencies contains the following: "@babel/core": "^7.14.3", "@babel/preset-env": "^7.14.2", "babel-loader": "^8.2.2", "webpack": "^5.37.1", "webpack-cli": "^4.7.0".

And my .babelrc contains {"presets": ["@babel/preset-env"]}

Twelvetone answered 24/5, 2021 at 11:17 Comment(0)
T
7

This can be resolved by changing

target: "es5"

To

target: ["web", "es5"]

The need for both targets isn't explicitly mentioned in the relevant documentation (https://webpack.js.org/configuration/target/), so hopefully this will help anyone with the same problem.

Twelvetone answered 26/5, 2021 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.