forked from zhaoky/flqin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
99 lines (97 loc) · 2.69 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const path = require('path');
const WebpackBar = require('webpackbar');
const WebpackStylish = require('webpack-stylish');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const _DEV_ = process.env.NODE_ENV === 'development';
const _SEO_ = process.env.BUILD_TYPE === 'seo';
const config = {
mode: process.env.NODE_ENV || 'production',
entry: {
main: path.resolve(__dirname, `./src/js/${_SEO_ ? 'index-seo.js' : 'index.js'}`)
},
output: {
path: path.resolve(__dirname, './dist'),
filename: _DEV_ ? 'bundle.js' : '[name].[chunkhash:8].js',
publicPath: _DEV_ ? '/' : './'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader?cacheDirectory']
},
{
test: /\.(c|le)ss$/,
use: [MiniCssExtractPlugin.loader, `css-loader?sourceMap=${_DEV_}`, 'postcss-loader', 'less-loader']
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
name: 'assets/[name].[ext]'
}
}
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: `src/template/${_SEO_ ? 'index-seo.html' : 'index.html'}`,
favicon: 'src/assets/favicon.png'
}),
new MiniCssExtractPlugin({
filename: _DEV_ ? 'css/[name].css' : '[name].[contenthash:8].css',
chunkFilename: _DEV_ ? 'css/[name].css' : '[name].[contenthash:8].css'
}),
new WebpackStylish(),
new WebpackBar()
],
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendors: {
priority: 1,
name: 'vendors',
chunks: 'all',
test: /[\\/]node_modules[\\/]/
}
}
}
}
};
if (_DEV_) {
config.devtool = 'eval-source-map';
config.devServer = {
host: '0.0.0.0',
port: 8080,
stats: 'errors-only',
overlay: true
};
} else {
config.stats = 'none';
config.plugins.push(
new OptimizeCssAssetsPlugin(),
new ScriptExtHtmlWebpackPlugin({
inline: [/runtime\.[a-z0-9]{8}\.js$/],
preload: {
chunks: 'initial',
test: [/vendors\.[a-z0-9]{8}\.js$/, /main\.[a-z0-9]{8}\.js$/]
}
})
);
}
if (process.env.MODE === 'analysis') {
config.plugins.push(new BundleAnalyzerPlugin());
}
module.exports = config;