/
webpack.client.js
155 lines (147 loc) · 4.52 KB
/
webpack.client.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* eslint-disable max-len */
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const AssetsPlugin = require('assets-webpack-plugin');
const ExtractCssChunks = require('extract-css-chunks-webpack-plugin');
const workboxPlugin = require('workbox-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const __PWA_ENV__ = process.env.PWA_ENV;
const __PWA_PUBLIC_PATH__ = process.env.PWA_PUBLIC_PATH;
const isProd = process.env.NODE_ENV === 'production';
const cssModuleNames = '[name]__[local]___[hash:base64:5]';
const cssLoaderConfig = [
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: `${cssModuleNames}`,
discardComments: true,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
outputStyle: 'compressed',
sourceMap: true,
},
},
];
module.exports = {
cache: !isProd,
entry: {
main: './client/index.js',
vendor: ['./client/vendor/js/index.js', './client/vendor/css/index.css'],
},
output: {
path: path.resolve('./build/client'),
publicPath: __PWA_PUBLIC_PATH__,
filename: isProd ? 'js/[name].[chunkhash:8].js' : 'js/[name].js',
chunkFilename: isProd ? 'js/[name].[chunkhash:8].js' : 'js/[name].js',
},
resolve: {
alias: {
react: 'preact-compat',
'react-dom': 'preact-compat',
},
},
module: {
rules: isProd ? [
{ test: /\.js$/, exclude: /node_modules/, use: ['babel-loader'] },
{
test: /\.(css|scss)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: cssLoaderConfig,
}),
},
{ test: /\.(gif|png|jpe?g|svg|ico)$/i, use: [{ loader: 'file-loader', options: { name: 'images/[name].[hash:8].[ext]' } }] },
{ test: /\.(woff(2)?|ttf|otf|eot)(\?[a-z0-9=&.]+)?$/, use: [{ loader: 'url-loader', options: { limit: 1000, name: 'fonts/[name].[hash:8].[ext]' } }] },
] : [
{ test: /\.js$/, exclude: /node_modules/, use: [{ loader: 'babel-loader', options: { cacheDirectory: 'babel_cache' } }] },
{
test: /\.(css|scss)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: cssLoaderConfig,
}),
},
{ test: /\.(gif|png|jpe?g|svg|ico)$/i, use: [{ loader: 'file-loader', options: { name: 'images/[name].[ext]' } }] },
{ test: /\.(woff(2)?|ttf|otf|eot)(\?[a-z0-9=&.]+)?$/, use: [{ loader: 'url-loader', options: { limit: 1000, name: 'fonts/[name].[ext]' } }] },
],
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new CleanWebpackPlugin(['./build/client']),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.DefinePlugin({
'process.env.NODE_ENV': isProd ? '"production"' : '"development"',
__BROWSER__: true,
__PWA_ENV__: JSON.stringify(__PWA_ENV__),
__LOCAL__: __PWA_ENV__ === 'local',
}),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'webpackManifest'],
minChunks: Infinity,
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'main',
children: true,
minChunks: 2,
}),
new AssetsPlugin({
filename: 'assetsManifest.json',
path: path.resolve('./build/client'),
prettyPrint: true,
}),
new ExtractTextPlugin({
filename: 'css/[name].[contenthash:8].css',
allChunks: true,
}),
...(isProd ? [
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
}),
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
screw_ie8: true,
warnings: false,
},
mangle: {
screw_ie8: true,
},
output: {
comments: false,
screw_ie8: true,
},
}),
new ExtractCssChunks('css/[name].[contenthash:8].css'),
] : [
new webpack.NamedModulesPlugin(),
]),
// new workboxPlugin({
// globDirectory: 'build/client',
// globPatterns: ['**/*.{html,js,css,png,jpeg}'],
// swDest: './build/client/sw.js',
// }),
],
devtool: isProd ? 'hidden-source-map' : 'inline-source-map',
devServer: {
contentBase: path.resolve('./build/client'),
headers: { 'Access-Control-Allow-Origin': '*' },
overlay: true,
quiet: true,
},
};