|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as path from "path" |
| 7 | +import * as fs from "fs" |
| 8 | +import { fileURLToPath } from "url" |
| 9 | +import { createRequire, register } from "node:module" |
| 10 | +import { product, pkg } from "./bootstrap-meta.js" |
| 11 | +import "./bootstrap-node.js" |
| 12 | +import * as performance from "./vs/base/common/performance.js" |
| 13 | +import { INLSConfiguration } from "./vs/nls.js" |
| 14 | + |
| 15 | +const require = createRequire(import.meta.url) |
| 16 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 17 | + |
| 18 | +// Install a hook to module resolution to map 'fs' to 'original-fs' |
| 19 | +if (process.env["ELECTRON_RUN_AS_NODE"] || process.versions["electron"]) { |
| 20 | + const jsCode = ` |
| 21 | + export async function resolve(specifier, context, nextResolve) { |
| 22 | + if (specifier === 'fs') { |
| 23 | + return { |
| 24 | + format: 'builtin', |
| 25 | + shortCircuit: true, |
| 26 | + url: 'node:original-fs' |
| 27 | + }; |
| 28 | + } |
| 29 | +
|
| 30 | + // Defer to the next hook in the chain, which would be the |
| 31 | + // Node.js default resolve if this is the last user-specified loader. |
| 32 | + return nextResolve(specifier, context); |
| 33 | + }` |
| 34 | + register(`data:text/javascript;base64,${Buffer.from(jsCode).toString("base64")}`, import.meta.url) |
| 35 | +} |
| 36 | + |
| 37 | +// Prepare globals that are needed for running |
| 38 | +globalThis._VSCODE_PRODUCT_JSON = { ...product } |
| 39 | +if (process.env["VSCODE_DEV"]) { |
| 40 | + try { |
| 41 | + const overrides: unknown = require("../product.overrides.json") |
| 42 | + globalThis._VSCODE_PRODUCT_JSON = Object.assign(globalThis._VSCODE_PRODUCT_JSON, overrides) |
| 43 | + } catch (error) { |
| 44 | + /* ignore */ |
| 45 | + } |
| 46 | +} |
| 47 | +globalThis._VSCODE_PACKAGE_JSON = { ...pkg } |
| 48 | +globalThis._VSCODE_FILE_ROOT = __dirname |
| 49 | + |
| 50 | +//#region NLS helpers |
| 51 | + |
| 52 | +let setupNLSResult: Promise<INLSConfiguration | undefined> | undefined = undefined |
| 53 | + |
| 54 | +function setupNLS(): Promise<INLSConfiguration | undefined> { |
| 55 | + if (!setupNLSResult) { |
| 56 | + setupNLSResult = doSetupNLS() |
| 57 | + } |
| 58 | + |
| 59 | + return setupNLSResult |
| 60 | +} |
| 61 | + |
| 62 | +async function doSetupNLS(): Promise<INLSConfiguration | undefined> { |
| 63 | + performance.mark("code/willLoadNls") |
| 64 | + |
| 65 | + let nlsConfig: INLSConfiguration | undefined = undefined |
| 66 | + |
| 67 | + let messagesFile: string | undefined |
| 68 | + if (process.env["VSCODE_NLS_CONFIG"]) { |
| 69 | + try { |
| 70 | + nlsConfig = JSON.parse(process.env["VSCODE_NLS_CONFIG"]) |
| 71 | + if (nlsConfig?.languagePack?.messagesFile) { |
| 72 | + messagesFile = nlsConfig.languagePack.messagesFile |
| 73 | + } else if (nlsConfig?.defaultMessagesFile) { |
| 74 | + messagesFile = nlsConfig.defaultMessagesFile |
| 75 | + } |
| 76 | + |
| 77 | + globalThis._VSCODE_NLS_LANGUAGE = nlsConfig?.resolvedLanguage |
| 78 | + } catch (e) { |
| 79 | + console.error(`Error reading VSCODE_NLS_CONFIG from environment: ${e}`) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if ( |
| 84 | + process.env["VSCODE_DEV"] || // no NLS support in dev mode |
| 85 | + !messagesFile // no NLS messages file |
| 86 | + ) { |
| 87 | + return undefined |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + globalThis._VSCODE_NLS_MESSAGES = JSON.parse((await fs.promises.readFile(messagesFile)).toString()) |
| 92 | + } catch (error) { |
| 93 | + console.error(`Error reading NLS messages file ${messagesFile}: ${error}`) |
| 94 | + |
| 95 | + // Mark as corrupt: this will re-create the language pack cache next startup |
| 96 | + if (nlsConfig?.languagePack?.corruptMarkerFile) { |
| 97 | + try { |
| 98 | + await fs.promises.writeFile(nlsConfig.languagePack.corruptMarkerFile, "corrupted") |
| 99 | + } catch (error) { |
| 100 | + console.error(`Error writing corrupted NLS marker file: ${error}`) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Fallback to the default message file to ensure english translation at least |
| 105 | + if (nlsConfig?.defaultMessagesFile && nlsConfig.defaultMessagesFile !== messagesFile) { |
| 106 | + try { |
| 107 | + globalThis._VSCODE_NLS_MESSAGES = JSON.parse( |
| 108 | + (await fs.promises.readFile(nlsConfig.defaultMessagesFile)).toString(), |
| 109 | + ) |
| 110 | + } catch (error) { |
| 111 | + console.error(`Error reading default NLS messages file ${nlsConfig.defaultMessagesFile}: ${error}`) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + performance.mark("code/didLoadNls") |
| 117 | + |
| 118 | + return nlsConfig |
| 119 | +} |
| 120 | + |
| 121 | +//#endregion |
| 122 | + |
| 123 | +export async function bootstrapESM(): Promise<void> { |
| 124 | + // NLS |
| 125 | + await setupNLS() |
| 126 | +} |
0 commit comments