Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to include babel polyfill using multiple entry points

I'm using a webpack config using multiple entry points:

var fs = require('fs'); var webpack = require('webpack'); var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.[hash].js');  module.exports = {     cache: true,     entry: {         main: './resources/assets/js/main.js',         services: './resources/assets/js/services.js'     },     output: {         path: './public/assets/web/js',         filename: '[name].[hash].js',         publicPath: '/assets/web/js/',         chunkFilename: '[id].[chunkhash].js'     },     resolve: {         modulesDirectories: ['node_modules', 'web_modules', 'modules']     },     module: {         loaders: [{             test: /\.js$/,             exclude: [/node_modules/, /web_modules/],             loader: 'babel-loader',             query: {                 stage: 0             }         }]     },     plugins: [commonsPlugin,         new webpack.ProvidePlugin({             jQuery: 'jquery',             $: 'jquery',             'window.jQuery': 'jquery'         }),         function() {             this.plugin('done', function(stats) {                 fs.writeFile('./cache.json', JSON.stringify({                     hash: stats.hash                 }));             });         }     ] }; 

Is there any way to include the babel polyfill in my webpack config. Or what is the best way to include it in my setup?

Do I have to include it as a require in all my modules?

Thank you very much in advance!

like image 600
Berto Yáñez Avatar asked Oct 27 '15 23:10

Berto Yáñez


People also ask

Where do I put babel polyfill?

babelrc then include @babel/polyfill at the top of the entry point to your application via require or import as discussed above. If useBuiltIns key is not specified or it is explicitly set with useBuiltIns: false in your . babelrc, add @babel/polyfill directly to the entry array in your webpack. config.

How does babel polyfill work?

Babel Polyfill adds support to the web browsers for features, which are not available. Babel compiles the code from recent ecma version to the one, which we want. It changes the syntax as per the preset, but cannot do anything for the objects or methods used.

Can Webpack have multiple entry points?

webpack.config.jsWe can also pass an array of file paths to the entry property which creates what is known as a "multi-main entry". This is useful when you would like to inject multiple dependent files together and graph their dependencies into one "chunk".


1 Answers

The easiest way would be to change

main: './resources/assets/js/main.js', services: './resources/assets/js/services.js' 

to be

main: ['babel-polyfill', './resources/assets/js/main.js'], services: ['./resources/assets/js/services.js'] 

so that the polyfill is loaded and executed as part of each entry point without your files needing to know about it.

This assumes that both main and services are loaded on the same page. If they are two separate pages, you'd want the babel-polyfill entry in both arrays.

Note

The above applies to Babel 5. For Babel 6, you'd npm install --save babel-polyfill and use babel-polyfill instead of babel/polyfill.

like image 131
loganfsmyth Avatar answered Sep 26 '22 14:09

loganfsmyth