webpack入门第六节:使用babel-loader转换ES6(上)

使用babel-loader转换ES6(上)

其实这一节和上节非常相似,test是用正则去匹配以js结尾的文件,loader是选择处理方式,后面的注释写明了参数的用途

var htmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/components/app.js',
    output: {
        path: __dirname + '/dist',
        filename: 'js/[name].bundle.js',
    },
    module:{
        loaders:[
            {
                test:/.\js$/,
                loader:'babel',
                //loader时不包括node_modules
                exclude:'./node_modules',
                //loader src目录下的所有文件
                include:'./src/',
                //制定loader的参数
                query:{
                    presests:['latest']
                }
            }
        ]
    },
    plugins: [
        new htmlWebpackPlugin({
            filename:'index.html',
            template:'index.html',//引用的H5模板
            inject:'body'
        })
    ]
}

当然babel也可以在写package.json文件中

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "webpack": "webpack --config webpack.config.js --progress --display-modules --colors --display-reasons"
  },
  也可以写在这里
  "babel":{
    "presets":["latest"]
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^1.13.2"
  }
}

最后打包的结果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
<script type="text/javascript" src="js/main.bundle.js"></script></body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42450794/article/details/81749550