Skip to content

Commit acf9826

Browse files
committed
refactor: update language-service package and tests to work with ESM (#48521)
The Angular Language Service package is now tested & compiled using ESM. Previously it was a mismatch of CommonJS and ESM. Still the language-service ESM output is transformed into a single UMD bundle to allow for module resolution to be overriden. See `bundles/BUILD`. This is kept as is. To fully ship ESM (language-service is an exception here), we need to: * Update all code to no longer reference typescript via import. Instead typescript needs to be passed around so that the extension can control the version * The VSCode extension/ the TS server needs to be able to load ESM. It looks like the server supports ESM global plugins, but the extension might not yet. This is out of scope for the dev-infra effort as it requires more insight into VSCode & the extension system & the TS language server. PR Close #48521
1 parent 04e9696 commit acf9826

25 files changed

+84
-37
lines changed
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
1-
load("//tools:defaults.bzl", "pkg_npm", "ts_library")
1+
load("//tools:defaults.bzl", "esbuild", "extract_types", "pkg_npm", "ts_library")
22

33
package(default_visibility = ["//visibility:public"])
44

55
ts_library(
66
name = "api",
77
srcs = [
88
"api.ts",
9-
"index.ts",
10-
"override_rename_ts_plugin.ts",
119
],
12-
prodmode_module = "commonjs",
1310
deps = [
11+
"@npm//typescript",
12+
],
13+
)
14+
15+
ts_library(
16+
name = "factory_lib",
17+
srcs = ["plugin-factory.ts"],
18+
deps = [
19+
":api",
1420
"@npm//@types/node",
1521
"@npm//typescript",
1622
],
1723
)
1824

25+
esbuild(
26+
name = "factory_bundle",
27+
entry_point = ":plugin-factory.ts",
28+
external = ["./bundles/language-service.js"],
29+
format = "cjs",
30+
deps = [":factory_lib"],
31+
)
32+
33+
extract_types(
34+
name = "factory_types",
35+
deps = [":factory_lib"],
36+
)
37+
1938
pkg_npm(
2039
name = "npm_package",
2140
package_name = "@angular/language-service",
22-
srcs = ["package.json"],
41+
srcs = [
42+
"index.d.ts",
43+
"index.js",
44+
"package.json",
45+
],
2346
tags = [
2447
"release-with-framework",
2548
],
@@ -32,8 +55,8 @@ pkg_npm(
3255
"//integration:__subpackages__",
3356
],
3457
deps = [
35-
":api",
36-
# min bundle is not used at the moment; omit from package to speed up build
58+
":factory_bundle",
59+
":factory_types",
3760
"//packages/language-service/bundles:language-service.js",
3861
],
3962
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {factory} from './plugin-factory';
10+
11+
// Tsserver expects `@angular/language-service` to provide a factory function
12+
// as the default export of the package.
13+
export = factory;

packages/language-service/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
const {factory} = require('./factory_bundle');
10+
11+
// Tsserver expects `@angular/language-service` to provide a factory function
12+
// as the default export of the package.
13+
module.exports = factory;

packages/language-service/override_rename_ts_plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as ts from 'typescript/lib/tsserverlibrary';
9+
import ts from 'typescript/lib/tsserverlibrary';
1010

1111
function isAngularCore(path: string): boolean {
1212
return isExternalAngularCore(path) || isInternalAngularCore(path);

packages/language-service/index.ts renamed to packages/language-service/plugin-factory.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as ts from 'typescript/lib/tsserverlibrary';
9+
import ts from 'typescript/lib/tsserverlibrary';
10+
1011
import {NgLanguageService, PluginConfig} from './api';
1112

1213
interface PluginModule extends ts.server.PluginModule {
1314
create(createInfo: ts.server.PluginCreateInfo): NgLanguageService;
1415
onConfigurationChanged?(config: PluginConfig): void;
1516
}
1617

17-
const factory: ts.server.PluginModuleFactory = (tsModule): PluginModule => {
18+
export const factory: ts.server.PluginModuleFactory = (tsModule): PluginModule => {
1819
let plugin: PluginModule;
1920

2021
return {
@@ -30,10 +31,3 @@ const factory: ts.server.PluginModuleFactory = (tsModule): PluginModule => {
3031
},
3132
};
3233
};
33-
34-
/**
35-
* Tsserver expects `@angular/language-service` to provide a factory function
36-
* as the default export of the package. See
37-
* https://github.com/microsoft/TypeScript/blob/f4d0ea6539edb6d8f70b626132d6f9ac1ac4281a/src/server/project.ts#L1611
38-
*/
39-
export = factory;

packages/language-service/src/adapters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {AbsoluteFsPath, FileStats, PathSegment, PathString} from '@angular/compi
1414
import {isShim} from '@angular/compiler-cli/src/ngtsc/shims';
1515
import {getRootDirs} from '@angular/compiler-cli/src/ngtsc/util/src/typescript';
1616
import * as p from 'path';
17-
import * as ts from 'typescript/lib/tsserverlibrary';
17+
import ts from 'typescript/lib/tsserverlibrary';
1818

1919
import {isTypeScriptFile} from './utils';
2020

packages/language-service/src/codefixes/code_fixes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
10-
import * as tss from 'typescript/lib/tsserverlibrary';
10+
import tss from 'typescript/lib/tsserverlibrary';
1111

1212
import {TemplateInfo} from '../utils';
1313

packages/language-service/src/codefixes/fix_invalid_banana_in_box.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {ErrorCode, ngErrorCode} from '@angular/compiler-cli/src/ngtsc/diagnostics';
1010
import {BoundEvent} from '@angular/compiler/src/render3/r3_ast';
11-
import * as tss from 'typescript/lib/tsserverlibrary';
11+
import tss from 'typescript/lib/tsserverlibrary';
1212

1313
import {getTargetAtPosition, TargetNodeKind} from '../template_target';
1414
import {getTemplateInfoAtPosition, TemplateInfo} from '../utils';

packages/language-service/src/codefixes/fix_missing_member.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {findFirstMatchingNode} from '@angular/compiler-cli/src/ngtsc/typecheck/src/comments';
1010
import * as e from '@angular/compiler/src/expression_parser/ast'; // e for expression AST
1111
import ts from 'typescript';
12-
import * as tss from 'typescript/lib/tsserverlibrary';
12+
import tss from 'typescript/lib/tsserverlibrary';
1313

1414
import {getTargetAtPosition, getTcbNodesOfTemplateAtPosition, TargetNodeKind} from '../template_target';
1515
import {getTemplateInfoAtPosition} from '../utils';

packages/language-service/src/codefixes/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {absoluteFrom} from '@angular/compiler-cli';
1010
import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
11-
import * as tss from 'typescript/lib/tsserverlibrary';
11+
import tss from 'typescript/lib/tsserverlibrary';
1212

1313
import {TemplateInfo} from '../utils';
1414

0 commit comments

Comments
 (0)