From c0a4e9e84f97ade01840dbbdd0ab1d5ec675d235 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 24 Jun 2020 17:43:52 +0200 Subject: [PATCH] bootstrap - add helper utility for node.js things --- build/gulpfile.vscode.js | 1 + src/bootstrap-fork.js | 59 +++------------------------------------ src/bootstrap-node.js | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 55 deletions(-) create mode 100644 src/bootstrap-node.js diff --git a/build/gulpfile.vscode.js b/build/gulpfile.vscode.js index d93496f928c..9aa6aff5ea7 100644 --- a/build/gulpfile.vscode.js +++ b/build/gulpfile.vscode.js @@ -59,6 +59,7 @@ const vscodeResources = [ 'out-build/bootstrap.js', 'out-build/bootstrap-fork.js', 'out-build/bootstrap-amd.js', + 'out-build/bootstrap-node.js', 'out-build/bootstrap-window.js', 'out-build/paths.js', 'out-build/vs/**/*.{svg,png,html}', diff --git a/src/bootstrap-fork.js b/src/bootstrap-fork.js index c253c7a50a3..97a4d9934cd 100644 --- a/src/bootstrap-fork.js +++ b/src/bootstrap-fork.js @@ -7,15 +7,16 @@ 'use strict'; const bootstrap = require('./bootstrap'); +const bootstrapNode = require('./bootstrap-node'); // Remove global paths from the node module lookup -removeGlobalNodeModuleLookupPaths(); +bootstrapNode.removeGlobalNodeModuleLookupPaths(); // Enable ASAR in our forked processes bootstrap.enableASARSupport(); if (process.env['VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH']) { - injectNodeModuleLookupPath(process.env['VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH']); + bootstrapNode.injectNodeModuleLookupPath(process.env['VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH']); } // Configure: pipe logging to parent process @@ -39,61 +40,9 @@ configureCrashReporter(); // Load AMD entry point require('./bootstrap-amd').load(process.env['AMD_ENTRYPOINT']); + //#region Helpers -/** - * Add support for redirecting the loading of node modules - * - * @param {string} injectPath - */ -function injectNodeModuleLookupPath(injectPath) { - if (!injectPath) { - throw new Error('Missing injectPath'); - } - - const Module = require('module'); - const path = require('path'); - - const nodeModulesPath = path.join(__dirname, '../node_modules'); - - // @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; - } - } - } - - return paths; - }; -} - -function removeGlobalNodeModuleLookupPaths() { - const Module = require('module'); - // @ts-ignore - const globalPaths = Module.globalPaths; - - // @ts-ignore - const originalResolveLookupPaths = Module._resolveLookupPaths; - - // @ts-ignore - Module._resolveLookupPaths = function (moduleName, parent) { - const paths = originalResolveLookupPaths(moduleName, parent); - let commonSuffixLength = 0; - while (commonSuffixLength < paths.length && paths[paths.length - 1 - commonSuffixLength] === globalPaths[globalPaths.length - 1 - commonSuffixLength]) { - commonSuffixLength++; - } - return paths.slice(0, paths.length - commonSuffixLength); - }; -} - function pipeLoggingToParent() { const MAX_LENGTH = 100000; diff --git a/src/bootstrap-node.js b/src/bootstrap-node.js new file mode 100644 index 00000000000..2686249978d --- /dev/null +++ b/src/bootstrap-node.js @@ -0,0 +1,60 @@ +/*--------------------------------------------------------------------------------------------- + * 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'; + +/** + * Add support for redirecting the loading of node modules + * + * @param {string} injectPath + */ +exports.injectNodeModuleLookupPath = function (injectPath) { + if (!injectPath) { + throw new Error('Missing injectPath'); + } + + const Module = require('module'); + const path = require('path'); + + const nodeModulesPath = path.join(__dirname, '../node_modules'); + + // @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; + } + } + } + + return paths; + }; +}; + +exports.removeGlobalNodeModuleLookupPaths = function () { + const Module = require('module'); + // @ts-ignore + const globalPaths = Module.globalPaths; + + // @ts-ignore + const originalResolveLookupPaths = Module._resolveLookupPaths; + + // @ts-ignore + Module._resolveLookupPaths = function (moduleName, parent) { + const paths = originalResolveLookupPaths(moduleName, parent); + let commonSuffixLength = 0; + while (commonSuffixLength < paths.length && paths[paths.length - 1 - commonSuffixLength] === globalPaths[globalPaths.length - 1 - commonSuffixLength]) { + commonSuffixLength++; + } + return paths.slice(0, paths.length - commonSuffixLength); + }; +};