Webpack with Babel

Source: Internet
Author: User
Tags unsupported

I. INTRODUCTION of Babel

①babel is a JavaScript compiler that can turn ES6 syntax into a browser-compatible ES5 syntax

②babel Chinese official website: https://www.babeljs.cn/

③babel can be used alone, but is generally used in conjunction with Webpack


second, the use of Babel in Webpack

1, Babel-loader Babel-core babel-preset-env (conversion syntax)

① Installation dependencies:

// has been installed in the project Webpack case --save-dev babel-loader babel-core babel-preset-env

② Configuration

//the file is actually going to be executed in the node environment.Const PATH = require (' path ') Const Htmlwebpackplugin= Require (' Html-webpack-plugin ')//to export an object with a special property configurationModule.exports ={entry:'./src/main.js ',/*Portal File Module path*/output:{path:path.join (__dirname,'./dist/'),/*Export File module belongs to the directory, must be an absolute path*/FileName:' Bundle.js '/*packaged result file name*/}, devserver:{//Configuring the WWW directory for webpack-dev-serverContentbase: './dist '}, plugins:[//The plugin can pack the index.html into the directory where the Bundle.js file belongs, followed by the bundle.        //The script reference link is also automatically injected into the index.html, and the referenced resource name is also dependent on the packaged file name        Newhtmlwebpackplugin ({Template:'./index.html '})], module:{rules:[{test:/.css$/, use:[//Note: The order here is important, don't mess up the order' Style-loader ',                    ' Css-loader ']}, {test:/. (jpg|png|gif|svg) $/, use:[' File-loader ']}, {test: /\.js$/, exclude:/(node_modules|bower_components)/,//Exclude Node_module Directoryuse:{Loader: ' Babel-loader ', options:{     presets:[' env '] //transcoding rules                    }                } }        ]    }}           

③ Packaging

Run Build

2. Babel-polyfill to provide unsupported APIs in low-version browsers

① Installation Dependencies

--save-dev Babel-polyfill

② configuration: This will provide a stepping pad for compatibility with an unsupported API in a low-version browser (a method compatible with a low-version browser) when packaging

//the file is actually going to be executed in the node environment.Const PATH = require (' path ') Const Htmlwebpackplugin= Require (' Html-webpack-plugin ')//to export an object with a special property configurationModule.exports ={entry:[' Babel-polyfill ','./src/main.js '],/*Portal File Module path*/output:{path:path.join (__dirname,'./dist/'),/*Export File module belongs to the directory, must be an absolute path*/FileName:' Bundle.js '/*packaged result file name*/}, devserver:{//Configuring the WWW directory for webpack-dev-serverContentbase: './dist '}, plugins:[//The plugin can pack the index.html into the directory where the Bundle.js file belongs, followed by the bundle.        //The script reference link is also automatically injected into the index.html, and the referenced resource name is also dependent on the packaged file name        Newhtmlwebpackplugin ({Template:'./index.html '})], module:{rules:[{test:/.css$/, use:[//Note: The order here is important, don't mess up the order' Style-loader ',                    ' Css-loader ']}, {test:/. (jpg|png|gif|svg) $/, use:[' File-loader ']}, {test:/\.js$/, exclude:/(node_modules|bower_components)/,//Exclude Node_module Directoryuse:{Loader:' Babel-loader ', options:{presets:[' Env ']//transcoding rules                    }                }            }        ]    }}

③ Packaging

Run Build

3, Transform-runtime solve the code duplication problem

① in the packaging process, Babel will provide some tool functions in the package, and these tool functions may appear repeatedly in multiple modules.

② This causes the packaging to be too bulky, so Babel provides the babel-transform-runtime to solve the problem of too large a volume

③ Installation Dependencies

babel-plugin-transform-runtime--save-  DevBabel  -runtime--save

④ Configuration

//the file is actually going to be executed in the node environment.Const PATH = require (' path ') Const Htmlwebpackplugin= Require (' Html-webpack-plugin ')//to export an object with a special property configurationModule.exports ={entry:[' Babel-polyfill ', './src/main.js '],/*Portal File Module path*/output:{path:path.join (__dirname,'./dist/'),/*Export File module belongs to the directory, must be an absolute path*/FileName:' Bundle.js '/*packaged result file name*/}, devserver:{//Configuring the WWW directory for webpack-dev-serverContentbase: './dist '}, plugins:[//The plugin can pack the index.html into the directory where the Bundle.js file belongs, followed by the bundle.        //The script reference link is also automatically injected into the index.html, and the referenced resource name is also dependent on the packaged file name        Newhtmlwebpackplugin ({Template:'./index.html '})], module:{rules:[{test:/.css$/, use:[//Note: The order here is important, don't mess up the order' Style-loader ',                    ' Css-loader ']}, {test:/. (jpg|png|gif|svg) $/, use:[' File-loader ']}, {test:/\.js$/, exclude:/(node_modules|bower_components)/,//Exclude Node_module Directoryuse:{Loader:' Babel-loader ', options:{presets:[' Env '],//transcoding rules                        plugins:[' Transform-runtime ']                    }                }            }        ]    }}

Webpack with Babel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.