@@ -11,6 +11,51 @@ if (ipcMsgListener) {
11
11
process . on ( "message" , ipcMsgListener ) ;
12
12
}
13
13
14
+ /**
15
+ * Attempt to require a module from a list of native packages
16
+ * that could be expected to exist in the unpacked version of
17
+ * VS Code. The list may grow in the future.
18
+ */
19
+ const requireNativeModules = ( mod : any ) : void => {
20
+ // tslint:disable-next-line:no-any
21
+ const nativeModules : ReadonlyArray < { id : string , module : any } > = [
22
+ { id : "vscode-textmate" , module : require ( "../../../../lib/vscode/node_modules/vscode-textmate" ) as typeof import ( "../../../../lib/vscode/node_modules/vscode-textmate" ) } ,
23
+ { id : "vscode-languageserver" , module : require ( "../../../../lib/vscode/node_modules/vscode-languageserver" ) as typeof import ( "../../../../lib/vscode/node_modules/vscode-languageserver" ) } ,
24
+ { id : "vscode-languageserver-types" , module : require ( "../../../../lib/vscode/node_modules/vscode-languageserver-types" ) as typeof import ( "../../../../lib/vscode/node_modules/vscode-languageserver-types" ) } ,
25
+ ] ;
26
+ const oldRequire = mod . prototype . require ;
27
+ // tslint:disable-next-line:no-any
28
+ mod . prototype . require = function ( id : string ) : any {
29
+ if ( id === "typescript" ) {
30
+ return require ( "typescript" ) ;
31
+ }
32
+ if ( id . slice ( 0 , 3 ) === "/./" ) {
33
+ id = id . slice ( 3 ) ;
34
+ }
35
+ const requireNativeModule = ( id : string ) : any => {
36
+ for ( let i = 0 ; i < nativeModules . length ; i ++ ) {
37
+ if ( id . slice ( - 1 * ( nativeModules [ i ] . id . length + 1 ) ) !== `/${ nativeModules [ i ] . id } ` ) {
38
+ continue ;
39
+ }
40
+
41
+ return nativeModules [ i ] . module ;
42
+ }
43
+ throw new Error ( `Module '${ id } ' undefined. If you need this native module, file an issue on GitHub.` ) ;
44
+ } ;
45
+
46
+ try {
47
+ // tslint:disable-next-line:no-any
48
+ return oldRequire . call ( this , id as any ) ;
49
+ } catch ( ex ) {
50
+ try {
51
+ return requireNativeModule ( id ) ;
52
+ } catch ( nex ) {
53
+ throw new Error ( `${ ex . message } : ${ nex . message } ` ) ;
54
+ }
55
+ }
56
+ } ;
57
+ } ;
58
+
14
59
/**
15
60
* Requires a module from the filesystem.
16
61
*
@@ -44,15 +89,7 @@ const requireFilesystemModule = (id: string, builtInExtensionsDir: string): any
44
89
*/
45
90
export const requireFork = ( modulePath : string , args : string [ ] , builtInExtensionsDir : string ) : void => {
46
91
const Module = require ( "module" ) as typeof import ( "module" ) ;
47
- const oldRequire = Module . prototype . require ;
48
- // tslint:disable-next-line:no-any
49
- Module . prototype . require = ( id : string ) : any => {
50
- if ( id === "typescript" ) {
51
- return require ( "typescript" ) ;
52
- }
53
-
54
- return oldRequire ( id ) ;
55
- } ;
92
+ requireNativeModules ( Module ) ;
56
93
57
94
if ( ! process . send ) {
58
95
throw new Error ( "No IPC messaging initialized" ) ;
@@ -79,33 +116,7 @@ export const requireModule = (modulePath: string, dataDir: string, builtInExtens
79
116
( global as any ) . XMLHttpRequest = xml . XMLHttpRequest ;
80
117
81
118
const mod = require ( "module" ) as typeof import ( "module" ) ;
82
-
83
- /**
84
- * A list of native packages that could be expected to
85
- * exist in the unpacked version of VS Code. This list may
86
- * grow in the future.
87
- */
88
- // tslint:disable-next-line:no-any
89
- const nativeModules : ReadonlyArray < { id : string , module : any } > = [
90
- { id : "vscode-textmate" , module : require ( "../../../../lib/vscode/node_modules/vscode-textmate" ) as typeof import ( "../../../../lib/vscode/node_modules/vscode-textmate" ) } ,
91
- ] ;
92
- const oldRequire = mod . prototype . require ;
93
- // tslint:disable-next-line:no-any
94
- mod . prototype . require = function ( id : string ) : any {
95
- if ( id . slice ( 0 , 3 ) === "/./" ) {
96
- for ( let i = 0 ; i < nativeModules . length ; i ++ ) {
97
- if ( id . slice ( - 1 * ( nativeModules [ i ] . id . length + 1 ) ) !== `/${ nativeModules [ i ] . id } ` ) {
98
- continue ;
99
- }
100
-
101
- return nativeModules [ i ] . module ;
102
- }
103
- throw new Error ( `Module '${ id } ' undefined. If you need this native module, file an issue on GitHub.` ) ;
104
- }
105
-
106
- // tslint:disable-next-line:no-any
107
- return oldRequire . call ( this , id as any ) ;
108
- } ;
119
+ requireNativeModules ( mod ) ;
109
120
110
121
const promiseFinally = require ( "promise.prototype.finally" ) as { shim : ( ) => void } ;
111
122
promiseFinally . shim ( ) ;
0 commit comments