Webpack 4 + Babel 7 打造 ES6+ 前端环境。
- 指定浏览器兼容 Babel 编译 ES6+ 代码。
- CSS 自动加前缀、压缩、代码文件分离。
- 热模块更新。
git clone https://github.com/VincentTV/ES6.git
cd ES6
npm install
npm run start
开始 ES6 编程。
npm run build
ES6
│
├───dist
│ ├───assets
│ │ └───avatar-78c62249.png
│ ├───css
│ │ └───main.css
│ ├───index.html
│ └───main.bundle.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
module.exports = {
entry: "./src/main.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: "../",
hmr: true
}
},
{
loader: "css-loader",
options: { importLoaders: 1 }
},
{
loader: "postcss-loader",
options: {
plugins: [require("autoprefixer"), require("cssnano")]
}
}
]
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: "url-loader",
options: {
limit: 8192,
name: "assets/[name]-[hash:8].[ext]"
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: "src/index.html",
filename: "index.html"
}),
new MiniCssExtractPlugin({
filename: "css/[name].css"
}),
new webpack.HotModuleReplacementPlugin()
]
};
配置过程及详解,参考个人博客 Webpack4 + Babel7 打造 ES6+ 前端环境 | Vincent F0ng。