Skip to content

Commit 30fb9fa

Browse files
authored
Refactor plugin related code (microsoft#53942)
1 parent 58a5f4e commit 30fb9fa

File tree

8 files changed

+115
-134
lines changed

8 files changed

+115
-134
lines changed

src/compiler/sys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import {
2828
matchesExclude,
2929
matchFiles,
3030
memoize,
31+
ModuleImportResult,
3132
noop,
3233
normalizePath,
3334
normalizeSlashes,
3435
orderedRemoveItem,
3536
Path,
3637
perfLogger,
3738
PollingWatchKind,
38-
RequireResult,
3939
resolveJSModule,
4040
some,
4141
startsWith,
@@ -1428,7 +1428,7 @@ export interface System {
14281428
base64decode?(input: string): string;
14291429
base64encode?(input: string): string;
14301430
/** @internal */ bufferFrom?(input: string, encoding?: string): Buffer;
1431-
/** @internal */ require?(baseDir: string, moduleName: string): RequireResult;
1431+
/** @internal */ require?(baseDir: string, moduleName: string): ModuleImportResult;
14321432

14331433
// For testing
14341434
/** @internal */ now?(): Date;

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7371,7 +7371,7 @@ export interface ConfigFileSpecs {
73717371
}
73727372

73737373
/** @internal */
7374-
export type RequireResult<T = {}> =
7374+
export type ModuleImportResult<T = {}> =
73757375
| { module: T, modulePath?: string, error: undefined }
73767376
| { module: undefined, modulePath?: undefined, error: { stack?: string, message?: string } };
73777377

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ class SessionServerHost implements ts.server.ServerHost, ts.server.Logger {
885885
return mockHash(s);
886886
}
887887

888-
require(_initialDir: string, _moduleName: string): ts.RequireResult {
888+
require(_initialDir: string, _moduleName: string): ts.ModuleImportResult {
889889
switch (_moduleName) {
890890
// Adds to the Quick Info a fixed string and a string from the config file
891891
// and replaces the first display part

src/server/editorServices.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ export class ProjectService {
962962
public readonly globalPlugins: readonly string[];
963963
public readonly pluginProbeLocations: readonly string[];
964964
public readonly allowLocalPluginLoads: boolean;
965-
private currentPluginConfigOverrides: Map<string, any> | undefined;
965+
/** @internal */ currentPluginConfigOverrides: Map<string, any> | undefined;
966966

967967
public readonly typesMapLocation: string | undefined;
968968

@@ -2189,7 +2189,6 @@ export class ProjectService {
21892189
/*lastFileExceededProgramSize*/ this.getFilenameForExceededTotalSizeLimitForNonTsFiles(projectFileName, compilerOptions, files, externalFilePropertyReader),
21902190
options.compileOnSave === undefined ? true : options.compileOnSave,
21912191
/*projectFilePath*/ undefined,
2192-
this.currentPluginConfigOverrides,
21932192
watchOptionsAndErrors?.watchOptions
21942193
);
21952194
project.setProjectErrors(watchOptionsAndErrors?.errors);
@@ -2354,7 +2353,7 @@ export class ProjectService {
23542353
project.enableLanguageService();
23552354
this.watchWildcards(configFilename, configFileExistenceInfo, project);
23562355
}
2357-
project.enablePluginsWithOptions(compilerOptions, this.currentPluginConfigOverrides);
2356+
project.enablePluginsWithOptions(compilerOptions);
23582357
const filesToAdd = parsedCommandLine.fileNames.concat(project.getExternalFiles());
23592358
this.updateRootAndOptionsOfNonInferredProject(project, filesToAdd, fileNamePropertyReader, compilerOptions, parsedCommandLine.typeAcquisition!, parsedCommandLine.compileOnSave, parsedCommandLine.watchOptions);
23602359
tracing?.pop();
@@ -2737,7 +2736,7 @@ export class ProjectService {
27372736
typeAcquisition = this.typeAcquisitionForInferredProjects;
27382737
}
27392738
watchOptionsAndErrors = watchOptionsAndErrors || undefined;
2740-
const project = new InferredProject(this, this.documentRegistry, compilerOptions, watchOptionsAndErrors?.watchOptions, projectRootPath, currentDirectory, this.currentPluginConfigOverrides, typeAcquisition);
2739+
const project = new InferredProject(this, this.documentRegistry, compilerOptions, watchOptionsAndErrors?.watchOptions, projectRootPath, currentDirectory, typeAcquisition);
27412740
project.setProjectErrors(watchOptionsAndErrors?.errors);
27422741
if (isSingleInferredProject) {
27432742
this.inferredProjects.unshift(project);
@@ -4242,8 +4241,11 @@ export class ProjectService {
42424241
return false;
42434242
}
42444243

4245-
/** @internal */
4246-
requestEnablePlugin(project: Project, pluginConfigEntry: PluginImport, searchPaths: string[], pluginConfigOverrides: Map<string, any> | undefined) {
4244+
/**
4245+
* Performs the initial steps of enabling a plugin by finding and instantiating the module for a plugin either asynchronously or synchronously
4246+
* @internal
4247+
*/
4248+
requestEnablePlugin(project: Project, pluginConfigEntry: PluginImport, searchPaths: string[]) {
42474249
if (!this.host.importPlugin && !this.host.require) {
42484250
this.logger.info("Plugins were requested but not running in environment that supports 'require'. Nothing will be loaded");
42494251
return;
@@ -4257,7 +4259,12 @@ export class ProjectService {
42574259

42584260
// If the host supports dynamic import, begin enabling the plugin asynchronously.
42594261
if (this.host.importPlugin) {
4260-
const importPromise = project.beginEnablePluginAsync(pluginConfigEntry, searchPaths, pluginConfigOverrides);
4262+
const importPromise = Project.importServicePluginAsync(
4263+
pluginConfigEntry,
4264+
searchPaths,
4265+
this.host,
4266+
s => this.logger.info(s),
4267+
) as Promise<BeginEnablePluginResult>;
42614268
this.pendingPluginEnablements ??= new Map();
42624269
let promises = this.pendingPluginEnablements.get(project);
42634270
if (!promises) this.pendingPluginEnablements.set(project, promises = []);
@@ -4266,7 +4273,33 @@ export class ProjectService {
42664273
}
42674274

42684275
// Otherwise, load the plugin using `require`
4269-
project.endEnablePlugin(project.beginEnablePluginSync(pluginConfigEntry, searchPaths, pluginConfigOverrides));
4276+
this.endEnablePlugin(project, Project.importServicePluginSync(
4277+
pluginConfigEntry,
4278+
searchPaths,
4279+
this.host,
4280+
s => this.logger.info(s),
4281+
));
4282+
}
4283+
4284+
/**
4285+
* Performs the remaining steps of enabling a plugin after its module has been instantiated.
4286+
* @internal
4287+
*/
4288+
private endEnablePlugin(project: Project, { pluginConfigEntry, resolvedModule, errorLogs }: BeginEnablePluginResult) {
4289+
if (resolvedModule) {
4290+
const configurationOverride = this.currentPluginConfigOverrides?.get(pluginConfigEntry.name);
4291+
if (configurationOverride) {
4292+
// Preserve the name property since it's immutable
4293+
const pluginName = pluginConfigEntry.name;
4294+
pluginConfigEntry = configurationOverride;
4295+
pluginConfigEntry.name = pluginName;
4296+
}
4297+
project.enableProxy(resolvedModule, pluginConfigEntry);
4298+
}
4299+
else {
4300+
forEach(errorLogs, message => this.logger.info(message));
4301+
this.logger.info(`Couldn't find ${pluginConfigEntry.name}`);
4302+
}
42704303
}
42714304

42724305
/** @internal */
@@ -4345,7 +4378,7 @@ export class ProjectService {
43454378
}
43464379

43474380
for (const result of results) {
4348-
project.endEnablePlugin(result);
4381+
this.endEnablePlugin(project, result);
43494382
}
43504383

43514384
// Plugins may have modified external files, so mark the project as dirty.

0 commit comments

Comments
 (0)