-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
191 lines (168 loc) · 6.51 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
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const BrotliPlugin = require('brotli-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
// We'll refer to our source and dist paths frequently, so let's store them here
const PATH_SOURCE = path.join(__dirname, './src');
const PATH_DIST = path.join(__dirname, './dist');
// If we export a function, it will be passed two parameters, the first
// of which is the webpack command line environment option `--env`.
// `webpack --env.production` sets env.production = true
// `webpack --env.a = b` sets env.a = 'b'
// https://webpack.js.org/configuration/configuration-types#exporting-a-function
module.exports = (env) => {
const environment = env.ENVIRONMENT;
const isProduction = environment === 'production';
console.log('WEBPACK ENV:', environment);
return {
// Tell Webpack to do some optimizations for our environment (development
// or production). Webpack will enable certain plugins and set
// `process.env.NODE_ENV` according to the environment we specify.
// https://webpack.js.org/configuration/mode
mode: environment,
// Configuration options for Webpack DevServer, an Express web server that
// aids with development. It provides live reloading out of the box and can
// be configured to do a lot more.
devServer: {
// The dev server will serve content from this directory.
contentBase: [PATH_DIST, path.join(__dirname, 'static')],
publicPath: '/',
hot: true,
// Specify a host. (Defaults to 'localhost'.)
host: 'localhost',
// Specify a port number on which to listen for requests.
port: 8080,
// When using the HTML5 History API (you'll probably do this with React
// later), index.html should be served in place of 404 responses.
historyApiFallback: true,
// Show a full-screen overlay in the browser when there are compiler
// errors or warnings.
overlay: {
errors: true,
warnings: true,
},
open: true,
},
devtool: !isProduction ? 'inline-source-map' : '',
optimization: {
usedExports: true,
// splitChunks: {
// cacheGroups: {
// commons: {
// test: /[\\/]node_modules[\\/]/,
// name: 'vendors',
// chunks: 'all',
// },
// },
// },
},
// The point or points to enter the application. This is where Webpack will
// start. We generally have one entry point per HTML page. For single-page
// applications, this means one entry point. For traditional multi-page apps,
// we may have multiple entry points.
// https://webpack.js.org/concepts#entry
entry: [path.join(PATH_SOURCE, './index.js')],
// Tell Webpack where to emit the bundles it creates and how to name them.
// https://webpack.js.org/concepts#output
// https://webpack.js.org/configuration/output#output-filename
output: {
path: PATH_DIST,
filename: 'js/[name].[hash].js',
publicPath: '/',
},
// Determine how the different types of modules will be treated.
// https://webpack.js.org/configuration/module
// https://webpack.js.org/concepts#loaders
module: {
rules: [
{
test: /\.css$/,
loader: ['style-loader', 'css-loader'],
},
{
test: /\.s[ac]ss$/i,
use: ['style-loader', 'css-loader', 'sass-loader'],
},
{
test: /\.(png|jp(e*)g|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[hash]-[name].[ext]',
},
},
],
},
{
test: /\.(otf|eot|ttf|woff)/,
loader: 'url-loader?limit=8192',
},
{
test: /\.js$/, // Apply this rule to files ending in .js
exclude: /node_modules/, // Don't apply to files residing in node_modules
use: {
// Use the following loader and options
loader: 'babel-loader',
// We can pass options to both babel-loader and Babel. This option object
// will replace babel.config.js
options: {
presets: [
[
'@babel/preset-env',
{
debug: true, // Output the targets/plugins used when compiling
// Configure how @babel/preset-env handles polyfills from core-js.
// https://babeljs.io/docs/en/babel-preset-env
useBuiltIns: 'usage',
// Specify the core-js version. Must match the version in package.json
corejs: 3,
// Specify which environments we support/target for our project.
// (We have chosen to specify targets in .browserslistrc, so there
// is no need to do it here.)
// targets: "",
},
],
// The react preset includes several plugins that are required to write
// a React app. For example, it transforms JSX:
// <div> -> React.createElement('div')
'@babel/preset-react',
],
},
},
},
],
},
plugins: [
new BundleAnalyzerPlugin(),
new webpack.DefinePlugin({
'process.env.CS_FORM': JSON.stringify(env.CS_FORM),
'process.env.ENVIRONMENT': JSON.stringify(env.ENVIRONMENT),
}),
new CopyPlugin({
patterns: [{ from: './static', to: './static' }],
}),
isProduction
? new BrotliPlugin({
asset: '[path].br[query]',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
})
: () => {},
// This plugin will generate an HTML5 file that imports all our Webpack
// bundles using <script> tags. The file will be placed in `output.path`.
// https://github.com/jantimon/html-webpack-plugin
new HtmlWebpackPlugin({
template: path.join(PATH_SOURCE, './index.html'),
}),
// This plugin will delete all files inside `output.path` (the dist directory),
// but the directory itself will be kept.
// https://github.com/johnagan/clean-webpack-plugin
new CleanWebpackPlugin(),
],
};
};