Skip to content

Commit 91616ca

Browse files
committed
feat(browser-runtime): fix require with webpack
1 parent ef4989b commit 91616ca

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/browser-runtime-electron/src/electron-runtime.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/camelcase */
12
import {
23
ElectronInterpreterEnvironment
34
} from './electron-interpreter-environment';
@@ -11,15 +12,33 @@ import {
1112

1213
import { ServiceProvider } from '@mongosh/service-provider-core';
1314

15+
declare const __webpack_require__: any;
16+
declare const __non_webpack_require__: any;
17+
1418
export class ElectronRuntime implements Runtime {
1519
private openContextRuntime: OpenContextRuntime;
1620

1721
constructor(serviceProvider: ServiceProvider, messageBus?: {
1822
emit: (eventName: string, ...args: any[]) => void;
1923
}) {
24+
// NOTE:
25+
//
26+
// This is necessary for client code bundling this library with
27+
// webpack.
28+
//
29+
// Webpack will replace require with its own implementation, and that would
30+
// not necessarily have access to the node modules available in node
31+
// (depends on the target configuration).
32+
//
33+
// IMPORTANT: as it cannot be easily tested be aware of this bug before
34+
// changing this line: https://github.com/webpack/webpack/issues/5939 (it
35+
// seems that checking for `typeof __non_webpack_require__` does not work).
36+
//
37+
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
38+
2039
this.openContextRuntime = new OpenContextRuntime(
2140
serviceProvider,
22-
new ElectronInterpreterEnvironment({ require }),
41+
new ElectronInterpreterEnvironment({ require: requireFunc }),
2342
messageBus
2443
);
2544
}

0 commit comments

Comments
 (0)