@mongosh/cli-repl 2.1.4 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/webpack.config.js CHANGED
@@ -1,10 +1,23 @@
1
1
  'use strict';
2
+ const fs = require('fs');
3
+ const Module = require('module');
4
+ const crypto = require('crypto');
2
5
  const { merge } = require('webpack-merge');
3
6
  const path = require('path');
4
7
  const { WebpackDependenciesPlugin } = require('@mongodb-js/sbom-tools');
5
8
 
6
9
  const baseWebpackConfig = require('../../config/webpack.base.config');
7
10
 
11
+ // Builtins that the driver and/or devtools-connect refer to but which
12
+ // cannot be snapshotted yet
13
+ // https://github.com/nodejs/node/pull/50943 addresses some of this,
14
+ // we can try to remove at least child_process once we are using a
15
+ // Node.js version that supports it.
16
+ const lazyNodeBuiltins = ['http', 'https', 'tls', 'child_process'];
17
+ const eagerNodeBuiltins = Module.builtinModules.filter(
18
+ (m) => !lazyNodeBuiltins.includes(m)
19
+ );
20
+
8
21
  const webpackDependenciesPlugin = new WebpackDependenciesPlugin({
9
22
  outputFilename: path.resolve(
10
23
  __dirname,
@@ -30,6 +43,91 @@ const config = {
30
43
  },
31
44
  plugins: [webpackDependenciesPlugin],
32
45
  entry: './lib/run.js',
46
+ resolve: {
47
+ alias: {
48
+ // This is similar to https://github.com/babel/babel/issues/12442,
49
+ // @babel/code-frame loads chalk loads supports-color which checks
50
+ // for TTY color support during startup rather than at runtime
51
+ '@babel/code-frame': makeLazyForwardModule('@babel/code-frame'),
52
+ // express is used by oidc-plugin but is a) quite heavy and rarely used
53
+ // b) uses http, which is not a supported module for snapshots at this point.
54
+ express: makeLazyForwardModule('express'),
55
+ 'openid-client': makeLazyForwardModule('openid-client'),
56
+ ...Object.fromEntries(
57
+ lazyNodeBuiltins.map((m) => [m, makeLazyForwardModule(m)])
58
+ ),
59
+ ...Object.fromEntries(
60
+ lazyNodeBuiltins.map((m) => [`node:${m}`, makeLazyForwardModule(m)])
61
+ ),
62
+ },
63
+ },
64
+
65
+ externals: {
66
+ electron: 'commonjs2 electron', // optional dep of the OIDC plugin
67
+ ...Object.fromEntries(eagerNodeBuiltins.map((m) => [m, `commonjs2 ${m}`])),
68
+ ...Object.fromEntries(
69
+ Module.builtinModules.map((m) => [`node:${m}`, `commonjs2 ${m}`])
70
+ ), // node: builtin specifiers need to be always declared as externals in webpack right now
71
+ },
72
+
73
+ externalsPresets: {
74
+ node: false,
75
+ },
33
76
  };
34
77
 
35
78
  module.exports = merge(baseWebpackConfig, config);
79
+
80
+ // Helper to create a module that lazily loads the actual target package
81
+ // when it is being encountered. This is useful for snapshotting, where some
82
+ // packages either cannot be snapshotted or perform initialization during
83
+ // startup that should depend on runtime state.
84
+ function makeLazyForwardModule(pkg) {
85
+ const S = JSON.stringify;
86
+ const tmpdir = path.resolve(__dirname, '..', 'tmp', 'lazy-webpack-modules');
87
+ fs.mkdirSync(tmpdir, { recursive: true });
88
+ const filename = path.join(
89
+ tmpdir,
90
+ crypto.createHash('sha256').update(pkg).digest('hex').slice(0, 16) + '.js'
91
+ );
92
+
93
+ const realRequire = Module.isBuiltin(pkg)
94
+ ? `__non_webpack_require__(${S(pkg)})`
95
+ : `require(${S(require.resolve(pkg))})`;
96
+
97
+ const moduleContents = require(pkg);
98
+ let source = `'use strict';\nlet _cache;\n`;
99
+ source += `function orig() {\n_cache = ${realRequire}; orig = () => _cache; return _cache;\n}\n`;
100
+ if (typeof moduleContents === 'function') {
101
+ source += `module.exports = function(...args) { return orig().apply(this, args); };\n`;
102
+ } else {
103
+ source += `module.exports = {};\n`;
104
+ }
105
+ let i = 0;
106
+ for (const key of Object.keys(moduleContents)) {
107
+ if (typeof moduleContents[key] === 'function') {
108
+ source += `module.exports[${S(
109
+ key
110
+ )}] = function(...args) { return orig()[${S(
111
+ key
112
+ )}].apply(this, args); };\n`;
113
+ } else {
114
+ source += `let value_${i}, value_${i}_set = false;\n`;
115
+ source += `Object.defineProperty(module.exports, ${S(key)}, {
116
+ enumerable: true, configurable: true,
117
+ get() {
118
+ if (value_${i}_set) return value_${i};
119
+ value_${i} = orig()[${S(key)}];
120
+ value_${i}_set = true;
121
+ return value_${i};
122
+ }, set(v) {
123
+ value_${i}_set = true;
124
+ value_${i} = v;
125
+ }
126
+ })\n`;
127
+ i++;
128
+ }
129
+ }
130
+
131
+ fs.writeFileSync(filename, source);
132
+ return filename;
133
+ }