From 16654e61265ca89e53612bb7a61f89f4e99aeb3c Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 9 Jul 2024 08:08:56 +0200 Subject: [PATCH] esm - reduce diff to branch (#221154) --- scripts/code-server.js | 1 - src/bootstrap-amd.js | 152 ++++++++++---- src/bootstrap-fork.js | 15 +- src/bootstrap-meta.js | 16 +- src/bootstrap-node.js | 74 ++++--- src/bootstrap-window.js | 188 +++++++++++++----- src/bootstrap.js | 24 ++- src/cli.js | 29 ++- src/main.js | 42 ++-- src/server-cli.js | 20 +- src/server-main.js | 46 +++-- src/tsec.exemptions.json | 3 + src/typings/vscode-globals-product.d.ts | 11 + src/vs/base/common/jsonc.js | 12 +- src/vs/base/common/performance.js | 9 + src/vs/base/common/semver/semver.d.ts | 7 +- src/vs/base/common/semver/semver.js | 45 +++++ src/vs/base/node/nls.js | 14 ++ src/vs/base/node/unc.js | 12 ++ .../base/parts/sandbox/common/sandboxTypes.ts | 5 + src/vs/base/parts/storage/node/storage.ts | 8 +- .../processExplorer/processExplorer.js | 3 +- .../electron-sandbox/workbench/workbench.js | 3 +- .../platform/environment/node/userDataPath.js | 16 +- .../node/nativeModules.integrationTest.ts | 5 + src/vs/platform/request/node/proxy.ts | 20 +- src/vs/platform/terminal/node/ptyService.ts | 9 +- src/vs/server/node/server.cli.ts | 39 ++-- .../api/node/extHostExtensionService.ts | 6 +- src/vs/workbench/api/node/proxyResolver.ts | 11 + .../issue/electron-sandbox/issueReporter.js | 3 +- 31 files changed, 650 insertions(+), 198 deletions(-) diff --git a/scripts/code-server.js b/scripts/code-server.js index c043bf2671b..56945e76ca7 100644 --- a/scripts/code-server.js +++ b/scripts/code-server.js @@ -69,4 +69,3 @@ function startServer(programArgs) { } main(); - diff --git a/src/bootstrap-amd.js b/src/bootstrap-amd.js index 27d15eb76d5..f8a0c00be95 100644 --- a/src/bootstrap-amd.js +++ b/src/bootstrap-amd.js @@ -7,10 +7,28 @@ 'use strict'; /** - * @typedef {import('./vs/nls').INLSConfiguration} INLSConfiguration + * @import { INLSConfiguration } from './vs/nls' * @import { IProductConfiguration } from './vs/base/common/product' */ +// ESM-comment-begin +const isESM = false; +// ESM-comment-end +// ESM-uncomment-begin +// import * as path from 'path'; +// import * as fs from 'fs'; +// import { fileURLToPath } from 'url'; +// import { createRequire } from 'node:module'; +// import { product, pkg } from './bootstrap-meta.js'; +// import * as bootstrap from './bootstrap.js'; +// import * as performance from './vs/base/common/performance.js'; +// +// const require = createRequire(import.meta.url); +// const isESM = true; +// const module = { exports: {} }; +// const __dirname = path.dirname(fileURLToPath(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. @@ -21,7 +39,12 @@ globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target // VSCODE_GLOBALS: package/product.json /** @type Partial */ +// 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 { @@ -30,29 +53,21 @@ if (process.env['VSCODE_DEV']) { 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 -// @ts-ignore -const loader = require('./vs/loader'); +// VSCODE_GLOBALS: file root of all resources +globalThis._VSCODE_FILE_ROOT = __dirname; + +// ESM-comment-begin const bootstrap = require('./bootstrap'); -const performance = require('./vs/base/common/performance'); +const performance = require(`./vs/base/common/performance`); const fs = require('fs'); - -// Bootstrap: Loader -loader.config({ - baseUrl: bootstrap.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 - }); -} +// ESM-comment-end //#region NLS helpers @@ -138,31 +153,82 @@ async function doSetupNLS() { //#endregion -/** - * @param {string=} entrypoint - * @param {(value: any) => void=} onLoad - * @param {(err: Error) => void=} onError - */ -exports.load = function (entrypoint, onLoad, onError) { - if (!entrypoint) { - return; - } +//#region Loader Config - // code cache config - if (process.env['VSCODE_CODE_CACHE_PATH']) { - loader.config({ - nodeCachedData: { - path: process.env['VSCODE_CODE_CACHE_PATH'], - seed: entrypoint - } +if (isESM) { + + /** + * @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); + }); + }; +} else { + + // @ts-ignore + const loader = require('./vs/loader'); + + loader.config({ + baseUrl: bootstrap.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 }); } - onLoad = onLoad || function () { }; - onError = onError || function (err) { console.error(err); }; + /** + * @param {string=} entrypoint + * @param {(value: any) => void} [onLoad] + * @param {(err: Error) => void} [onError] + */ + module.exports.load = function (entrypoint, onLoad, onError) { + if (!entrypoint) { + return; + } - setupNLS().then(() => { - performance.mark('code/fork/willLoadCode'); - loader([entrypoint], onLoad, onError); - }); -}; + // 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); + }); + }; +} + +//#endregion + +// ESM-uncomment-begin +// export const load = module.exports.load; +// ESM-uncomment-end diff --git a/src/bootstrap-fork.js b/src/bootstrap-fork.js index 9de1e6f0d15..a95cbf53589 100644 --- a/src/bootstrap-fork.js +++ b/src/bootstrap-fork.js @@ -6,11 +6,20 @@ //@ts-check 'use strict'; +// ESM-comment-begin const performance = require('./vs/base/common/performance'); -performance.mark('code/fork/start'); - const bootstrap = require('./bootstrap'); const bootstrapNode = require('./bootstrap-node'); +const bootstrapAmd = require('./bootstrap-amd'); +// ESM-comment-end +// ESM-uncomment-begin +// import * as performance from './vs/base/common/performance.js'; +// import * as bootstrap from './bootstrap.js'; +// import * as bootstrapNode from './bootstrap-node.js'; +// import * as bootstrapAmd from './bootstrap-amd.js'; +// ESM-uncomment-end + +performance.mark('code/fork/start'); // Crash reporter configureCrashReporter(); @@ -41,7 +50,7 @@ if (process.env['VSCODE_PARENT_PID']) { } // Load AMD entry point -require('./bootstrap-amd').load(process.env['VSCODE_AMD_ENTRYPOINT']); +bootstrapAmd.load(process.env['VSCODE_AMD_ENTRYPOINT']); //#region Helpers diff --git a/src/bootstrap-meta.js b/src/bootstrap-meta.js index 7924b77eec8..46041d5d91a 100644 --- a/src/bootstrap-meta.js +++ b/src/bootstrap-meta.js @@ -10,6 +10,13 @@ * @import { IProductConfiguration } from './vs/base/common/product' */ +// ESM-uncomment-begin +// import { createRequire } from 'node:module'; +// +// const require = createRequire(import.meta.url); +// const module = { exports: {} }; +// ESM-uncomment-end + /** @type Partial & { BUILD_INSERT_PRODUCT_CONFIGURATION?: string } */ let product = { BUILD_INSERT_PRODUCT_CONFIGURATION: 'BUILD_INSERT_PRODUCT_CONFIGURATION' }; // DO NOT MODIFY, PATCHED DURING BUILD if (product['BUILD_INSERT_PRODUCT_CONFIGURATION']) { @@ -24,5 +31,10 @@ if (pkg['BUILD_INSERT_PACKAGE_CONFIGURATION']) { pkg = require('../package.json'); // Running out of sources } -exports.product = product; -exports.pkg = pkg; +module.exports.product = product; +module.exports.pkg = pkg; + +// ESM-uncomment-begin +// export const product = module.exports.product; +// export const pkg = module.exports.pkg; +// ESM-uncomment-end diff --git a/src/bootstrap-node.js b/src/bootstrap-node.js index 914b8290380..4a829557f0d 100644 --- a/src/bootstrap-node.js +++ b/src/bootstrap-node.js @@ -6,12 +6,28 @@ //@ts-check 'use strict'; +// ESM-comment-begin +const path = require('path'); +const fs = require('fs'); + +const isESM = false; +// ESM-comment-end +// ESM-uncomment-begin +// import * as path from 'path'; +// import * as fs from 'fs'; +// import { fileURLToPath } from 'url'; +// import { createRequire } from 'node:module'; +// +// const require = createRequire(import.meta.url); +// const isESM = true; +// const module = { exports: {} }; +// const __dirname = path.dirname(fileURLToPath(import.meta.url)); +// ESM-uncomment-end + // Setup current working directory in all our node & electron processes // - Windows: call `process.chdir()` to always set application folder as cwd // - all OS: store the `process.cwd()` inside `VSCODE_CWD` for consistent lookups function setupCurrentWorkingDirectory() { - const path = require('path'); - try { // Store the `process.cwd()` inside `VSCODE_CWD` @@ -38,36 +54,41 @@ setupCurrentWorkingDirectory(); * * @param {string} injectPath */ -exports.injectNodeModuleLookupPath = function (injectPath) { +module.exports.injectNodeModuleLookupPath = function (injectPath) { if (!injectPath) { throw new Error('Missing injectPath'); } - const Module = require('module'); - const path = require('path'); + const Module = require('node:module'); + if (isESM) { + // register a loader hook + // ESM-uncomment-begin + // Module.register('./server-loader.mjs', { parentURL: import.meta.url, data: injectPath }); + // ESM-uncomment-end + } else { + const nodeModulesPath = path.join(__dirname, '../node_modules'); - const nodeModulesPath = path.join(__dirname, '../node_modules'); + // @ts-ignore + const originalResolveLookupPaths = Module._resolveLookupPaths; - // @ts-ignore - const originalResolveLookupPaths = Module._resolveLookupPaths; - - // @ts-ignore - Module._resolveLookupPaths = function (moduleName, parent) { - const paths = originalResolveLookupPaths(moduleName, parent); - if (Array.isArray(paths)) { - for (let i = 0, len = paths.length; i < len; i++) { - if (paths[i] === nodeModulesPath) { - paths.splice(i, 0, injectPath); - break; + // @ts-ignore + Module._resolveLookupPaths = function (moduleName, parent) { + const paths = originalResolveLookupPaths(moduleName, parent); + if (Array.isArray(paths)) { + for (let i = 0, len = paths.length; i < len; i++) { + if (paths[i] === nodeModulesPath) { + paths.splice(i, 0, injectPath); + break; + } } } - } - return paths; - }; + return paths; + }; + } }; -exports.removeGlobalNodeModuleLookupPaths = function () { +module.exports.removeGlobalNodeModuleLookupPaths = function () { const Module = require('module'); // @ts-ignore const globalPaths = Module.globalPaths; @@ -95,10 +116,7 @@ exports.removeGlobalNodeModuleLookupPaths = function () { * @param {Partial} product * @returns {{ portableDataPath: string; isPortable: boolean; }} */ -exports.configurePortable = function (product) { - const fs = require('fs'); - const path = require('path'); - +module.exports.configurePortable = function (product) { const appRoot = path.dirname(__dirname); /** @@ -158,3 +176,9 @@ exports.configurePortable = function (product) { isPortable }; }; + +// ESM-uncomment-begin +// export const injectNodeModuleLookupPath = module.exports.injectNodeModuleLookupPath; +// export const removeGlobalNodeModuleLookupPaths = module.exports.removeGlobalNodeModuleLookupPaths; +// export const configurePortable = module.exports.configurePortable; +// ESM-uncomment-end diff --git a/src/bootstrap-window.js b/src/bootstrap-window.js index cd859a847f9..59ddc3fdfbf 100644 --- a/src/bootstrap-window.js +++ b/src/bootstrap-window.js @@ -10,12 +10,20 @@ /** * @import { ISandboxConfiguration } from './vs/base/parts/sandbox/common/sandboxTypes' + * @typedef {any} LoaderConfig */ -/* eslint-disable no-restricted-globals */ +/* eslint-disable no-restricted-globals, */ + +// ESM-comment-begin +const isESM = false; +// ESM-comment-end +// ESM-uncomment-begin +// const isESM = true; +// ESM-uncomment-end // Simple module style to support node.js and browser environments -(function (globalThis, factory) { +(function (factory) { // Node.js if (typeof exports === 'object') { @@ -27,7 +35,7 @@ // @ts-ignore globalThis.MonacoBootstrapWindow = factory(); } -}(this, function () { +}(function () { const bootstrapLib = bootstrap(); const preloadGlobals = sandboxGlobals(); const safeProcess = preloadGlobals.process; @@ -96,59 +104,137 @@ window['MonacoEnvironment'] = {}; - /** @type {any} */ - const loaderConfig = { - baseUrl: `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out`, - preferScriptTags: true - }; + if (isESM) { - // use a trusted types policy when loading via script tags - loaderConfig.trustedTypesPolicy = window.trustedTypes?.createPolicy('amdLoader', { - createScriptURL(value) { - if (value.startsWith(window.location.origin)) { - return value; - } - throw new Error(`Invalid script url: ${value}`); + // Signal before require() + if (typeof options?.beforeRequire === 'function') { + options.beforeRequire(configuration); } - }); - // Teach the loader the location of the node modules we use in renderers - // This will enable to load these modules via