forked from meltylabs/melty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-amd.js
246 lines (208 loc) · 7.14 KB
/
bootstrap-amd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
'use strict';
/**
* @import { INLSConfiguration } from './vs/nls'
* @import { IProductConfiguration } from './vs/base/common/product'
*/
// ESM-uncomment-begin
// import * as path from 'path';
// import * as fs from 'fs';
// import { fileURLToPath } from 'url';
// import { createRequire, register } from 'node:module';
// import { product, pkg } from './bootstrap-meta.js';
// import './bootstrap-node.js';
// import * as performance from './vs/base/common/performance.js';
//
// const require = createRequire(import.meta.url);
// const module = { exports: {} };
// const __dirname = path.dirname(fileURLToPath(import.meta.url));
//
// // Install a hook to module resolution to map 'fs' to 'original-fs'
// if (process.env['ELECTRON_RUN_AS_NODE'] || process.versions['electron']) {
// const jsCode = `
// export async function resolve(specifier, context, nextResolve) {
// if (specifier === 'fs') {
// return {
// format: 'builtin',
// shortCircuit: true,
// url: 'node:original-fs'
// };
// }
// // Defer to the next hook in the chain, which would be the
// // Node.js default resolve if this is the last user-specified loader.
// return nextResolve(specifier, context);
// }`;
// register(`data:text/javascript;base64,${Buffer.from(jsCode).toString('base64')}`, import.meta.url);
// }
// ESM-uncomment-end
// Store the node.js require function in a variable
// before loading our AMD loader to avoid issues
// when this file is bundled with other files.
const nodeRequire = require;
// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => nodeRequire(String(mod)) });
// VSCODE_GLOBALS: package/product.json
/** @type Partial<IProductConfiguration> */
// ESM-comment-begin
globalThis._VSCODE_PRODUCT_JSON = require('./bootstrap-meta').product;
// ESM-comment-end
// ESM-uncomment-begin
// globalThis._VSCODE_PRODUCT_JSON = { ...product };
// ESM-uncomment-end
if (process.env['VSCODE_DEV']) {
// Patch product overrides when running out of sources
try {
// @ts-ignore
const overrides = require('../product.overrides.json');
globalThis._VSCODE_PRODUCT_JSON = Object.assign(globalThis._VSCODE_PRODUCT_JSON, overrides);
} catch (error) { /* ignore */ }
}
// ESM-comment-begin
globalThis._VSCODE_PACKAGE_JSON = require('./bootstrap-meta').pkg;
// ESM-comment-end
// ESM-uncomment-begin
// globalThis._VSCODE_PACKAGE_JSON = { ...pkg };
// ESM-uncomment-end
// VSCODE_GLOBALS: file root of all resources
globalThis._VSCODE_FILE_ROOT = __dirname;
// ESM-comment-begin
const bootstrapNode = require('./bootstrap-node');
const performance = require(`./vs/base/common/performance`);
const fs = require('fs');
// ESM-comment-end
//#region NLS helpers
/** @type {Promise<INLSConfiguration | undefined> | undefined} */
let setupNLSResult = undefined;
/**
* @returns {Promise<INLSConfiguration | undefined>}
*/
function setupNLS() {
if (!setupNLSResult) {
setupNLSResult = doSetupNLS();
}
return setupNLSResult;
}
/**
* @returns {Promise<INLSConfiguration | undefined>}
*/
async function doSetupNLS() {
performance.mark('code/amd/willLoadNls');
/** @type {INLSConfiguration | undefined} */
let nlsConfig = undefined;
/** @type {string | undefined} */
let messagesFile;
if (process.env['VSCODE_NLS_CONFIG']) {
try {
/** @type {INLSConfiguration} */
nlsConfig = JSON.parse(process.env['VSCODE_NLS_CONFIG']);
if (nlsConfig?.languagePack?.messagesFile) {
messagesFile = nlsConfig.languagePack.messagesFile;
} else if (nlsConfig?.defaultMessagesFile) {
messagesFile = nlsConfig.defaultMessagesFile;
}
globalThis._VSCODE_NLS_LANGUAGE = nlsConfig?.resolvedLanguage;
} catch (e) {
console.error(`Error reading VSCODE_NLS_CONFIG from environment: ${e}`);
}
}
if (
process.env['VSCODE_DEV'] || // no NLS support in dev mode
!messagesFile // no NLS messages file
) {
return undefined;
}
try {
globalThis._VSCODE_NLS_MESSAGES = JSON.parse((await fs.promises.readFile(messagesFile)).toString());
} catch (error) {
console.error(`Error reading NLS messages file ${messagesFile}: ${error}`);
// Mark as corrupt: this will re-create the language pack cache next startup
if (nlsConfig?.languagePack?.corruptMarkerFile) {
try {
await fs.promises.writeFile(nlsConfig.languagePack.corruptMarkerFile, 'corrupted');
} catch (error) {
console.error(`Error writing corrupted NLS marker file: ${error}`);
}
}
// Fallback to the default message file to ensure english translation at least
if (nlsConfig?.defaultMessagesFile && nlsConfig.defaultMessagesFile !== messagesFile) {
try {
globalThis._VSCODE_NLS_MESSAGES = JSON.parse((await fs.promises.readFile(nlsConfig.defaultMessagesFile)).toString());
} catch (error) {
console.error(`Error reading default NLS messages file ${nlsConfig.defaultMessagesFile}: ${error}`);
}
}
}
performance.mark('code/amd/didLoadNls');
return nlsConfig;
}
//#endregion
//#region Loader Config
// ESM-uncomment-begin
// /**
// * @param {string=} entrypoint
// * @param {(value: any) => void} [onLoad]
// * @param {(err: Error) => void} [onError]
// */
// module.exports.load = function (entrypoint, onLoad, onError) {
// if (!entrypoint) {
// return;
// }
// entrypoint = `./${entrypoint}.js`;
// onLoad = onLoad || function () { };
// onError = onError || function (err) { console.error(err); };
// setupNLS().then(() => {
// performance.mark(`code/fork/willLoadCode`);
// import(entrypoint).then(onLoad, onError);
// });
// };
// ESM-uncomment-end
// ESM-comment-begin
// @ts-ignore
const loader = require('./vs/loader');
loader.config({
baseUrl: bootstrapNode.fileUriFromPath(__dirname, { isWindows: process.platform === 'win32' }),
catchError: true,
nodeRequire,
amdModulesPattern: /^vs\//,
recordStats: true
});
// Running in Electron
if (process.env['ELECTRON_RUN_AS_NODE'] || process.versions['electron']) {
loader.define('fs', ['original-fs'], function (/** @type {import('fs')} */originalFS) {
return originalFS; // replace the patched electron fs with the original node fs for all AMD code
});
}
/**
* @param {string=} entrypoint
* @param {(value: any) => void} [onLoad]
* @param {(err: Error) => void} [onError]
*/
module.exports.load = function (entrypoint, onLoad, onError) {
if (!entrypoint) {
return;
}
// code cache config
if (process.env['VSCODE_CODE_CACHE_PATH']) {
loader.config({
nodeCachedData: {
path: process.env['VSCODE_CODE_CACHE_PATH'],
seed: entrypoint
}
});
}
onLoad = onLoad || function () { };
onError = onError || function (err) { console.error(err); };
setupNLS().then(() => {
performance.mark('code/fork/willLoadCode');
loader([entrypoint], onLoad, onError);
});
};
// ESM-comment-end
//#endregion
// ESM-uncomment-begin
// export const load = module.exports.load;
// ESM-uncomment-end