33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import * as fs from 'node:fs' ;
7- import { register } from 'node:module' ;
8- import { product , pkg } from './bootstrap-meta.js' ;
9- import './bootstrap-node.js' ;
10- import * as performance from './vs/base/common/performance.js' ;
11- import { INLSConfiguration } from './vs/nls.js' ;
12-
13- // Install a hook to module resolution to map 'fs' to 'original-fs'
14- if ( process . env [ 'ELECTRON_RUN_AS_NODE' ] || process . versions [ 'electron' ] ) {
6+ import * as fs from "node:fs" ;
7+ import * as Module from "node:module" ;
8+ import { register } from "node:module" ;
9+ import { product , pkg } from "./bootstrap-meta.js" ;
10+ import "./bootstrap-node.js" ;
11+ import * as performance from "./vs/base/common/performance.js" ;
12+ import { INLSConfiguration } from "./vs/nls.js" ;
13+
14+ // Install a hook to module resolution to map "fs" to "original-fs"
15+ if ( process . env [ "ELECTRON_RUN_AS_NODE" ] || process . versions [ "electron" ] ) {
1516 const jsCode = `
1617 export async function resolve(specifier, context, nextResolve) {
17- if (specifier === 'fs' ) {
18+ if (specifier === "fs" ) {
1819 return {
19- format: ' builtin' ,
20+ format: " builtin" ,
2021 shortCircuit: true,
21- url: ' node:original-fs'
22+ url: " node:original-fs"
2223 };
2324 }
2425
2526 // Defer to the next hook in the chain, which would be the
2627 // Node.js default resolve if this is the last user-specified loader.
2728 return nextResolve(specifier, context);
2829 }` ;
29- register ( `data:text/javascript;base64,${ Buffer . from ( jsCode ) . toString ( 'base64' ) } ` , import . meta. url ) ;
30+
31+ // Try Module.register first (Node.js 20.6+), fallback to named import
32+ // @ts -ignore - Module.register may not be available in all Node.js versions
33+ if ( Module . register ) {
34+ // @ts -ignore - Module.register compatibility check
35+ Module . register (
36+ `data:text/javascript;base64,${ Buffer . from ( jsCode ) . toString ( "base64" ) } ` ,
37+ import . meta. url ,
38+ ) ;
39+ } else {
40+ // Fallback for environments where Module.register doesn't exist
41+ register (
42+ `data:text/javascript;base64,${ Buffer . from ( jsCode ) . toString ( "base64" ) } ` ,
43+ import . meta. url ,
44+ ) ;
45+ }
3046}
3147
3248// Prepare globals that are needed for running
@@ -36,7 +52,8 @@ globalThis._VSCODE_FILE_ROOT = import.meta.dirname;
3652
3753//#region NLS helpers
3854
39- let setupNLSResult : Promise < INLSConfiguration | undefined > | undefined = undefined ;
55+ let setupNLSResult : Promise < INLSConfiguration | undefined > | undefined =
56+ undefined ;
4057
4158function setupNLS ( ) : Promise < INLSConfiguration | undefined > {
4259 if ( ! setupNLSResult ) {
@@ -47,14 +64,14 @@ function setupNLS(): Promise<INLSConfiguration | undefined> {
4764}
4865
4966async function doSetupNLS ( ) : Promise < INLSConfiguration | undefined > {
50- performance . mark ( ' code/willLoadNls' ) ;
67+ performance . mark ( " code/willLoadNls" ) ;
5168
5269 let nlsConfig : INLSConfiguration | undefined = undefined ;
5370
5471 let messagesFile : string | undefined ;
55- if ( process . env [ ' VSCODE_NLS_CONFIG' ] ) {
72+ if ( process . env [ " VSCODE_NLS_CONFIG" ] ) {
5673 try {
57- nlsConfig = JSON . parse ( process . env [ ' VSCODE_NLS_CONFIG' ] ) ;
74+ nlsConfig = JSON . parse ( process . env [ " VSCODE_NLS_CONFIG" ] ) ;
5875 if ( nlsConfig ?. languagePack ?. messagesFile ) {
5976 messagesFile = nlsConfig . languagePack . messagesFile ;
6077 } else if ( nlsConfig ?. defaultMessagesFile ) {
@@ -68,45 +85,58 @@ async function doSetupNLS(): Promise<INLSConfiguration | undefined> {
6885 }
6986
7087 if (
71- process . env [ ' VSCODE_DEV' ] || // no NLS support in dev mode
72- ! messagesFile // no NLS messages file
88+ process . env [ " VSCODE_DEV" ] || // no NLS support in dev mode
89+ ! messagesFile // no NLS messages file
7390 ) {
7491 return undefined ;
7592 }
7693
7794 try {
78- globalThis . _VSCODE_NLS_MESSAGES = JSON . parse ( ( await fs . promises . readFile ( messagesFile ) ) . toString ( ) ) ;
95+ globalThis . _VSCODE_NLS_MESSAGES = JSON . parse (
96+ ( await fs . promises . readFile ( messagesFile ) ) . toString ( ) ,
97+ ) ;
7998 } catch ( error ) {
8099 console . error ( `Error reading NLS messages file ${ messagesFile } : ${ error } ` ) ;
81100
82101 // Mark as corrupt: this will re-create the language pack cache next startup
83102 if ( nlsConfig ?. languagePack ?. corruptMarkerFile ) {
84103 try {
85- await fs . promises . writeFile ( nlsConfig . languagePack . corruptMarkerFile , 'corrupted' ) ;
104+ await fs . promises . writeFile (
105+ nlsConfig . languagePack . corruptMarkerFile ,
106+ "corrupted" ,
107+ ) ;
86108 } catch ( error ) {
87109 console . error ( `Error writing corrupted NLS marker file: ${ error } ` ) ;
88110 }
89111 }
90112
91113 // Fallback to the default message file to ensure english translation at least
92- if ( nlsConfig ?. defaultMessagesFile && nlsConfig . defaultMessagesFile !== messagesFile ) {
114+ if (
115+ nlsConfig ?. defaultMessagesFile &&
116+ nlsConfig . defaultMessagesFile !== messagesFile
117+ ) {
93118 try {
94- globalThis . _VSCODE_NLS_MESSAGES = JSON . parse ( ( await fs . promises . readFile ( nlsConfig . defaultMessagesFile ) ) . toString ( ) ) ;
119+ globalThis . _VSCODE_NLS_MESSAGES = JSON . parse (
120+ (
121+ await fs . promises . readFile ( nlsConfig . defaultMessagesFile )
122+ ) . toString ( ) ,
123+ ) ;
95124 } catch ( error ) {
96- console . error ( `Error reading default NLS messages file ${ nlsConfig . defaultMessagesFile } : ${ error } ` ) ;
125+ console . error (
126+ `Error reading default NLS messages file ${ nlsConfig . defaultMessagesFile } : ${ error } ` ,
127+ ) ;
97128 }
98129 }
99130 }
100131
101- performance . mark ( ' code/didLoadNls' ) ;
132+ performance . mark ( " code/didLoadNls" ) ;
102133
103134 return nlsConfig ;
104135}
105136
106137//#endregion
107138
108139export async function bootstrapESM ( ) : Promise < void > {
109-
110140 // NLS
111141 await setupNLS ( ) ;
112142}
0 commit comments