diff --git a/build/lib/standalone.js b/build/lib/standalone.js index cf0e452aff3..78030842569 100644 --- a/build/lib/standalone.js +++ b/build/lib/standalone.js @@ -131,7 +131,7 @@ function createESMSourcesAndResources2(options) { } if (file === 'tsconfig.json') { const tsConfig = JSON.parse(fs.readFileSync(path.join(SRC_FOLDER, file)).toString()); - tsConfig.compilerOptions.module = 'es6'; + tsConfig.compilerOptions.module = 'es2022'; tsConfig.compilerOptions.outDir = path.join(path.relative(OUT_FOLDER, OUT_RESOURCES_FOLDER), 'vs').replace(/\\/g, '/'); write(getDestAbsoluteFilePath(file), JSON.stringify(tsConfig, null, '\t')); continue; diff --git a/build/lib/standalone.ts b/build/lib/standalone.ts index 9a65bfa7444..e1b9db65e12 100644 --- a/build/lib/standalone.ts +++ b/build/lib/standalone.ts @@ -157,7 +157,7 @@ export function createESMSourcesAndResources2(options: IOptions2): void { if (file === 'tsconfig.json') { const tsConfig = JSON.parse(fs.readFileSync(path.join(SRC_FOLDER, file)).toString()); - tsConfig.compilerOptions.module = 'es6'; + tsConfig.compilerOptions.module = 'es2022'; tsConfig.compilerOptions.outDir = path.join(path.relative(OUT_FOLDER, OUT_RESOURCES_FOLDER), 'vs').replace(/\\/g, '/'); write(getDestAbsoluteFilePath(file), JSON.stringify(tsConfig, null, '\t')); continue; diff --git a/src/bootstrap-amd.js b/src/bootstrap-amd.js index c228faccfed..b8cc7a8bb50 100644 --- a/src/bootstrap-amd.js +++ b/src/bootstrap-amd.js @@ -11,20 +11,16 @@ * @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, register } from 'node:module'; // import { product, pkg } from './bootstrap-meta.js'; -// import * as bootstrapNode from './bootstrap-node.js'; +// import './bootstrap-node.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)); // @@ -171,77 +167,77 @@ async function doSetupNLS() { //#region Loader Config -if (isESM) { +// 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; +// } - /** - * @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`; - entrypoint = `./${entrypoint}.js`; +// onLoad = onLoad || function () { }; +// onError = onError || function (err) { console.error(err); }; - 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 - setupNLS().then(() => { - performance.mark(`code/fork/willLoadCode`); - import(entrypoint).then(onLoad, onError); - }); - }; -} else { +// ESM-comment-begin +// @ts-ignore +const loader = require('./vs/loader'); - // @ts-ignore - const loader = require('./vs/loader'); +loader.config({ + baseUrl: bootstrapNode.fileUriFromPath(__dirname, { isWindows: process.platform === 'win32' }), + catchError: true, + nodeRequire, + amdModulesPattern: /^vs\//, + recordStats: true +}); - 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 }); +} - // 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 + } }); } - /** - * @param {string=} entrypoint - * @param {(value: any) => void} [onLoad] - * @param {(err: Error) => void} [onError] - */ - module.exports.load = function (entrypoint, onLoad, onError) { - if (!entrypoint) { - return; - } + onLoad = onLoad || function () { }; + onError = onError || function (err) { console.error(err); }; - // 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); - }); - }; -} + setupNLS().then(() => { + performance.mark('code/fork/willLoadCode'); + loader([entrypoint], onLoad, onError); + }); +}; +// ESM-comment-end //#endregion diff --git a/src/bootstrap-node.js b/src/bootstrap-node.js index 1484cbb8bed..b375c69b9b3 100644 --- a/src/bootstrap-node.js +++ b/src/bootstrap-node.js @@ -10,18 +10,12 @@ const path = require('path'); const fs = require('fs'); const Module = require('module'); - -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 Module = require('module'); -// const isESM = true; // const module = { exports: {} }; // const __dirname = path.dirname(fileURLToPath(import.meta.url)); // ESM-uncomment-end @@ -86,32 +80,31 @@ module.exports.devInjectNodeModuleLookupPath = function (injectPath) { } const Module = require('node:module'); - if (isESM) { - // register a loader hook - // ESM-uncomment-begin - // Module.register('./bootstrap-import.js', { parentURL: import.meta.url, data: injectPath }); - // ESM-uncomment-end - } else { - const nodeModulesPath = path.join(__dirname, '../node_modules'); + // ESM-uncomment-begin + // // register a loader hook + // Module.register('./bootstrap-import.js', { parentURL: import.meta.url, data: injectPath }); + // ESM-uncomment-end + // ESM-comment-begin + 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; + }; + // ESM-comment-end }; module.exports.removeGlobalNodeModuleLookupPaths = function () { @@ -207,30 +200,28 @@ module.exports.configurePortable = function (product) { * Helper to enable ASAR support. */ module.exports.enableASARSupport = function () { - if (isESM) { - return; // TODO@esm ASAR support is disabled in ESM - } else { - const NODE_MODULES_PATH = path.join(__dirname, '../node_modules'); - const NODE_MODULES_ASAR_PATH = `${NODE_MODULES_PATH}.asar`; + // ESM-comment-begin + const NODE_MODULES_PATH = path.join(__dirname, '../node_modules'); + const NODE_MODULES_ASAR_PATH = `${NODE_MODULES_PATH}.asar`; - // @ts-ignore - const originalResolveLookupPaths = Module._resolveLookupPaths; + // @ts-ignore + const originalResolveLookupPaths = Module._resolveLookupPaths; - // @ts-ignore - Module._resolveLookupPaths = function (request, parent) { - const paths = originalResolveLookupPaths(request, parent); - if (Array.isArray(paths)) { - for (let i = 0, len = paths.length; i < len; i++) { - if (paths[i] === NODE_MODULES_PATH) { - paths.splice(i, 0, NODE_MODULES_ASAR_PATH); - break; - } + // @ts-ignore + Module._resolveLookupPaths = function (request, parent) { + const paths = originalResolveLookupPaths(request, parent); + if (Array.isArray(paths)) { + for (let i = 0, len = paths.length; i < len; i++) { + if (paths[i] === NODE_MODULES_PATH) { + paths.splice(i, 0, NODE_MODULES_ASAR_PATH); + break; } } + } - return paths; - }; - } + return paths; + }; + // ESM-comment-end }; /** diff --git a/src/bootstrap-window.js b/src/bootstrap-window.js index b3216ac0f2e..25a4a216ba2 100644 --- a/src/bootstrap-window.js +++ b/src/bootstrap-window.js @@ -15,13 +15,6 @@ /* eslint-disable no-restricted-globals */ -// ESM-comment-begin -const isESM = false; -// ESM-comment-end -// ESM-uncomment-begin -// const isESM = true; -// ESM-uncomment-end - (function (factory) { // @ts-ignore globalThis.MonacoBootstrapWindow = factory(); @@ -95,133 +88,133 @@ const isESM = false; window['MonacoEnvironment'] = {}; - if (isESM) { + // ESM-uncomment-begin + // // Signal before require() + // if (typeof options?.beforeRequire === 'function') { + // options.beforeRequire(configuration); + // } - // Signal before require() - if (typeof options?.beforeRequire === 'function') { - options.beforeRequire(configuration); - } + // const baseUrl = new URL(`${fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out/`); + // globalThis._VSCODE_FILE_ROOT = baseUrl.toString(); - const baseUrl = new URL(`${fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out/`); - globalThis._VSCODE_FILE_ROOT = baseUrl.toString(); + // // DEV --------------------------------------------------------------------------------------- + // // DEV: This is for development and enables loading CSS via import-statements via import-maps. + // // DEV: For each CSS modules that we have we defined an entry in the import map that maps to + // // DEV: a blob URL that loads the CSS via a dynamic @import-rule. + // // DEV --------------------------------------------------------------------------------------- + // if (Array.isArray(configuration.cssModules) && configuration.cssModules.length > 0) { + // performance.mark('code/willAddCssLoader'); - // DEV --------------------------------------------------------------------------------------- - // DEV: This is for development and enables loading CSS via import-statements via import-maps. - // DEV: For each CSS modules that we have we defined an entry in the import map that maps to - // DEV: a blob URL that loads the CSS via a dynamic @import-rule. - // DEV --------------------------------------------------------------------------------------- - if (Array.isArray(configuration.cssModules) && configuration.cssModules.length > 0) { - performance.mark('code/willAddCssLoader'); + // const style = document.createElement('style'); + // style.type = 'text/css'; + // style.media = 'screen'; + // style.id = 'vscode-css-loading'; + // document.head.appendChild(style); - const style = document.createElement('style'); - style.type = 'text/css'; - style.media = 'screen'; - style.id = 'vscode-css-loading'; - document.head.appendChild(style); + // globalThis._VSCODE_CSS_LOAD = function (url) { + // style.textContent += `@import url(${url});\n`; + // }; - globalThis._VSCODE_CSS_LOAD = function (url) { - style.textContent += `@import url(${url});\n`; - }; + // /** + // * @type { { imports: Record }} + // */ + // const importMap = { imports: {} }; + // for (const cssModule of configuration.cssModules) { + // const cssUrl = new URL(cssModule, baseUrl).href; + // const jsSrc = `globalThis._VSCODE_CSS_LOAD('${cssUrl}');\n`; + // const blob = new Blob([jsSrc], { type: 'application/javascript' }); + // importMap.imports[cssUrl] = URL.createObjectURL(blob); + // } - /** - * @type { { imports: Record }} - */ - const importMap = { imports: {} }; - for (const cssModule of configuration.cssModules) { - const cssUrl = new URL(cssModule, baseUrl).href; - const jsSrc = `globalThis._VSCODE_CSS_LOAD('${cssUrl}');\n`; - const blob = new Blob([jsSrc], { type: 'application/javascript' }); - importMap.imports[cssUrl] = URL.createObjectURL(blob); + // const ttp = window.trustedTypes?.createPolicy('vscode-bootstrapImportMap', { createScript(value) { return value; }, }); + // const importMapSrc = JSON.stringify(importMap, undefined, 2); + // const importMapScript = document.createElement('script'); + // importMapScript.type = 'importmap'; + // importMapScript.setAttribute('nonce', '0c6a828f1297'); + // // @ts-ignore + // importMapScript.textContent = ttp?.createScript(importMapSrc) ?? importMapSrc; + // document.head.appendChild(importMapScript); + + // performance.mark('code/didAddCssLoader'); + // } + + // const result = Promise.all(modulePaths.map(modulePath => { + // if (modulePath.includes('vs/css!')) { + // // ESM/CSS when seeing the old `vs/css!` prefix we use that as a signal to + // // load CSS via a tag + // const cssModule = modulePath.replace('vs/css!', ''); + // const link = document.createElement('link'); + // link.rel = 'stylesheet'; + // link.href = new URL(`${cssModule}.css`, baseUrl).href; + // document.head.appendChild(link); + // return Promise.resolve(); + + // } else { + // // ESM/JS module loading + // return import(new URL(`${modulePath}.js`, baseUrl).href); + // } + // })); + + // result.then((res) => invokeResult(res[0]), onUnexpectedError); + // ESM-uncomment-end + + // ESM-comment-begin + /** @type {LoaderConfig} */ + const loaderConfig = { + baseUrl: `${fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out`, + preferScriptTags: true + }; + + // 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; } - - const ttp = window.trustedTypes?.createPolicy('vscode-bootstrapImportMap', { createScript(value) { return value; }, }); - const importMapSrc = JSON.stringify(importMap, undefined, 2); - const importMapScript = document.createElement('script'); - importMapScript.type = 'importmap'; - importMapScript.setAttribute('nonce', '0c6a828f1297'); - // @ts-ignore - importMapScript.textContent = ttp?.createScript(importMapSrc) ?? importMapSrc; - document.head.appendChild(importMapScript); - - performance.mark('code/didAddCssLoader'); + throw new Error(`Invalid script url: ${value}`); } + }); - const result = Promise.all(modulePaths.map(modulePath => { - if (modulePath.includes('vs/css!')) { - // ESM/CSS when seeing the old `vs/css!` prefix we use that as a signal to - // load CSS via a tag - const cssModule = modulePath.replace('vs/css!', ''); - const link = document.createElement('link'); - link.rel = 'stylesheet'; - link.href = new URL(`${cssModule}.css`, baseUrl).href; - document.head.appendChild(link); - return Promise.resolve(); + // Teach the loader the location of the node modules we use in renderers + // This will enable to load these modules via