Skip to content

Commit b8710fb

Browse files
atscottKeen Yee Liau
authored andcommitted
build: switch to esbuild for all compilation
This commit configures the esbuild output of for the banner and server to be in a format that will work for the extension. It mimics the sort of behavior that we would get in the amd format (which is not supported by esbuild) by simply overriding the built-in `require` function with the function which is capable of resolving typescript at runtime from the location passed in via the arguments. Fixes #1123
1 parent ef5297d commit b8710fb

File tree

5 files changed

+38
-122
lines changed

5 files changed

+38
-122
lines changed

esbuild.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const esbuild = require('esbuild');
22
const fs = require('fs');
33

4+
/** @type esbuild.BuildOptions */
45
const defaultOptions = {
56
bundle: true,
67
platform: 'node',
78
logLevel: 'info',
9+
format: 'cjs',
810
};
911

1012
/** @type esbuild.BuildOptions */
@@ -20,7 +22,6 @@ const clientConfig = {
2022
'vscode-languageserver-protocol',
2123
'vscode-jsonrpc',
2224
],
23-
format: 'cjs',
2425
// Do not enable minification. It seems to break the extension on Windows (with WSL). See #1198.
2526
minify: false,
2627
};
@@ -33,7 +34,10 @@ const bannerConfig = {
3334
external: [
3435
'path',
3536
],
36-
format: 'cjs',
37+
// This is described in more detail in the `server/banner.ts` but this line actually overrides
38+
// the built-in `require` function by adding a line at the bottom of the generated banner code
39+
// to assign the override function to the `require` name.
40+
footer: {js: 'require = requireOverride;'}
3741
};
3842

3943
/** @type esbuild.BuildOptions */
@@ -49,16 +53,16 @@ const serverConfig = {
4953
'vscode-uri',
5054
'vscode-jsonrpc',
5155
],
52-
// TODO(atscott): Figure out how to use the banner correctly.
53-
// iife format produces a `require("typescript/lib/tsserverlibrary");` line but it needs to use
54-
// the `define` in the banner to resolve tsserverlibrary.
55-
format: 'iife',
5656
};
5757

5858
async function build() {
5959
try {
6060
await esbuild.build(clientConfig);
6161
await esbuild.build(bannerConfig);
62+
await esbuild.build({
63+
...serverConfig,
64+
banner: {js: fs.readFileSync('dist/banner/banner.esbuild.js', 'utf8')},
65+
});
6266
} catch (e) {
6367
console.error(e);
6468
process.exit(1);

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
],
137137
"main": "./dist/client/extension",
138138
"scripts": {
139-
"compile": "tsc -p server/banner.tsconfig.json && tsc -b && node esbuild.js && rollup -c",
139+
"compile": "tsc -p server/banner.tsconfig.json && tsc -b && node esbuild.js",
140140
"compile:test": "tsc -b server/src/tests && tsc -b client/src/tests",
141141
"compile:integration": "tsc -b integration",
142142
"compile:syntaxes-test": "tsc -b syntaxes/test",
@@ -166,8 +166,6 @@
166166
"esbuild": "^0.9.0",
167167
"jasmine": "3.6.4",
168168
"prettier": "2.2.1",
169-
"rollup": "2.38.5",
170-
"rollup-plugin-commonjs": "10.1.0",
171169
"tslint": "6.1.3",
172170
"tslint-eslint-rules": "5.4.0",
173171
"vsce": "1.85.0",
@@ -180,4 +178,4 @@
180178
"type": "git",
181179
"url": "https://github.com/angular/vscode-ng-language-service"
182180
}
183-
}
181+
}

rollup.config.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

server/src/banner.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import {parseCommandLine} from './cmdline_utils';
22
import {resolveTsServer} from './version_provider';
33

4+
const originalRequire = require;
5+
46
/**
5-
* This method provides a custom implementation for the AMD loader to resolve
6-
* `typescript` module at runtime.
7-
* @param modules modules to resolve
8-
* @param cb function to invoke with resolved modules
7+
* This method provides a custom implementation for the require function to resolve
8+
* `typescript` module at runtime. We provide this as an override to the built-in
9+
* 'require' method using the banner funcionality of esbuild. That is, esbuild will
10+
* compile the server and add this banner to the top of the compilation so any place
11+
* in the server code that uses `require` will get routed through this override.
12+
*
13+
* Refer also to `esbuild.js`, the `bannerConfig` which overrides the `require` using
14+
* the `footer` option, and the `serverConfig` which provides the banner code at the top
15+
* of the server output using the `banner` option.
16+
*
17+
* @param moduleName The module to resolve
18+
* @returns
919
*/
10-
export function define(modules: string[], cb: (...modules: any[]) => void) {
20+
export function requireOverride(moduleName: string) {
1121
const TSSERVER = 'typescript/lib/tsserverlibrary';
12-
const resolvedModules = modules.map(m => {
13-
if (m === 'typescript') {
14-
throw new Error(`Import '${TSSERVER}' instead of 'typescript'`);
15-
}
16-
if (m === TSSERVER) {
17-
const {tsProbeLocations} = parseCommandLine(process.argv);
18-
m = resolveTsServer(tsProbeLocations).resolvedPath;
19-
}
20-
return require(m);
21-
});
22-
cb(...resolvedModules);
22+
if (moduleName === 'typescript') {
23+
throw new Error(`Import '${TSSERVER}' instead of 'typescript'`);
24+
}
25+
if (moduleName === TSSERVER) {
26+
const {tsProbeLocations} = parseCommandLine(process.argv);
27+
moduleName = resolveTsServer(tsProbeLocations).resolvedPath;
28+
}
29+
return originalRequire(moduleName);
2330
}
31+
32+
requireOverride.resolve = originalRequire.resolve;

yarn.lock

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
3434
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
3535

36-
"@types/estree@*":
37-
version "0.0.46"
38-
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.46.tgz#0fb6bfbbeabd7a30880504993369c4bf1deab1fe"
39-
integrity sha512-laIjwTQaD+5DukBZaygQ79K1Z0jb1bPEMRrkXSLjtCcZm+abyp5YbrqpSLzD42FwWW6gK/aS4NYpJ804nG2brg==
40-
4136
4237
version "3.6.3"
4338
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-3.6.3.tgz#824df555b8a5114f91619e78d8f59624d6f23050"
@@ -346,11 +341,6 @@ esprima@^4.0.0:
346341
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
347342
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
348343

349-
estree-walker@^0.6.1:
350-
version "0.6.1"
351-
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
352-
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==
353-
354344
esutils@^1.1.6:
355345
version "1.1.6"
356346
resolved "https://registry.yarnpkg.com/esutils/-/esutils-1.1.6.tgz#c01ccaa9ae4b897c6d0c3e210ae52f3c7a844375"
@@ -368,11 +358,6 @@ fs.realpath@^1.0.0:
368358
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
369359
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
370360

371-
fsevents@~2.3.1:
372-
version "2.3.2"
373-
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
374-
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
375-
376361
function-bind@^1.1.1:
377362
version "1.1.1"
378363
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
@@ -482,20 +467,13 @@ inherits@2, inherits@^2.0.1, inherits@^2.0.3:
482467
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
483468
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
484469

485-
is-core-module@^2.1.0, is-core-module@^2.2.0:
470+
is-core-module@^2.1.0:
486471
version "2.2.0"
487472
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.2.0.tgz#97037ef3d52224d85163f5597b2b63d9afed981a"
488473
integrity sha512-XRAfAdyyY5F5cOXn7hYQDqh2Xmii+DEfIcQGxK/uNwMHhIkPWO0g8msXcbzLe+MpGoR951MlqM/2iIlU4vKDdQ==
489474
dependencies:
490475
has "^1.0.3"
491476

492-
is-reference@^1.1.2:
493-
version "1.2.1"
494-
resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.2.1.tgz#8b2dac0b371f4bc994fdeaba9eb542d03002d0b7"
495-
integrity sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==
496-
dependencies:
497-
"@types/estree" "*"
498-
499477
500478
version "0.0.1"
501479
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
@@ -551,13 +529,6 @@ lru-cache@^6.0.0:
551529
dependencies:
552530
yallist "^4.0.0"
553531

554-
magic-string@^0.25.2:
555-
version "0.25.7"
556-
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
557-
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
558-
dependencies:
559-
sourcemap-codec "^1.4.4"
560-
561532
markdown-it@^10.0.0:
562533
version "10.0.0"
563534
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-10.0.0.tgz#abfc64f141b1722d663402044e43927f1f50a8dc"
@@ -754,39 +725,6 @@ resolve@^1.1.6, resolve@^1.3.2:
754725
is-core-module "^2.1.0"
755726
path-parse "^1.0.6"
756727

757-
resolve@^1.11.0:
758-
version "1.20.0"
759-
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
760-
integrity sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==
761-
dependencies:
762-
is-core-module "^2.2.0"
763-
path-parse "^1.0.6"
764-
765-
766-
version "10.1.0"
767-
resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-10.1.0.tgz#417af3b54503878e084d127adf4d1caf8beb86fb"
768-
integrity sha512-jlXbjZSQg8EIeAAvepNwhJj++qJWNJw1Cl0YnOqKtP5Djx+fFGkp3WRh+W0ASCaFG5w1jhmzDxgu3SJuVxPF4Q==
769-
dependencies:
770-
estree-walker "^0.6.1"
771-
is-reference "^1.1.2"
772-
magic-string "^0.25.2"
773-
resolve "^1.11.0"
774-
rollup-pluginutils "^2.8.1"
775-
776-
rollup-pluginutils@^2.8.1:
777-
version "2.8.2"
778-
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"
779-
integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==
780-
dependencies:
781-
estree-walker "^0.6.1"
782-
783-
784-
version "2.38.5"
785-
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.38.5.tgz#be41ad4fe0c103a8794377afceb5f22b8f603d6a"
786-
integrity sha512-VoWt8DysFGDVRGWuHTqZzT02J0ASgjVq/hPs9QcBOGMd7B+jfTr/iqMVEyOi901rE3xq+Deq66GzIT1yt7sGwQ==
787-
optionalDependencies:
788-
fsevents "~2.3.1"
789-
790728
safe-buffer@~5.2.0:
791729
version "5.2.1"
792730
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
@@ -817,11 +755,6 @@ source-map@^0.6.0:
817755
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
818756
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
819757

820-
sourcemap-codec@^1.4.4:
821-
version "1.4.8"
822-
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
823-
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
824-
825758
sprintf-js@~1.0.2:
826759
version "1.0.3"
827760
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"

0 commit comments

Comments
 (0)