由于现在的浏览器还不支持ES6的新语法,所以当我们在使用ES6语法时就要使用babel来进行预编译。
下面将详细介绍如何在webpack中引入babel-loader。
安装:
npm install --save-dev babel-loader babel-core babel-preset-es2015
创建.babelrc文件,其内容如下:
{
"presets": ["es2015"]
}
webpack.config.js配置如下:
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loader: 'babel'}
]
}
其中exclude指定不需要babel预编译的文件夹。