-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.base.js
222 lines (207 loc) · 8.35 KB
/
webpack.config.base.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* eslint-disable */
const os = require('os');
const path = require('path');
const arp = require('app-root-path');
const webpack = require("webpack");
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin').TsconfigPathsPlugin;
const ManifestPlugin = require('webpack-manifest-plugin').WebpackManifestPlugin;
module.exports = (_env, options, returnConfigObject) => {
console.log("operating in mode", options.mode);
///////////////////////////////////
// PROVIDE UTILS
const isDevelopment = Boolean(options.mode === "development");
const cacheLoaderSettings = (cacheName) => {
return {
loader: 'cache-loader',
options: {
cacheDirectory: path.resolve(arp.path, options.cacheDir, cacheName)
}
};
};
const threadLoaderSettings = () => {
return {
loader: 'thread-loader',
options: {
// there should be 1 cpu for the fork-ts-checker-webpack-plugin
workers: Math.max(Math.floor((os.cpus().length) / 2), 1),
poolRespawn: false,
poolTimeout: options.watch ? Infinity : 1000 // set this to Infinity in watch mode - see https://github.com/webpack-contrib/thread-loader
}
};
};
///////////////////////////////////
// CONFIGURE BUILD
const settings = {
output: {
filename: "[name].js"
},
context: path.resolve(arp.path, "out"),
devtool: isDevelopment ? 'inline-source-map' : '', // use cheap-eval-source-map when sourcemaps are broken
resolve: {
extensions: [".ts", ".tsx", ".js", ".njk", ".less"],
alias: {
//less path resolve. "~" is replaced by less-loader
"static": path.resolve(arp.path, "source", "app", "client")
},
// Add `.ts` and `.tsx` as a resolvable extension.
plugins: [
new TsconfigPathsPlugin({
configFile: path.resolve(arp.path, options.tsConfigPath),
extensions: [".ts", ".tsx", ".js"]
})
]
},
plugins: [
new webpack.ExtendedAPIPlugin(),
new webpack.DefinePlugin({
ENVIRONMENTAL_ROUTES_PATH: JSON.stringify(path.resolve(arp.path, options.scriptDir, "routes")),
ENVIRONMENTAL_MODELS_PATH: JSON.stringify(path.resolve(arp.path, options.scriptDir, "models")),
ENVIRONMENTAL_INTERFACES_PATH: JSON.stringify(path.resolve(arp.path, options.scriptDir, "interfaces"))
}),
new CleanWebpackPlugin({
protectWebpackAssets: true,
cleanOnceBeforeBuildPatterns: options.cleanupPatterns ? options.cleanupPatterns.concat(["!*.md"]) : ["!*.md"],
cleanStaleWebpackAssets: false
}),
new ManifestPlugin({
fileName: options.manifestFileName || "chunkManifest.json",
sort: (a, b) => {
if (a < b) return 1;
else if (a > b) return -1;
else return 0;
},
generate: (_seed, _fileDescriptor, entryPoints) => {
return entryPoints;
}
}),
new ForkTsCheckerWebpackPlugin({
async: true,
typescript: {
enabled: true,
configFile: path.resolve(arp.path, options.tsConfigPath),
diagnosticOptions: {
syntactic: true
},
profile: true
}
}),
new FilterWarningsPlugin({
exclude: [
/Critical dependency: the request of a dependency is an expression/
]
})
],
module: {
rules: [{
test: /\.tsx?$/,
use: [cacheLoaderSettings("typescript"), threadLoaderSettings(), {
loader: 'babel-loader',
options: {
plugins: [
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-optional-chaining"
],
sourceMap: 'inline'
}
}, {
loader: 'ts-loader',
options: {
happyPackMode: true, // IMPORTANT! use happyPackMode mode to speed-up compilation and reduce errors reported to webpack
transpileOnly: true,
experimentalWatchApi: options.watch === true,
allowTsInNodeModules: false
}
}]
}, {
test: /locales/, // TODO: Test could be more specific
loader: '@alienfast/i18next-loader',
// options here
query: { basenameAsNamespace: true }
}, {
test: /\.(njk|nunjucks)$/,
use: [cacheLoaderSettings("templates"), threadLoaderSettings(), {
loader: 'renewed-nunjucks-loader',
options: {
sourceMap: 'inline',
config: path.resolve(arp.path, "nunjucks.config.js"),
quiet: true
}
}]
}]
},
optimization: {
noEmitOnErrors: true,
removeAvailableModules: !isDevelopment,
removeEmptyChunks: !isDevelopment,
minimize: !isDevelopment,
minimizer: [new TerserPlugin({
extractComments: false,
terserOptions: {
compress: true,
keep_classnames: true,
keep_fnames: true,
sourceMap: false,
output: {
ecma: 2015,
comments: false,
beautify: false,
quote_style: 3
}
}
})]
}
};
///////////////////////////////////
// EXTEND BUILD PLUGINS
if (!isDevelopment) settings.plugins = settings.plugins.concat([new BundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: true,
generateStatsFile: true,
statsFilename: path.resolve(arp.path, "var", "webpack", "stats", options.analyzerFileName),
reportFilename: path.resolve(arp.path, "var", "webpack", "reports", options.analyzerFileName.split(".").map((pathPart, index, array) => {
if (index === array.length - 1) return "html";
return pathPart;
}).join("."))
})]);
///////////////////////////////////
// EXTEND OPTIMIZATION OPTIONS
if (!isDevelopment) {
settings.optimization.splitChunks = {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendor",
chunks: "all"
},
templates: {
test: /\.njk/,
name: "templates",
enforce: true,
chunks: "initial"
},
lib: {
test: /[\\/]lib[\\/]/,
name: "lib",
chunks: "initial",
reuseExistingChunk: true
},
styles: {
test: /\.less/,
name: "styles",
enforce: true,
chunks: "initial"
}
}
};
}
///////////////////////////////////
// EXTEND WATCH OPTIONS
if (options.watch) settings.watchOptions = { ignored: ["node_modules", "var/**/*"] };
const webpackConfigObject = { settings, cacheLoaderSettings, threadLoaderSettings };
return returnConfigObject ? webpackConfigObject : settings;
};