This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
forked from mikro-orm/nestjs-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
130 lines (113 loc) · 3.82 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
const path = require('path');
const { EnvironmentPlugin, IgnorePlugin } = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
// Mark our dev dependencies as externals so they don't get included in the webpack bundle.
const { devDependencies } = require('./package.json');
const externals = {};
for (const devDependency of Object.keys(devDependencies)) {
externals[devDependency] = `commonjs ${devDependency}`;
}
// And anything MikroORM's packaging can be ignored if it's not on disk.
// Later we check these dynamically and tell webpack to ignore the ones we don't have.
const optionalModules = new Set([
...Object.keys(require('knex/package.json').browser),
...Object.keys(require('@mikro-orm/core/package.json').peerDependencies),
...Object.keys(require('@mikro-orm/core/package.json').devDependencies || {}),
]);
module.exports = {
entry: path.resolve('src', 'main.ts'),
// You can toggle development mode on to better see what's going on in the webpack bundle,
// but for anything that is getting deployed, you should use 'production'.
// mode: 'development',
mode: 'production',
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
// We want to minify the bundle, but don't want Terser to change the names of our entity
// classes. This can be controlled in a more granular way if needed, (see
// https://terser.org/docs/api-reference.html#mangle-options) but the safest default
// config is that we simply disable mangling altogether but allow minification to proceed.
mangle: false,
// Similarly, Terser's compression may at its own discretion change function and class names.
// While it only rarely does so, it's safest to also disable changing their names here.
// This can be controlled in a more granular way if needed (see
// https://terser.org/docs/api-reference.html#compress-options).
compress: {
keep_classnames: true,
keep_fnames: true,
},
},
}),
],
},
target: 'node',
module: {
rules: [
// Bring in our typescript files.
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
// Native modules can be bundled as well.
{
test: /\.node$/,
use: 'node-loader',
},
// Some of MikroORM's dependencies use mjs files, so let's set them up here.
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
],
},
// These are computed above.
externals,
resolve: {
extensions: ['.ts', '.js'],
},
plugins: [
// Ignore any of our optional modules if they aren't installed. This ignores database drivers
// that we aren't using for example.
new EnvironmentPlugin({ WEBPACK: true }),
new IgnorePlugin({
checkResource: (resource) => {
const baseResource = resource
.split('/', resource[0] === '@' ? 2 : 1)
.join('/');
if (optionalModules.has(baseResource)) {
try {
require.resolve(resource);
return false;
} catch {
return true;
}
}
const lazyImports = [
'@nestjs/microservices',
'@nestjs/websockets/socket-module',
'cache-manager',
'@nestjs/microservices/microservices-module',
'class-transformer/storage',
'fastify-swagger',
];
if (!lazyImports.includes(resource)) {
return false;
}
try {
require.resolve(resource);
} catch (err) {
return true;
}
return false;
},
}),
],
output: {
filename: 'server.js',
libraryTarget: 'commonjs',
path: path.resolve(__dirname, 'output'),
},
};