T
T
tex6202022-01-20 13:16:09
JavaScript
tex620, 2022-01-20 13:16:09

How to transform js code for old version of node.js?

I am using the following webpack config.
Babel preset set to node.js 6.10 compatibility mode

const path = require('path');
module.exports = {
    entry: './src/index.js',
    module: {
        rules: [
            {
                test: /\.(js)$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env', {
                            targets: {
                                node: '6.10'
                            }
                        }]
                    }
                }
            }
        ]
    },
    output: {<code lang="javascript">

</code>
        path: path.resolve(__dirname, 'dist'),
        filename: 'index_bundle.js'
    },
    mode: 'production'//process.env.NODE_ENV === 'production' ? 'production' : 'development'
}


Source:

const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo'));
const promises = [promise1, promise2];

Promise.allSettled(promises).
then((results) => results.forEach((result) => console.log(result.status)));


After transformation:
(()=>{var e=[Promise.resolve(3),new Promise((function(e,o){return setTimeout(o,100,"foo")}))];Promise.allSettled(e).then((function(e){return e.forEach((function(e){return console.log(e.status)}))}))})();

However, after the transformation, the Promise.allSettled method remained, which is not in node version 6.10.
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2022-01-21
@Aetae

In theory, preset-env should automatically include a polyfill for this case from core.js.
Here they say that yes it should be in the modern version.

'@babel/preset-env', {
    useBuiltIns: 'usage',
    corejs: {version: 3, proposals: true},
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question