We will use three webpack configuration files. This way can make project maintenance easier and more flexible.
- webpack.common.js <-- Put the common settings.
- webpack.dev.js <-- Only put the develop period settings
- webpack.prod.js <-- Only Put the release production settings.
Step 1. Setup webpack.common.js
- webpack.common.js
webpack.common.js
/* eslint-disable import/no-extraneous-dependencies */ <-- If you use ESLint, add this.
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
webpack.common.js
module.exports = {
...
...
...
entry: {
app: [
'babel-polyfill', <-- This setting is used for JS promise that make it can run on older browser.
'./src/Main.js',
],
},
...
...
...
}
webpack.common.js
module.exports = {
...
...
...
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
},
...
...
...
}
webpack.common.js
module.exports = {
...
...
...
resolve: {
extensions: ['.js', '.vue', '.json', '.css'],
alias: {
vue$: 'vue/dist/vue.esm.js', <-- This setting is must for Vue2.
'@': path.resolve(__dirname, 'src'), <-- This let us can used "import foo from '@/foo/foo';" instead of "import foo from '../../foo/foo';"
},
},
...
...
...
}
webpack.common.js
module.exports = {
...
...
...
module: {
rules: [
{
enforce: 'pre',
test: /\.(vue|js)$/,
include: [path.resolve('src')],
exclude: [path.resolve('node_modules')],
loader: 'eslint-loader',
},
{
test: /\.vue$/,
include: [path.resolve('src')],
exclude: [path.resolve('node_modules')],
loader: 'vue-loader',
},
{
test: /\.js$/,
include: [path.resolve('src')],
exclude: [path.resolve('node_modules')],
loader: 'babel-loader',
},
{
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)(\?.+)?$/,
include: [path.resolve('src')],
exclude: [path.resolve('node_modules')],
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
},
},
],
},
],
},
...
...
...
}
webpack.common.js
module.exports = {
...
...
...
target: 'web',
watchOptions: {
ignored: /node_modules/,
aggregateTimeout: 300,
poll: 1000,
},
...
...
...
}
webpack.common.js
module.exports = {
...
...
...
plugins: [
new VueLoaderPlugin(),
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Johnny Chu Demo',
template: 'index.html',
}),
],
...
...
...
}
Step 2. Setup webpack.dev.js
- webpack.dev.js
webpack.dev.js
/* eslint-disable import/no-extraneous-dependencies */ <-- If you use ESLint, add this.
const path = require('path');
const merge = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const common = require('./webpack.common.js');
webpack.dev.js
module.exports = merge(common, {
...
...
...
mode: 'development',
devtool: '#eval-source-map',
devServer: {
compress: true,
contentBase: 'dist',
historyApiFallback: true,
host: '0.0.0.0',
hot: true,
port: 9487,
},
...
...
...
}
webpack.dev.js
module.exports = merge(common, {
...
...
...
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
include: [path.resolve('src')],
exclude: [path.resolve('node_modules')],
use: [
'vue-style-loader',
'css-loader',
],
},
],
},
...
...
...
}
webpack.dev.js
module.exports = merge(common, {
...
...
...
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
}),
],
...
...
...
}
Step 3. Setup webpack.prod.js
- webpack.prod.js
webpack.prod.js
/* eslint-disable import/no-extraneous-dependencies */ <-- If you use ESLint, add this.
const path = require('path');
const merge = require('webpack-merge');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const common = require('./webpack.common.js');
webpack.prod.js
module.exports = merge(common, {
...
...
...
mode: 'production',
devtool: '#source-map',
...
...
...
}
webpack.prod.js
module.exports = merge(common, {
...
...
...
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true,
}),
new OptimizeCSSAssetsPlugin({}),
],
},
...
...
...
}
webpack.prod.js
module.exports = merge(common, {
...
...
...
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[chunkhash].js',
chunkFilename: '[id].[chunkhash].js',
},
...
...
...
}
webpack.prod.js
module.exports = merge(common, {
...
...
...
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
include: [path.resolve('src')],
exclude: [path.resolve('node_modules')],
use: [
MiniCssExtractPlugin.loader,
'css-loader',
],
},
],
},
...
...
...
}
webpack.prod.js
module.exports = merge(common, {
...
...
...
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[hash].css',
chunkFilename: '[id].[hash].css',
}),
],
...
...
...
}