-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
148 lines (134 loc) · 4.56 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
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const isDev = process.env.NODE_ENV === 'development';
const webpackConfig = {
module: {
rules: [
{
test: /\.tsx?$/,
// Being explicit about what files to include can vastly improve performance
// so that webpack knows when to even bother checking a file
include: [path.join(__dirname, 'client', 'scripts'), path.join(__dirname, 'common')],
use: {
loader: 'tslint-loader',
options: {
// Disabling typeCheck gives a bump in build performance, but makes certain rules
// unable to run. Opting for more linting rather than saving a bit of build time
typeCheck: true,
tsConfigFile: path.join(__dirname, 'tsconfig.webpack.json')
}
},
enforce: 'pre'
},
{
test: /\.css$/,
include: [path.join(__dirname, 'client')],
use: [
// Style-loader is required for css hot-reloading, but we need the performance boost
// of extracting to a single CSS file in production
isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: isDev ? '[name]__[local]' : '[name]__[local]___[chunkhash]',
minimize: !isDev,
sourceMap: isDev
}
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: [
// Don't need to include cssnano here, because that's done automatically by css-loader above
require('stylelint'),
// So useful so I don't have to worry about browser-compatibility as much!
require('autoprefixer')
]
}
}
]
},
{
test: /\.tsx?$/,
include: [path.join(__dirname, 'client', 'scripts'), path.join(__dirname, 'common')],
use: {
loader: 'ts-loader',
options: {
configFile: "tsconfig.webpack.json",
compilerOptions: {
sourceMap: isDev
}
}
}
},
{
test: /\.(png|svg|jpg|gif)$/,
include: [path.join(__dirname, 'client', 'images')],
loader: 'file-loader'
}
]
},
entry: ['./client/scripts/index.tsx'],
output: {
// Hashing the name takes a non-trivial amount of time, so we don't bother in development mode
// since Chrome dev tools disables caching anyway
filename: isDev ? '[name].js' : '[name].[chunkhash].min.js',
chunkFilename: isDev ? '[name].js' : '[name].[chunkhash].min.js',
path: path.join(__dirname, 'build', 'public')
},
// Setting this does lots of lovely things for us by default
// https://webpack.js.org/concepts/mode/
mode: process.env.NODE_ENV,
target: 'web',
// Don't want to scope just to the client directory, because the common directory is used by
// both the client & server code
context: __dirname,
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.css']
},
devtool: isDev && 'inline-source-map',
plugins: [
// Yay auto-injecting generated assets into the HTML!
new HtmlWebpackPlugin({
title: 'TicTacToe Dungeon',
inject: 'body',
template: path.join(__dirname, 'client', 'index.ejs')
})
],
optimization: {
// Enable automatic chunk splitting. By default webpack will split out all vendor
// code into its own bundle. This will improve client performance, because vendor packages
// change much less frequently than application code, so the browser can cache the vendor
// packages rather than downloading them again every time the code changes
splitChunks: {
chunks: 'all'
}
},
performance: {
// The default max size is only 244kb... which we reach just by including react
maxAssetSize: 1000000,
maxEntrypointSize: 1000000
}
};
if (!isDev) {
module.exports = merge(webpackConfig, {
plugins: [
new CleanWebpackPlugin(['public'], {
root: path.join(__dirname, 'build'),
verbose: true
}),
new MiniCssExtractPlugin({
filename: '[name].[chunkhash].min.css',
})
]
});
} else {
module.exports = webpackConfig;
}