-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
220 lines (213 loc) · 6.75 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { InjectManifest } = require('workbox-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
// const BrotliPlugin = require('brotli-webpack-plugin');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const config = {
// entry: {
// app: [
// 'react-hot-loader/patch',
// './app/index.js'
// ],
// vendor: ['react', 'react-dom', 'axios', 'redux', 'redux-thunk', 'prop-types']
// },
entry: [
// 'react-hot-loader/patch',
'./app/index.tsx',
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
publicPath: '/',
},
// Enable sourcemaps for debugging webpack's output.
devtool: 'source-map',
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'awesome-typescript-loader',
options: {
// skip typechecking for speed
transpileOnly: true,
},
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: 'pre', test: /\.(tsx | ts | js)$/, loader: 'source-map-loader' },
{ test: /\.(js)$/, exclude: /node_modules/, use: 'babel-loader' },
// { test: /\.module\.css$/, use: ['style-loader', 'css-loader'] },
// { test: /\.module\.css$/, use: ['style-loader', 'css-loader'] },
{// Use the MiniCssExtractPlugin to extract css file
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: '../',
hmr: process.env.NODE_ENV === 'development',
reloadAll: true,
},
},
'css-loader',
],
},
{ test: /\.(png|jpg|gif)$/, use: [{ loader: 'url-loader', options: { limit: 8192 } }] },
],
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
// externals: {
// react: 'React',
// 'react-dom': 'ReactDOM',
// },
devServer: {
historyApiFallback: true,
hot: true,
https: true,
},
optimization: {
splitChunks: {
name: false,
},
// splitChunks: {
// cacheGroups: {
// commons: {
// test: /[\\/]node_modules[\\/]/,
// name: 'vendors',
// chunks: 'all',
// },
// },
// },
},
resolve: {
alias: {
'react-dom': '@hot-loader/react-dom',
},
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: ['.ts', '.tsx', '.js', '.json'],
},
plugins: [
new HtmlWebpackPlugin({ template: 'app/index.html' }),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].css',
chunkFilename: '[id].css',
}),
// new webpack.optimize.CommonsChunkPlugin({ Deprecated
// name: 'vendor', // Specify the common bundle's name.
// minChunks: Infinity
// }),
new CopyWebpackPlugin([
// Copy directory contents to {output}/
{ from: 'app/pwa' },
]),
new InjectManifest({
swSrc: './app/service-worker.js',
swDest: './service-worker.js',
}),
// new BundleAnalyzerPlugin(), // Analyze the bundle file.
],
};
if (process.env.NODE_ENV && process.env.NODE_ENV.trim() === 'production') {
config.mode = 'production';
delete config.resolve.alias; // Remove the react hot loader dom
config.devtool = ''; // Remove the source map
config.plugins.push(
new CompressionPlugin({
filename: '[file]',
algorithm: 'gzip',
test: /\.ts|\.tsx|\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.7,
deleteOriginalAssets: false,
}),
);
// Due to Brotli compression is not supported by all browser, the gzip will be used at this moment.
// config.plugins.push(
// new BrotliPlugin({
// asset: '[path].br[query]',
// test: /\.ts|\.tsx|\.js$|\.css$|\.html$/,
// threshold: 10240,
// minRatio: 0.7,
// }),
// );
// config.module.rules[1] = ({ // In production model, replace the css rules in order to use ExtractTextPlugin. My css rules is in the position 2.
// test: /\.css$/,
// use: [
// {
// loader: MiniCssExtractPlugin.loader,
// options: {
// // you can specify a publicPath here
// // by default it uses publicPath in webpackOptions.output
// publicPath: '../',
// hmr: process.env.NODE_ENV === 'development',
// },
// },
// 'css-loader',
// ],
// });
// config.module.rules[1] = ({
// test: /\.css$/,
// use: [
// MiniCssExtractPlugin.loader,
// {
// loader: 'css-loader',
// options: {
// importLoaders: 1,
// sourceMap: true,
// modules: true,
// // localIdentName: '[local]___[hash:base64:5]',
// minimize: {
// safe: true,
// },
// },
// },
// ],
// exclude: /\.global\.css$/,
// });
// config.devtool = 'source-map';
// config.plugins.push(
// // new webpack.DefinePlugin({
// // 'process.env': {
// // NODE_ENV: JSON.stringify(process.env.NODE_ENV)
// // }
// // }),
// // new ExtractTextPlugin({ filename: '[name].css', allChunks: true }), // Extract css to one file.
// new MiniCssExtractPlugin({
// filename: '[name].css',
// }),
// new UglifyJsPlugin({
// uglifyOptions: {
// compress: {
// // remove warnings
// // warnings: false,
// sequences: true, // Join consecutive simple statements using the comma operator
// dead_code: true,
// conditionals: true, // Apply optimizations for if-s and conditional expressions.
// booleans: true,
// unused: true, // Drop unreferenced functions and variables.
// if_return: true,
// join_vars: true,
// drop_console: true,
// },
// },
// }),
// );
// config.optimization = {
// minimizer: [new UglifyJsPlugin()],
// };
}
module.exports = config;