1
+ /* eslint-disable @typescript-eslint/camelcase */
1
2
import {
2
3
ElectronInterpreterEnvironment
3
4
} from './electron-interpreter-environment' ;
@@ -11,15 +12,33 @@ import {
11
12
12
13
import { ServiceProvider } from '@mongosh/service-provider-core' ;
13
14
15
+ declare const __webpack_require__ : any ;
16
+ declare const __non_webpack_require__ : any ;
17
+
14
18
export class ElectronRuntime implements Runtime {
15
19
private openContextRuntime : OpenContextRuntime ;
16
20
17
21
constructor ( serviceProvider : ServiceProvider , messageBus ?: {
18
22
emit : ( eventName : string , ...args : any [ ] ) => void ;
19
23
} ) {
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
+
20
39
this . openContextRuntime = new OpenContextRuntime (
21
40
serviceProvider ,
22
- new ElectronInterpreterEnvironment ( { require } ) ,
41
+ new ElectronInterpreterEnvironment ( { require : requireFunc } ) ,
23
42
messageBus
24
43
) ;
25
44
}
0 commit comments