-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
204 lines (194 loc) · 6.16 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
const path = require('path'),
webpack = require('webpack'),
miniCssExtractPlugin = require('mini-css-extract-plugin'),
{ BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'),
compressionPlugin = require('compression-webpack-plugin'),
htmlWebpackPlugin = require('html-webpack-plugin'),
optimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'),
terserJSPlugin = require('terser-webpack-plugin'),
nodeExternals = require('webpack-node-externals'),
distDir = path.resolve(__dirname, 'dist'),
srcDir = path.resolve(__dirname, 'src'),
extractPlugin = new miniCssExtractPlugin({
filename: 'styles.[contenthash].css', //'styles.css',
chunkFilename: 'styles-[hash].css',
ignoreOrder: false
}),
environmentVariables = new webpack.DefinePlugin({
'process.env.PORT': JSON.stringify(`${process.env.PORT}`),
}),
analyzeBundle = new BundleAnalyzerPlugin({
analyzerMode: "static",
}),
gzip = new compressionPlugin(),
htmlGenerator = new htmlWebpackPlugin({
template: 'src/index.html',
scriptLoading: 'defer'
}),
cssMiniMizer = new optimizeCSSAssetsPlugin({}),
jsMinimizer = new terserJSPlugin({});
// WebpackShellPlugin = require('webpack-shell-plugin'),
/**
* This plugin helps run npm command on build completion.
* The plugin was placed in the client config section as
* the client build runs last, ensuring all files
* needed by the script will be in place.
*/
// shellPlugin = new WebpackShellPlugin({
// onBuildEnd: ['npm start']
// });
/**
* custom plugin that moves all webpage asset tags from head to body tags.
*/
class AssetTagsToBody {
apply (compiler) {
compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
// Static Plugin interface |compilation |HOOK NAME | register listener
const hooks = htmlWebpackPlugin.getHooks(compilation);
hooks.alterAssetTagGroups.tapAsync(
'AssetTagsToBody', // <-- Set a meaningful name here for stacktraces
(data, cb) => {
// Manipulate the content
if (data && data.headTags.length > 0) {
data.headTags.forEach((tag, _) => {
const attr = tag.attributes;
(attr && attr.href && attr.href.endsWith('.css')) ?
data.bodyTags.unshift(tag)
:
data.bodyTags.push(tag);
});
data.headTags = [];
}
// Tell webpack to move on
cb(null, data)
}
)
})
}
}
/**
* The build config had to be splitted into two as one targets web (client),
* and the other node (server) to avoid various bundling errors.
*/
// Configuaration file for server side (Express and Socket.io)
const serverConfig = {
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".js", ".ts", ".json"]
},
entry: {
server: `${srcDir}/server.ts`
},
output: {
path: `${distDir}/server`,
filename: '[name].js'
},
target: 'node',
// This helps to solve an issue where __dirname returns '/'
// node installation path. This helps to set it to source path
node: {
__dirname: true
},
// this helps to avoid bundling node(server) dependencies like express
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.ts$/,
include: srcDir,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader'
}
]
},
// {
// enforce: "pre",
// test: /\.js?$/,
// loader: "source-map-loader",
// exclude: /node_modules/
// },
]
}
};
// configuration entry for client side (React)
const clientConfig = {
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".js", ".ts", ".tsx", ".json"]
},
entry: {
client: `${srcDir}/client/index.tsx`,
},
output: {
path: `${distDir}/client`,
filename: '[name].[contenthash].js'
},
target: 'web',
module: {
rules: [
{
test: /\.ts(x?)$/,
include: srcDir,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
}
},
]
},
{
test: /\.css$/,
use: [
{
loader: miniCssExtractPlugin.loader
},
'css-loader'
]
}
]
},
optimization: {
minimizer: [jsMinimizer, cssMiniMizer]
},
plugins: [extractPlugin, environmentVariables, analyzeBundle, gzip, htmlGenerator, new AssetTagsToBody()] // shellPlugin
};
const testConfig = {
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".js", ".ts", ".json"]
},
entry: {
server: `${srcDir}/tests/server/server.spec.ts`
},
output: {
path: `${distDir}/tests`,
filename: '[name].spec.js'
},
target: 'node',
// This helps to solve an issue where __dirname returns '/'
// node installation path. This helps to set it to source path
node: {
__dirname: true
},
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.ts$/,
include: srcDir,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader'
}
]
}
]
}
};
module.exports = [serverConfig, clientConfig, testConfig];