-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
167 lines (143 loc) · 5.02 KB
/
webpack.config.js
File metadata and controls
167 lines (143 loc) · 5.02 KB
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
const path = require('path'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
MiniCssExtractPlugin = require('mini-css-extract-plugin'),
CleanWebpackPlugin = require('clean-webpack-plugin'),
UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const devMode = process.env.NODE_ENV !== 'production';
const styleLoaders = (config = { withCssModules: false }) => {
const afterCssLoader = [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
];
const beforeCssLoader = [
postCSSLoader,
'sass-loader'
];
return [
...afterCssLoader,
cssLoader(config.withCssModules, beforeCssLoader.length),
...beforeCssLoader
];
};
// https://medium.freecodecamp.org/how-i-integrated-css-modules-with-scss-into-my-react-application-32f473e1bb51
const cssLoader = (withCssModules = false, importLoaders = 0) => {
let options = {
modules: false,
sourceMap: true,
minimize: true,
importLoaders
};
if (withCssModules) {
options = {
...options,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]',
}
}
return {
loader: 'css-loader',
options
};
};
const postCSSLoader = {
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: true,
plugins: () => [
require('autoprefixer')({
'browsers': ['> 1%', 'last 2 versions']
}),
require('cssnano')({
preset: 'default',
})
],
}
};
module.exports = {
entry: './src/index.tsx',
output: {
// https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/#placeholders
filename: 'bundle.[chunkhash].js',
path: path.resolve(__dirname, 'dist'),
},
// Enable sourcemaps for debugging webpack's output.
devtool: 'source-map',
devServer: {
// Parse host and port from env to allow customization.
//
// If you use Docker, Vagrant or Cloud9, set
// host: options.host || "0.0.0.0";
//
// 0.0.0.0 is available to all network devices
// unlike default `localhost`.
host: process.env.HOST, // Defaults to `localhost`
port: process.env.PORT, // Defaults to 8080
open: true, // Open the page in browser
overlay: true // provide an overlay for capturing compilation related warnings and errors
},
// Add '.ts', '.tsx', '.js', and '.json' as resolvable extensions.
resolve: { extensions: ['.ts', '.tsx', '.js', '.json'] },
// https://webpack.js.org/plugins/mini-css-extract-plugin/#extracting-all-css-in-a-single-file
optimization: {
minimizer: [
// https://webpack.js.org/configuration/optimization/
new UglifyJsPlugin({
// https://github.com/webpack-contrib/uglifyjs-webpack-plugin#sourcemap
sourceMap: true
})
],
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true
}
}
}
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: 'awesome-typescript-loader' },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' },
// All files with a '.scss', '.sass' or '.css' extension will be handled by 'css-loader & sass-loader'.
{
test: /\.(sa|sc|c)ss$/,
exclude: /\.module\.(sa|sc|c)ss$/,
use: styleLoaders()
},
{
test: /\.module\.(sa|sc|c)ss$/,
use: styleLoaders({ withCssModules: true })
},
{
test: /\.(png|svg|jpg|gif)$/,
loader: 'file-loader'
}
]
},
plugins: [
// to clean the ./dist & ./coverage folder before regenerating files.
new CleanWebpackPlugin([
'dist',
'coverage'
], {}),
// reloads webpack after file changes
new HtmlWebpackPlugin({ template: path.resolve(__dirname, 'index.html') }),
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
})
],
// 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'
}
};