generated from JoviDeCroock/product-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
150 lines (144 loc) · 3.97 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
const path = require('path');
const webpack = require('webpack');
const glob = require('glob');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const babelConfig = require('./.babelrc');
const PreactRefreshPlugin = require('@prefresh/webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PurgecssPlugin = require('purgecss-webpack-plugin');
const env = babelConfig.env;
const modernTerser = new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {
compress: {
keep_infinity: true,
pure_getters: true,
passes: 10,
},
output: {
// By default, Terser wraps function arguments in extra parens to trigger eager parsing.
wrap_func_args: false,
},
ecma: 8,
safari10: true,
toplevel: true,
},
});
const makeConfig = (mode, localDev) => {
const { NODE_ENV } = process.env;
const isProduction = NODE_ENV === 'production';
const isServer = mode === 'server';
// Build plugins
const plugins = [
localDev &&
new HtmlWebpackPlugin({ inject: true, template: './index.html' }),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
}),
new MiniCssExtractPlugin({
filename: '[name].[contenthash:5].css',
}),
isProduction &&
new PurgecssPlugin({
paths: glob.sync(path.join(__dirname, 'src') + '/**/*', {
nodir: true,
}),
}),
].filter(Boolean);
if (!isProduction) {
plugins.push(new PreactRefreshPlugin());
plugins.push(new webpack.HotModuleReplacementPlugin());
}
// Return configuration
return {
mode: isProduction ? 'production' : 'development',
entry: {
main: isServer ? './src/prerender.js' : './src/index.js',
},
context: path.resolve(__dirname, './'),
stats: 'normal',
devtool: isProduction ? '' : 'eval-source-map',
target: isServer ? 'node' : 'web',
devServer: {
contentBase: path.join(__dirname, 'dist'),
host: 'localhost',
port: 3000,
historyApiFallback: true,
hot: true,
inline: true,
publicPath: '/',
clientLogLevel: 'none',
open: true,
overlay: true,
},
output: {
chunkFilename: isServer ? undefined : `[name]-[contenthash].js`,
filename: isProduction
? isServer
? 'index.js'
: `[name]-[contenthash].js`
: `[name].js`,
path: isServer
? path.resolve(__dirname, 'dist', 'server')
: path.resolve(__dirname, 'dist'),
publicPath: '/',
libraryTarget: isServer ? 'commonjs2' : undefined,
},
optimization: {
minimize: !isServer,
minimizer: [modernTerser],
},
plugins,
resolve: {
mainFields: ['module', 'main', 'browser'],
extensions: ['.mjs', '.js', '.jsx'],
alias: {
preact: path.resolve(__dirname, 'node_modules', 'preact'),
url: 'native-url',
},
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true,
fallback: 'style-loader',
},
},
{ loader: 'css-loader', options: { esModule: true } },
],
},
{
test: /\.(png|jpe?g|gif|woff)$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},
{
// Makes our babel-loader the lord and savior over our TypeScript
test: /\.js$|\.jsx$/,
include: [path.resolve(__dirname, 'src')],
loader: 'babel-loader',
options: {
cacheDirectory: true,
...env[mode],
},
},
],
},
};
};
module.exports =
process.env.NODE_ENV === 'production'
? [makeConfig('modern'), makeConfig('server')]
: makeConfig('modern', true);