From f0195104abd5ba9e95bb54e0789c30bb09cb748d Mon Sep 17 00:00:00 2001 From: Alexandru Dima Date: Fri, 24 Feb 2023 22:02:49 +0100 Subject: [PATCH] Adopt latest loader (#175379) * Adopt latest loader * Update to latest loader --- src/vs/loader.js | 1154 ++++++++++++++++++++++------------------------ 1 file changed, 544 insertions(+), 610 deletions(-) diff --git a/src/vs/loader.js b/src/vs/loader.js index 67ef8665099..5c71e7aacf5 100644 --- a/src/vs/loader.js +++ b/src/vs/loader.js @@ -18,13 +18,33 @@ *--------------------------------------------------------------------------------------------- *--------------------------------------------------------------------------------------------- *--------------------------------------------------------------------------------------------*/ -var _amdLoaderGlobal = this; -var _commonjsGlobal = typeof global === 'object' ? global : {}; +const _amdLoaderGlobal = this; +const _commonjsGlobal = typeof global === 'object' ? global : {}; var AMDLoader; (function (AMDLoader) { AMDLoader.global = _amdLoaderGlobal; - var Environment = /** @class */ (function () { - function Environment() { + class Environment { + get isWindows() { + this._detect(); + return this._isWindows; + } + get isNode() { + this._detect(); + return this._isNode; + } + get isElectronRenderer() { + this._detect(); + return this._isElectronRenderer; + } + get isWebWorker() { + this._detect(); + return this._isWebWorker; + } + get isElectronNodeIntegrationWebWorker() { + this._detect(); + return this._isElectronNodeIntegrationWebWorker; + } + constructor() { this._detected = false; this._isWindows = false; this._isNode = false; @@ -32,47 +52,7 @@ var AMDLoader; this._isWebWorker = false; this._isElectronNodeIntegrationWebWorker = false; } - Object.defineProperty(Environment.prototype, "isWindows", { - get: function () { - this._detect(); - return this._isWindows; - }, - enumerable: false, - configurable: true - }); - Object.defineProperty(Environment.prototype, "isNode", { - get: function () { - this._detect(); - return this._isNode; - }, - enumerable: false, - configurable: true - }); - Object.defineProperty(Environment.prototype, "isElectronRenderer", { - get: function () { - this._detect(); - return this._isElectronRenderer; - }, - enumerable: false, - configurable: true - }); - Object.defineProperty(Environment.prototype, "isWebWorker", { - get: function () { - this._detect(); - return this._isWebWorker; - }, - enumerable: false, - configurable: true - }); - Object.defineProperty(Environment.prototype, "isElectronNodeIntegrationWebWorker", { - get: function () { - this._detect(); - return this._isElectronNodeIntegrationWebWorker; - }, - enumerable: false, - configurable: true - }); - Environment.prototype._detect = function () { + _detect() { if (this._detected) { return; } @@ -82,8 +62,8 @@ var AMDLoader; this._isElectronRenderer = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer'); this._isWebWorker = (typeof AMDLoader.global.importScripts === 'function'); this._isElectronNodeIntegrationWebWorker = this._isWebWorker && (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'worker'); - }; - Environment._isWindows = function () { + } + static _isWindows() { if (typeof navigator !== 'undefined') { if (navigator.userAgent && navigator.userAgent.indexOf('Windows') >= 0) { return true; @@ -93,9 +73,8 @@ var AMDLoader; return (process.platform === 'win32'); } return false; - }; - return Environment; - }()); + } + } AMDLoader.Environment = Environment; })(AMDLoader || (AMDLoader = {})); /*--------------------------------------------------------------------------------------------- @@ -104,40 +83,35 @@ var AMDLoader; *--------------------------------------------------------------------------------------------*/ var AMDLoader; (function (AMDLoader) { - var LoaderEvent = /** @class */ (function () { - function LoaderEvent(type, detail, timestamp) { + class LoaderEvent { + constructor(type, detail, timestamp) { this.type = type; this.detail = detail; this.timestamp = timestamp; } - return LoaderEvent; - }()); + } AMDLoader.LoaderEvent = LoaderEvent; - var LoaderEventRecorder = /** @class */ (function () { - function LoaderEventRecorder(loaderAvailableTimestamp) { - this._events = [new LoaderEvent(1 /* LoaderAvailable */, '', loaderAvailableTimestamp)]; + class LoaderEventRecorder { + constructor(loaderAvailableTimestamp) { + this._events = [new LoaderEvent(1 /* LoaderEventType.LoaderAvailable */, '', loaderAvailableTimestamp)]; } - LoaderEventRecorder.prototype.record = function (type, detail) { + record(type, detail) { this._events.push(new LoaderEvent(type, detail, AMDLoader.Utilities.getHighPerformanceTimestamp())); - }; - LoaderEventRecorder.prototype.getEvents = function () { - return this._events; - }; - return LoaderEventRecorder; - }()); - AMDLoader.LoaderEventRecorder = LoaderEventRecorder; - var NullLoaderEventRecorder = /** @class */ (function () { - function NullLoaderEventRecorder() { } - NullLoaderEventRecorder.prototype.record = function (type, detail) { + getEvents() { + return this._events; + } + } + AMDLoader.LoaderEventRecorder = LoaderEventRecorder; + class NullLoaderEventRecorder { + record(type, detail) { // Nothing to do - }; - NullLoaderEventRecorder.prototype.getEvents = function () { + } + getEvents() { return []; - }; - NullLoaderEventRecorder.INSTANCE = new NullLoaderEventRecorder(); - return NullLoaderEventRecorder; - }()); + } + } + NullLoaderEventRecorder.INSTANCE = new NullLoaderEventRecorder(); AMDLoader.NullLoaderEventRecorder = NullLoaderEventRecorder; })(AMDLoader || (AMDLoader = {})); /*--------------------------------------------------------------------------------------------- @@ -146,13 +120,11 @@ var AMDLoader; *--------------------------------------------------------------------------------------------*/ var AMDLoader; (function (AMDLoader) { - var Utilities = /** @class */ (function () { - function Utilities() { - } + class Utilities { /** * This method does not take care of / vs \ */ - Utilities.fileUriToFilePath = function (isWindows, uri) { + static fileUriToFilePath(isWindows, uri) { uri = decodeURI(uri).replace(/%23/g, '#'); if (isWindows) { if (/^file:\/\/\//.test(uri)) { @@ -170,41 +142,41 @@ var AMDLoader; } // Not sure... return uri; - }; - Utilities.startsWith = function (haystack, needle) { + } + static startsWith(haystack, needle) { return haystack.length >= needle.length && haystack.substr(0, needle.length) === needle; - }; - Utilities.endsWith = function (haystack, needle) { + } + static endsWith(haystack, needle) { return haystack.length >= needle.length && haystack.substr(haystack.length - needle.length) === needle; - }; + } // only check for "?" before "#" to ensure that there is a real Query-String - Utilities.containsQueryString = function (url) { + static containsQueryString(url) { return /^[^\#]*\?/gi.test(url); - }; + } /** * Does `url` start with http:// or https:// or file:// or / ? */ - Utilities.isAbsolutePath = function (url) { + static isAbsolutePath(url) { return /^((http:\/\/)|(https:\/\/)|(file:\/\/)|(\/))/.test(url); - }; - Utilities.forEachProperty = function (obj, callback) { + } + static forEachProperty(obj, callback) { if (obj) { - var key = void 0; + let key; for (key in obj) { if (obj.hasOwnProperty(key)) { callback(key, obj[key]); } } } - }; - Utilities.isEmpty = function (obj) { - var isEmpty = true; - Utilities.forEachProperty(obj, function () { + } + static isEmpty(obj) { + let isEmpty = true; + Utilities.forEachProperty(obj, () => { isEmpty = false; }); return isEmpty; - }; - Utilities.recursiveClone = function (obj) { + } + static recursiveClone(obj) { if (!obj || typeof obj !== 'object' || obj instanceof RegExp) { return obj; } @@ -212,8 +184,8 @@ var AMDLoader; // only clone "simple" objects return obj; } - var result = Array.isArray(obj) ? [] : {}; - Utilities.forEachProperty(obj, function (key, value) { + let result = Array.isArray(obj) ? [] : {}; + Utilities.forEachProperty(obj, (key, value) => { if (value && typeof value === 'object') { result[key] = Utilities.recursiveClone(value); } @@ -222,25 +194,24 @@ var AMDLoader; } }); return result; - }; - Utilities.generateAnonymousModule = function () { + } + static generateAnonymousModule() { return '===anonymous' + (Utilities.NEXT_ANONYMOUS_ID++) + '==='; - }; - Utilities.isAnonymousModule = function (id) { + } + static isAnonymousModule(id) { return Utilities.startsWith(id, '===anonymous'); - }; - Utilities.getHighPerformanceTimestamp = function () { + } + static getHighPerformanceTimestamp() { if (!this.PERFORMANCE_NOW_PROBED) { this.PERFORMANCE_NOW_PROBED = true; this.HAS_PERFORMANCE_NOW = (AMDLoader.global.performance && typeof AMDLoader.global.performance.now === 'function'); } return (this.HAS_PERFORMANCE_NOW ? AMDLoader.global.performance.now() : Date.now()); - }; - Utilities.NEXT_ANONYMOUS_ID = 1; - Utilities.PERFORMANCE_NOW_PROBED = false; - Utilities.HAS_PERFORMANCE_NOW = false; - return Utilities; - }()); + } + } + Utilities.NEXT_ANONYMOUS_ID = 1; + Utilities.PERFORMANCE_NOW_PROBED = false; + Utilities.HAS_PERFORMANCE_NOW = false; AMDLoader.Utilities = Utilities; })(AMDLoader || (AMDLoader = {})); /*--------------------------------------------------------------------------------------------- @@ -253,7 +224,7 @@ var AMDLoader; if (err instanceof Error) { return err; } - var result = new Error(err.message || String(err) || 'Unknown Error'); + const result = new Error(err.message || String(err) || 'Unknown Error'); if (err.stack) { result.stack = err.stack; } @@ -261,13 +232,11 @@ var AMDLoader; } AMDLoader.ensureError = ensureError; ; - var ConfigurationOptionsUtil = /** @class */ (function () { - function ConfigurationOptionsUtil() { - } + class ConfigurationOptionsUtil { /** * Ensure configuration options make sense */ - ConfigurationOptionsUtil.validateConfigurationOptions = function (options) { + static validateConfigurationOptions(options) { function defaultOnError(err) { if (err.phase === 'loading') { console.error('Loading "' + err.moduleId + '" failed'); @@ -331,138 +300,134 @@ var AMDLoader; options.nodeCachedData.writeDelay = 1000 * 7; } if (!options.nodeCachedData.path || typeof options.nodeCachedData.path !== 'string') { - var err = ensureError(new Error('INVALID cached data configuration, \'path\' MUST be set')); + const err = ensureError(new Error('INVALID cached data configuration, \'path\' MUST be set')); err.phase = 'configuration'; options.onError(err); options.nodeCachedData = undefined; } } return options; - }; - ConfigurationOptionsUtil.mergeConfigurationOptions = function (overwrite, base) { - if (overwrite === void 0) { overwrite = null; } - if (base === void 0) { base = null; } - var result = AMDLoader.Utilities.recursiveClone(base || {}); + } + static mergeConfigurationOptions(overwrite = null, base = null) { + let result = AMDLoader.Utilities.recursiveClone(base || {}); // Merge known properties and overwrite the unknown ones - AMDLoader.Utilities.forEachProperty(overwrite, function (key, value) { + AMDLoader.Utilities.forEachProperty(overwrite, (key, value) => { if (key === 'ignoreDuplicateModules' && typeof result.ignoreDuplicateModules !== 'undefined') { result.ignoreDuplicateModules = result.ignoreDuplicateModules.concat(value); } else if (key === 'paths' && typeof result.paths !== 'undefined') { - AMDLoader.Utilities.forEachProperty(value, function (key2, value2) { return result.paths[key2] = value2; }); + AMDLoader.Utilities.forEachProperty(value, (key2, value2) => result.paths[key2] = value2); } else if (key === 'config' && typeof result.config !== 'undefined') { - AMDLoader.Utilities.forEachProperty(value, function (key2, value2) { return result.config[key2] = value2; }); + AMDLoader.Utilities.forEachProperty(value, (key2, value2) => result.config[key2] = value2); } else { result[key] = AMDLoader.Utilities.recursiveClone(value); } }); return ConfigurationOptionsUtil.validateConfigurationOptions(result); - }; - return ConfigurationOptionsUtil; - }()); + } + } AMDLoader.ConfigurationOptionsUtil = ConfigurationOptionsUtil; - var Configuration = /** @class */ (function () { - function Configuration(env, options) { + class Configuration { + constructor(env, options) { this._env = env; this.options = ConfigurationOptionsUtil.mergeConfigurationOptions(options); this._createIgnoreDuplicateModulesMap(); this._createSortedPathsRules(); if (this.options.baseUrl === '') { if (this.options.nodeRequire && this.options.nodeRequire.main && this.options.nodeRequire.main.filename && this._env.isNode) { - var nodeMain = this.options.nodeRequire.main.filename; - var dirnameIndex = Math.max(nodeMain.lastIndexOf('/'), nodeMain.lastIndexOf('\\')); + let nodeMain = this.options.nodeRequire.main.filename; + let dirnameIndex = Math.max(nodeMain.lastIndexOf('/'), nodeMain.lastIndexOf('\\')); this.options.baseUrl = nodeMain.substring(0, dirnameIndex + 1); } } } - Configuration.prototype._createIgnoreDuplicateModulesMap = function () { + _createIgnoreDuplicateModulesMap() { // Build a map out of the ignoreDuplicateModules array this.ignoreDuplicateModulesMap = {}; - for (var i = 0; i < this.options.ignoreDuplicateModules.length; i++) { + for (let i = 0; i < this.options.ignoreDuplicateModules.length; i++) { this.ignoreDuplicateModulesMap[this.options.ignoreDuplicateModules[i]] = true; } - }; - Configuration.prototype._createSortedPathsRules = function () { - var _this = this; + } + _createSortedPathsRules() { // Create an array our of the paths rules, sorted descending by length to // result in a more specific -> less specific order this.sortedPathsRules = []; - AMDLoader.Utilities.forEachProperty(this.options.paths, function (from, to) { + AMDLoader.Utilities.forEachProperty(this.options.paths, (from, to) => { if (!Array.isArray(to)) { - _this.sortedPathsRules.push({ + this.sortedPathsRules.push({ from: from, to: [to] }); } else { - _this.sortedPathsRules.push({ + this.sortedPathsRules.push({ from: from, to: to }); } }); - this.sortedPathsRules.sort(function (a, b) { + this.sortedPathsRules.sort((a, b) => { return b.from.length - a.from.length; }); - }; + } /** * Clone current configuration and overwrite options selectively. * @param options The selective options to overwrite with. * @result A new configuration */ - Configuration.prototype.cloneAndMerge = function (options) { + cloneAndMerge(options) { return new Configuration(this._env, ConfigurationOptionsUtil.mergeConfigurationOptions(options, this.options)); - }; + } /** * Get current options bag. Useful for passing it forward to plugins. */ - Configuration.prototype.getOptionsLiteral = function () { + getOptionsLiteral() { return this.options; - }; - Configuration.prototype._applyPaths = function (moduleId) { - var pathRule; - for (var i = 0, len = this.sortedPathsRules.length; i < len; i++) { + } + _applyPaths(moduleId) { + let pathRule; + for (let i = 0, len = this.sortedPathsRules.length; i < len; i++) { pathRule = this.sortedPathsRules[i]; if (AMDLoader.Utilities.startsWith(moduleId, pathRule.from)) { - var result = []; - for (var j = 0, lenJ = pathRule.to.length; j < lenJ; j++) { + let result = []; + for (let j = 0, lenJ = pathRule.to.length; j < lenJ; j++) { result.push(pathRule.to[j] + moduleId.substr(pathRule.from.length)); } return result; } } return [moduleId]; - }; - Configuration.prototype._addUrlArgsToUrl = function (url) { + } + _addUrlArgsToUrl(url) { if (AMDLoader.Utilities.containsQueryString(url)) { return url + '&' + this.options.urlArgs; } else { return url + '?' + this.options.urlArgs; } - }; - Configuration.prototype._addUrlArgsIfNecessaryToUrl = function (url) { + } + _addUrlArgsIfNecessaryToUrl(url) { if (this.options.urlArgs) { return this._addUrlArgsToUrl(url); } return url; - }; - Configuration.prototype._addUrlArgsIfNecessaryToUrls = function (urls) { + } + _addUrlArgsIfNecessaryToUrls(urls) { if (this.options.urlArgs) { - for (var i = 0, len = urls.length; i < len; i++) { + for (let i = 0, len = urls.length; i < len; i++) { urls[i] = this._addUrlArgsToUrl(urls[i]); } } return urls; - }; + } /** * Transform a module id to a location. Appends .js to module ids */ - Configuration.prototype.moduleIdToPaths = function (moduleId) { + moduleIdToPaths(moduleId) { if (this._env.isNode) { - var isNodeModule = (this.options.amdModulesPattern instanceof RegExp + const isNodeModule = (this.options.amdModulesPattern instanceof RegExp && !this.options.amdModulesPattern.test(moduleId)); if (isNodeModule) { // This is a node module... @@ -476,11 +441,11 @@ var AMDLoader; } } } - var result = moduleId; - var results; + let result = moduleId; + let results; if (!AMDLoader.Utilities.endsWith(result, '.js') && !AMDLoader.Utilities.isAbsolutePath(result)) { results = this._applyPaths(result); - for (var i = 0, len = results.length; i < len; i++) { + for (let i = 0, len = results.length; i < len; i++) { if (this.isBuild() && results[i] === 'empty:') { continue; } @@ -499,12 +464,12 @@ var AMDLoader; results = [result]; } return this._addUrlArgsIfNecessaryToUrls(results); - }; + } /** * Transform a module id or url to a location. */ - Configuration.prototype.requireToUrl = function (url) { - var result = url; + requireToUrl(url) { + let result = url; if (!AMDLoader.Utilities.isAbsolutePath(result)) { result = this._applyPaths(result)[0]; if (!AMDLoader.Utilities.isAbsolutePath(result)) { @@ -512,14 +477,14 @@ var AMDLoader; } } return this._addUrlArgsIfNecessaryToUrl(result); - }; + } /** * Flag to indicate if current execution is as part of a build. */ - Configuration.prototype.isBuild = function () { + isBuild() { return this.options.isBuild; - }; - Configuration.prototype.shouldInvokeFactory = function (strModuleId) { + } + shouldInvokeFactory(strModuleId) { if (!this.options.isBuild) { // outside of a build, all factories should be invoked return true; @@ -532,41 +497,40 @@ var AMDLoader; return true; } return false; - }; + } /** * Test if module `moduleId` is expected to be defined multiple times */ - Configuration.prototype.isDuplicateMessageIgnoredFor = function (moduleId) { + isDuplicateMessageIgnoredFor(moduleId) { return this.ignoreDuplicateModulesMap.hasOwnProperty(moduleId); - }; + } /** * Get the configuration settings for the provided module id */ - Configuration.prototype.getConfigForModule = function (moduleId) { + getConfigForModule(moduleId) { if (this.options.config) { return this.options.config[moduleId]; } - }; + } /** * Should errors be caught when executing module factories? */ - Configuration.prototype.shouldCatchError = function () { + shouldCatchError() { return this.options.catchError; - }; + } /** * Should statistics be recorded? */ - Configuration.prototype.shouldRecordStats = function () { + shouldRecordStats() { return this.options.recordStats; - }; + } /** * Forward an error to the error handler. */ - Configuration.prototype.onError = function (err) { + onError(err) { this.options.onError(err); - }; - return Configuration; - }()); + } + } AMDLoader.Configuration = Configuration; })(AMDLoader || (AMDLoader = {})); /*--------------------------------------------------------------------------------------------- @@ -578,20 +542,19 @@ var AMDLoader; /** * Load `scriptSrc` only once (avoid multiple