-
Notifications
You must be signed in to change notification settings - Fork 60
/
webpack.config.js
216 lines (195 loc) · 6.57 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
205
206
207
208
209
210
211
212
213
214
215
216
// This is the webpack which builds our extension in the 'dist' folder.
const path = require('path');
const webpack = require('webpack');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
const { merge } = require('webpack-merge');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const { WebpackDependenciesPlugin } = require('@mongodb-js/sbom-tools');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = (env, argv) => {
const outputPath = path.join(__dirname, 'dist');
const webpackDependenciesPlugin = new WebpackDependenciesPlugin({
outputFilename: path.resolve(__dirname, '.sbom', 'dependencies.json'),
});
// here we use a function instead of a constant to make sure that if we want to share references
// to some object (ie. a plugin) we are doing it intentionally.
const baseConfig = () => ({
devtool: 'source-map',
resolve: {
alias: {
// Removes `browserslist` that is pulled in by `babel` and is unnecessary
// as well as being a particularly large dependency.
browserslist: false,
// Removes `ampersand-sync`: `ampersand-sync` is required by `ampersand-model` which is temporarily
// still used by some files inside `mongodb-data-service` (although not needed for the VSCode extension).
//
// `ampersand-sync` brings into the bundle a number of other dependencies that are outdated and having
// known vulnerabilities.
//
// This alias can be removed once `mongodb-data-service` will not include `mongodb-connection-model` anymore.
'ampersand-sync': false,
// Removes `electron`: is an optional dependency of `oidc-plugin`, but also installed as dev-dep,
// webpack would bring it inside the bundle otherwise.
electron: false,
'hadron-ipc': false,
// We don't currently support kerberos in our extension.
kerberos: false,
// Optional native-addon dependencies of ssh2
'cpu-features': false,
'./crypto/build/Release/sshcrypto.node': false,
},
},
externals: {
// The vscode-module is created on-the-fly and must be excluded.
vscode: 'vscode',
// Currently connection-model has a keytar dependency, vscode provides its
// own keytar dependency. Here we are telling it to use vscode's keytar.
keytar: 'keytar',
'@electron/remote': '@electron/remote',
// MongoDB node driver externals:
snappy: 'snappy',
'snappy/package.json': 'snappy/package.json',
'bson-ext': 'bson-ext',
'win-export-certificate-and-key': 'win-export-certificate-and-key',
'os-dns-native': 'os-dns-native',
'mongodb-client-encryption': 'mongodb-client-encryption',
'@mongodb-js/zstd': '@mongodb-js/zstd',
'gcp-metadata': 'gcp-metadata',
encoding: 'encoding',
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
output: { ascii_only: true },
// Not keeping classnames breaks shell-api during minification
keep_classnames: true,
compress: {
// The 'bindings' package relies on `error.stack;` having side effects.
pure_getters: false,
},
},
}),
],
},
plugins: [
webpackDependenciesPlugin,
...(argv.analyze
? [
new DuplicatePackageCheckerPlugin(),
new BundleAnalyzerPlugin({
analyzerPort: 'auto',
}),
]
: []),
],
output: {
strictModuleErrorHandling: true,
strictModuleExceptionHandling: true,
path: outputPath,
filename: '[name].js',
library: {
type: 'commonjs',
},
devtoolModuleFilenameTemplate: '../[resource-path]',
},
});
const nodeTargetConfig = () =>
merge(baseConfig(), {
target: 'node',
resolve: {
extensions: ['.js', '.ts', '.json'],
},
module: {
rules: [
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {},
},
{
test: /\.node$/,
loader: 'node-loader',
},
],
},
});
const extensionConfig = merge(nodeTargetConfig(), {
experiments: {
// required for `bson`
topLevelAwait: true,
},
entry: {
extension: './src/extension.ts',
},
});
const languageServerConfig = merge(nodeTargetConfig(), {
entry: {
languageServer: './src/language/server.ts',
},
});
const languageServerWorkerConfig = merge(nodeTargetConfig(), {
entry: {
languageServerWorker: './src/language/worker.ts',
},
});
const webviewConfig = merge(baseConfig(), {
target: 'web',
entry: {
webviewApp: './src/views/webview-app/index.tsx',
},
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
// This is here to deal with some node.js code brought in by
// @leafygreen/logo via @emotion/server:
fallback: {
stream: require.resolve('stream-browserify'),
buffer: require.resolve('buffer'),
crypto: require.resolve('crypto-browserify'),
path: require.resolve('path-browserify'),
mongodb: false,
},
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {},
},
],
},
plugins: [
// This plugin has been added to avoid Out of memory crashes of webpack on
// our Github runners. It does so by moving the type checking to a
// separate process.
new ForkTsCheckerWebpackPlugin(),
// This is here to deal with some node.js code brought in
// by @leafygreen/logo via @emotion/server:
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process/browser',
}),
],
watchOptions: {
// For some systems, watching many files can result in a lot of CPU or memory usage
// https://webpack.js.org/configuration/watch/#watchoptionsignored
// don't use this pattern, if you have a monorepo with linked packages.
ignored: /node_modules/,
},
});
return [
extensionConfig,
languageServerConfig,
languageServerWorkerConfig,
webviewConfig,
];
};