4

I use webpack to bundle my codes for my clients. According to my question here Is it possible to break away from await Promise.all when any promise has fulfilled (Chrome 80) I want to use Promise.any() but Promise.any is only available for Chrome85+ and my clients need me to support Chrome 80+. Naturally I had thought with babel I can polyfill Promise.any(), e.g. corejs provides Promise.any. But I don't know what I did wrong, now with Babel 7 polyfill I can't even make Promise.any work for Chrome85+!

Following is what I have done,

First, my webpack.config.js

module.exports = {
    entry: {
        index: './src/index.js'
        ui:'./src/ui/index.js'
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js',
        libraryTarget: 'umd',
        libraryExport: 'default',
        umdNamedDefine: true,
        library: '[name]'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
   ...
}

I initially used Babel 6. With Babel 6 I can verify the code (using Promise.any) bundled by webpack works for Chrome 85+ (probably it didn't polyfill Promise.any at all).

I then upgrade Babel 6 to Babel 7, using npx babel-upgrade --writeand with some twists (e.g. "useBuiltIns": 'usage'). webpack can bundle my code then, but to my surprise the bundled code with Promise.any() can't even work on Chrome85+. I got the error message "Uncaught (in promise) rejected"

The following is my babelrc

{
    "env": {
        "production": {
            "presets": [
                [
                    "@babel/preset-env",
                    {
                        "targets": {
                            "browsers": [
                                "last 4 Chrome versions",
                                "last 3 Firefox versions",
                            ]
                        },
                        "modules": "commonjs",
                        "debug": true,
                        "useBuiltIns": 'usage'
                    }
                ]
            ],
            "plugins": [
                [
                    "@babel/plugin-transform-runtime",
                    {
                        "corejs": 2,
                        "regenerator": true
                    }
                ]
            ]
        }
    }
}

What did I do wrong ?

Qiulang
  • 10,295
  • 11
  • 80
  • 129

1 Answers1

3

I opened an issue against core-js and the author told me I should not use core-js@2. With that pointer I finally made it work with core-js@3!

Following is my .babelrc. The main point is to set corejs3 either in "presets" or in "@babel/plugin-transform-runtime" , but not both. Making babel work is so hard that I list my .babelrc here and I am not sure I have set everything right.

"production": {
            "presets": [
                [
                    "@babel/preset-env",
                    {
                        "targets": { ... },
                        "modules": "commonjs",
                        "useBuiltIns": "usage",
                        "corejs": {
                            "version": 3, 
                            "proposals": true
                        }
                    }
                ]
            ],
            "plugins": [
                [
                    "@babel/plugin-transform-runtime",
                    {
                        "regenerator": true
                        //or don't useBuiltIns but here
                        // "corejs": {
                        //     "version": 3,
                        //     "proposals": true
                        // }
                    }
                ]
            ]
        }

With this setting I can see the webpack output like and polyfill finally works!

Added following core-js polyfills:
  esnext.aggregate-error { "chrome":"78", "ie":"10", "safari":"13.1" }
  esnext.promise.any { "chrome":"78", "ie":"10", "safari":"13.1" }
  ...
Qiulang
  • 10,295
  • 11
  • 80
  • 129