diff --git a/packages/language-service/BUILD.bazel b/packages/language-service/BUILD.bazel index 4a741ba60c..3379d4e4f7 100644 --- a/packages/language-service/BUILD.bazel +++ b/packages/language-service/BUILD.bazel @@ -6,15 +6,17 @@ ts_library( name = "api", srcs = [ "api.ts", + "index.ts", ], deps = [ + "@npm//@types/node", "@npm//typescript", ], ) ts_library( name = "language-service", - srcs = ["index.ts"] + glob( + srcs = glob( [ "src/**/*.ts", ], @@ -54,8 +56,7 @@ pkg_npm( "//integration:__pkg__", ], deps = [ - ":language-service", - "//packages/language-service/ivy", + ":api", # min bundle is not used at the moment; omit from package to speed up build "//packages/language-service/bundles:language-service.js", "//packages/language-service/bundles:ivy.js", diff --git a/packages/language-service/api.ts b/packages/language-service/api.ts index 603f82dbb5..a78f3173e4 100644 --- a/packages/language-service/api.ts +++ b/packages/language-service/api.ts @@ -14,6 +14,19 @@ import * as ts from 'typescript'; +export interface NgLanguageServiceConfig { + /** + * If true, return only Angular results. Otherwise, return Angular + TypeScript + * results. + */ + angularOnly: boolean; + /** + * If true, return factory function for Ivy LS during plugin initialization. + * Otherwise return factory function for View Engine LS. + */ + ivy: boolean; +} + export type GetTcbResponse = { /** * The filename of the SourceFile this typecheck block belongs to. diff --git a/packages/language-service/bundles/BUILD.bazel b/packages/language-service/bundles/BUILD.bazel index d1c5915e17..08e47e0d38 100644 --- a/packages/language-service/bundles/BUILD.bazel +++ b/packages/language-service/bundles/BUILD.bazel @@ -3,7 +3,7 @@ load("//dev-infra/benchmark/ng_rollup_bundle:ng_rollup_bundle.bzl", "ng_rollup_b ng_rollup_bundle( name = "language-service", build_optimizer = False, - entry_point = "//packages/language-service:index.ts", + entry_point = "//packages/language-service:src/ts_plugin.ts", format = "amd", globals = { "fs": "fs", diff --git a/packages/language-service/index.ts b/packages/language-service/index.ts index 0ed58aaa34..07a78428c5 100644 --- a/packages/language-service/index.ts +++ b/packages/language-service/index.ts @@ -6,5 +6,33 @@ * found in the LICENSE file at https://angular.io/license */ +import * as ts from 'typescript/lib/tsserverlibrary'; +import {NgLanguageService, NgLanguageServiceConfig} from './api'; + export * from './api'; -export {create, getExternalFiles} from './src/ts_plugin'; + +interface PluginModule extends ts.server.PluginModule { + create(createInfo: ts.server.PluginCreateInfo): NgLanguageService; + onConfigurationChanged?(config: NgLanguageServiceConfig): void; +} + +const factory: ts.server.PluginModuleFactory = (tsModule): PluginModule => { + let plugin: PluginModule; + + return { + create(info: ts.server.PluginCreateInfo): NgLanguageService { + const config: NgLanguageServiceConfig = info.config; + const bundleName = config.ivy ? 'ivy.js' : 'language-service.js'; + plugin = require(`./bundles/${bundleName}`)(tsModule); + return plugin.create(info); + }, + getExternalFiles(project: ts.server.Project): string[] { + return plugin?.getExternalFiles?.(project) ?? []; + }, + onConfigurationChanged(config: NgLanguageServiceConfig): void { + plugin?.onConfigurationChanged?.(config); + }, + }; +}; + +module.exports = factory; diff --git a/packages/language-service/package.json b/packages/language-service/package.json index e3e5b2a1b5..6bb933885d 100644 --- a/packages/language-service/package.json +++ b/packages/language-service/package.json @@ -2,7 +2,7 @@ "name": "@angular/language-service", "version": "0.0.0-PLACEHOLDER", "description": "Angular - language services", - "main": "./bundles/language-service.js", + "main": "./index.js", "typings": "./index.d.ts", "author": "angular", "license": "MIT",