Skip to content

Commit c0a4e9e

Browse files
author
Benjamin Pasero
committed
bootstrap - add helper utility for node.js things
1 parent 4deff1a commit c0a4e9e

3 files changed

Lines changed: 65 additions & 55 deletions

File tree

build/gulpfile.vscode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const vscodeResources = [
5959
'out-build/bootstrap.js',
6060
'out-build/bootstrap-fork.js',
6161
'out-build/bootstrap-amd.js',
62+
'out-build/bootstrap-node.js',
6263
'out-build/bootstrap-window.js',
6364
'out-build/paths.js',
6465
'out-build/vs/**/*.{svg,png,html}',

src/bootstrap-fork.js

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
'use strict';
88

99
const bootstrap = require('./bootstrap');
10+
const bootstrapNode = require('./bootstrap-node');
1011

1112
// Remove global paths from the node module lookup
12-
removeGlobalNodeModuleLookupPaths();
13+
bootstrapNode.removeGlobalNodeModuleLookupPaths();
1314

1415
// Enable ASAR in our forked processes
1516
bootstrap.enableASARSupport();
1617

1718
if (process.env['VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH']) {
18-
injectNodeModuleLookupPath(process.env['VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH']);
19+
bootstrapNode.injectNodeModuleLookupPath(process.env['VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH']);
1920
}
2021

2122
// Configure: pipe logging to parent process
@@ -39,60 +40,8 @@ configureCrashReporter();
3940
// Load AMD entry point
4041
require('./bootstrap-amd').load(process.env['AMD_ENTRYPOINT']);
4142

42-
//#region Helpers
43-
44-
/**
45-
* Add support for redirecting the loading of node modules
46-
*
47-
* @param {string} injectPath
48-
*/
49-
function injectNodeModuleLookupPath(injectPath) {
50-
if (!injectPath) {
51-
throw new Error('Missing injectPath');
52-
}
53-
54-
const Module = require('module');
55-
const path = require('path');
56-
57-
const nodeModulesPath = path.join(__dirname, '../node_modules');
5843

59-
// @ts-ignore
60-
const originalResolveLookupPaths = Module._resolveLookupPaths;
61-
62-
// @ts-ignore
63-
Module._resolveLookupPaths = function (moduleName, parent) {
64-
const paths = originalResolveLookupPaths(moduleName, parent);
65-
if (Array.isArray(paths)) {
66-
for (let i = 0, len = paths.length; i < len; i++) {
67-
if (paths[i] === nodeModulesPath) {
68-
paths.splice(i, 0, injectPath);
69-
break;
70-
}
71-
}
72-
}
73-
74-
return paths;
75-
};
76-
}
77-
78-
function removeGlobalNodeModuleLookupPaths() {
79-
const Module = require('module');
80-
// @ts-ignore
81-
const globalPaths = Module.globalPaths;
82-
83-
// @ts-ignore
84-
const originalResolveLookupPaths = Module._resolveLookupPaths;
85-
86-
// @ts-ignore
87-
Module._resolveLookupPaths = function (moduleName, parent) {
88-
const paths = originalResolveLookupPaths(moduleName, parent);
89-
let commonSuffixLength = 0;
90-
while (commonSuffixLength < paths.length && paths[paths.length - 1 - commonSuffixLength] === globalPaths[globalPaths.length - 1 - commonSuffixLength]) {
91-
commonSuffixLength++;
92-
}
93-
return paths.slice(0, paths.length - commonSuffixLength);
94-
};
95-
}
44+
//#region Helpers
9645

9746
function pipeLoggingToParent() {
9847
const MAX_LENGTH = 100000;

src/bootstrap-node.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
//@ts-check
7+
'use strict';
8+
9+
/**
10+
* Add support for redirecting the loading of node modules
11+
*
12+
* @param {string} injectPath
13+
*/
14+
exports.injectNodeModuleLookupPath = function (injectPath) {
15+
if (!injectPath) {
16+
throw new Error('Missing injectPath');
17+
}
18+
19+
const Module = require('module');
20+
const path = require('path');
21+
22+
const nodeModulesPath = path.join(__dirname, '../node_modules');
23+
24+
// @ts-ignore
25+
const originalResolveLookupPaths = Module._resolveLookupPaths;
26+
27+
// @ts-ignore
28+
Module._resolveLookupPaths = function (moduleName, parent) {
29+
const paths = originalResolveLookupPaths(moduleName, parent);
30+
if (Array.isArray(paths)) {
31+
for (let i = 0, len = paths.length; i < len; i++) {
32+
if (paths[i] === nodeModulesPath) {
33+
paths.splice(i, 0, injectPath);
34+
break;
35+
}
36+
}
37+
}
38+
39+
return paths;
40+
};
41+
};
42+
43+
exports.removeGlobalNodeModuleLookupPaths = function () {
44+
const Module = require('module');
45+
// @ts-ignore
46+
const globalPaths = Module.globalPaths;
47+
48+
// @ts-ignore
49+
const originalResolveLookupPaths = Module._resolveLookupPaths;
50+
51+
// @ts-ignore
52+
Module._resolveLookupPaths = function (moduleName, parent) {
53+
const paths = originalResolveLookupPaths(moduleName, parent);
54+
let commonSuffixLength = 0;
55+
while (commonSuffixLength < paths.length && paths[paths.length - 1 - commonSuffixLength] === globalPaths[globalPaths.length - 1 - commonSuffixLength]) {
56+
commonSuffixLength++;
57+
}
58+
return paths.slice(0, paths.length - commonSuffixLength);
59+
};
60+
};

0 commit comments

Comments
 (0)