-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathTypeScriptPlugin.ts
More file actions
414 lines (359 loc) · 14.4 KB
/
Copy pathTypeScriptPlugin.ts
File metadata and controls
414 lines (359 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import type * as TTypescript from 'typescript';
import { SyncHook } from 'tapable';
import { FileSystem } from '@rushstack/node-core-library';
import type { ITerminal } from '@rushstack/terminal';
import { ProjectConfigurationFile, InheritanceType, PathResolutionMethod } from '@rushstack/heft-config-file';
import type {
HeftConfiguration,
IHeftTaskSession,
IHeftTaskPlugin,
IHeftTaskRunHookOptions,
IHeftTaskRunIncrementalHookOptions,
ICopyOperation,
IHeftTaskFileOperations,
ConfigurationFile
} from '@rushstack/heft';
import { TypeScriptBuilder, type ITypeScriptBuilderConfiguration } from './TypeScriptBuilder';
import anythingSchema from './schemas/anything.schema.json';
import typescriptConfigSchema from './schemas/typescript.schema.json';
import { getTsconfigFilePath } from './tsconfigLoader';
/**
* The name of the plugin, as specified in heft-plugin.json
*
* @public
*/
export const PLUGIN_NAME: 'typescript-plugin' = 'typescript-plugin';
/**
* The ${configDir} token supported in TypeScript 5.5
* @see {@link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#the-configdir-template-variable-for-configuration-files}
*/
const CONFIG_DIR_TOKEN: '${configDir}' = '${configDir}';
/**
* @beta
*/
export interface IEmitModuleKind {
moduleKind: 'commonjs' | 'amd' | 'umd' | 'system' | 'es2015' | 'esnext';
outFolderName: string;
jsExtensionOverride?: string;
emitModulePackageJson?: boolean;
}
/**
* @beta
*/
export interface IStaticAssetsCopyConfiguration {
fileExtensions: string[];
excludeGlobs: string[];
includeGlobs: string[];
}
/**
* @beta
*/
export interface ITypeScriptConfigurationJson {
/**
* If provided, emit these module kinds in addition to the modules specified in the tsconfig.
* Note that this option only applies to the main tsconfig.json configuration.
*/
additionalModuleKindsToEmit?: IEmitModuleKind[] | undefined;
/**
* If 'true', emit CommonJS output into the TSConfig outDir with the file extension '.cjs'
*/
emitCjsExtensionForCommonJS?: boolean | undefined;
/**
* If 'true', emit ESModule output into the TSConfig outDir with the file extension '.mjs'
*/
emitMjsExtensionForESModule?: boolean | undefined;
/**
* If true, enable behavior analogous to the "tsc --build" command. Will build projects referenced by the main project in dependency order.
* Note that this will effectively enable \"noEmitOnError\".
*/
buildProjectReferences?: boolean;
/**
* If true, and the tsconfig has \"isolatedModules\": true, then transpilation will happen in parallel in a worker thread.
*/
useTranspilerWorker?: boolean;
/**
* If true, the TypeScript compiler will only resolve symlinks to their targets if the links are in a node_modules folder.
* This significantly reduces file system operations in typical usage.
*/
onlyResolveSymlinksInNodeModules?: boolean;
/*
* Specifies the tsconfig.json file that will be used for compilation. Equivalent to the "project" argument for the 'tsc' and 'tslint' command line tools.
*
* The default value is "./tsconfig.json"
*/
project?: string;
/**
* Configures additional file types that should be copied into the TypeScript compiler's emit folders, for example
* so that these files can be resolved by import statements.
*/
staticAssetsToCopy?: IStaticAssetsCopyConfiguration;
}
/**
* @beta
*/
export interface IPartialTsconfigCompilerOptions {
outDir?: string;
}
/**
* @beta
*/
export interface IPartialTsconfig {
compilerOptions?: IPartialTsconfigCompilerOptions;
}
/**
* @beta
*/
export interface IChangedFilesHookOptions {
program: TTypescript.Program;
changedFiles?: ReadonlySet<TTypescript.SourceFile>;
}
/**
* @beta
*/
export interface ITypeScriptPluginAccessor {
readonly onChangedFilesHook: SyncHook<IChangedFilesHookOptions>;
}
const TYPESCRIPT_LOADER_CONFIG: ConfigurationFile.IProjectConfigurationFileSpecification<ITypeScriptConfigurationJson> =
{
projectRelativeFilePath: 'config/typescript.json',
jsonSchemaObject: typescriptConfigSchema,
propertyInheritance: {
staticAssetsToCopy: {
// When merging objects, arrays will be automatically appended
inheritanceType: InheritanceType.merge
}
},
jsonPathMetadata: {
'$.additionalModuleKindsToEmit.*.outFolderName': {
pathResolutionMethod: PathResolutionMethod.resolvePathRelativeToProjectRoot
}
}
};
/**
* @beta
*/
export async function loadTypeScriptConfigurationFileAsync(
heftConfiguration: HeftConfiguration,
terminal: ITerminal
): Promise<ITypeScriptConfigurationJson | undefined> {
return await heftConfiguration.tryLoadProjectConfigurationFileAsync<ITypeScriptConfigurationJson>(
TYPESCRIPT_LOADER_CONFIG,
terminal
);
}
let _partialTsconfigFileLoader: ProjectConfigurationFile<IPartialTsconfig> | undefined;
const _partialTsconfigFilePromiseCache: Map<string, Promise<IPartialTsconfig | undefined>> = new Map();
/**
* @beta
*/
export async function loadPartialTsconfigFileAsync(
heftConfiguration: HeftConfiguration,
terminal: ITerminal,
typeScriptConfigurationJson: ITypeScriptConfigurationJson | undefined
): Promise<IPartialTsconfig | undefined> {
const buildFolderPath: string = heftConfiguration.buildFolderPath;
// Check the cache first
let partialTsconfigFilePromise: Promise<IPartialTsconfig | undefined> | undefined =
_partialTsconfigFilePromiseCache.get(buildFolderPath);
if (!partialTsconfigFilePromise) {
// We don't want to load the tsconfig.json file through the rig, but we do want to take
// advantage of the extends functionality that ConfigurationFile provides. So we'll
// check to see if the file exists and exit early if not.
const tsconfigFilePath: string = getTsconfigFilePath(
heftConfiguration,
typeScriptConfigurationJson?.project
);
terminal.writeVerboseLine(`Looking for tsconfig at ${tsconfigFilePath}`);
const tsconfigExists: boolean = await FileSystem.existsAsync(tsconfigFilePath);
if (!tsconfigExists) {
partialTsconfigFilePromise = Promise.resolve(undefined);
} else {
// Ensure that the file loader has been initialized.
if (!_partialTsconfigFileLoader) {
_partialTsconfigFileLoader = new ProjectConfigurationFile<IPartialTsconfig>({
projectRelativeFilePath: typeScriptConfigurationJson?.project || 'tsconfig.json',
jsonSchemaObject: anythingSchema,
propertyInheritance: {
compilerOptions: {
inheritanceType: InheritanceType.merge
}
},
jsonPathMetadata: {
'$.compilerOptions.outDir': {
pathResolutionMethod: PathResolutionMethod.custom,
customResolver(
resolverOptions: ConfigurationFile.IJsonPathMetadataResolverOptions<IPartialTsconfig>
): string {
if (resolverOptions.propertyValue.includes(CONFIG_DIR_TOKEN)) {
// Typescript 5.5. introduced the `${configDir}` token to refer to the directory containing the root tsconfig
const configDir: string = path.dirname(tsconfigFilePath);
// The token is an absolute path, so it should occur at most once.
return path.resolve(resolverOptions.propertyValue.replace(CONFIG_DIR_TOKEN, configDir));
} else {
const thisConfigDir: string = path.dirname(resolverOptions.configurationFilePath);
return path.resolve(thisConfigDir, resolverOptions.propertyValue);
}
}
}
}
});
}
partialTsconfigFilePromise = _partialTsconfigFileLoader.loadConfigurationFileForProjectAsync(
terminal,
buildFolderPath,
heftConfiguration.rigConfig
);
}
_partialTsconfigFilePromiseCache.set(buildFolderPath, partialTsconfigFilePromise);
}
return await partialTsconfigFilePromise;
}
interface ITypeScriptConfigurationJsonAndPartialTsconfigFile {
typeScriptConfigurationJson: ITypeScriptConfigurationJson | undefined;
partialTsconfigFile: IPartialTsconfig | undefined;
}
export default class TypeScriptPlugin implements IHeftTaskPlugin {
public accessor: ITypeScriptPluginAccessor = {
onChangedFilesHook: new SyncHook<IChangedFilesHookOptions>(['changedFilesHookOptions'])
};
public apply(taskSession: IHeftTaskSession, heftConfiguration: HeftConfiguration): void {
taskSession.hooks.registerFileOperations.tapPromise(
PLUGIN_NAME,
async (fileOperations: IHeftTaskFileOperations): Promise<IHeftTaskFileOperations> => {
// TODO: We should consider maybe only doing one copy of static assets and pointing
// all source files to this set of static assets. This would allow us to avoid
// having to copy the static assets multiple times, increasing build times and
// package size.
for (const copyOperation of await this._getStaticAssetCopyOperationsAsync(
taskSession,
heftConfiguration
)) {
fileOperations.copyOperations.add(copyOperation);
}
return fileOperations;
}
);
taskSession.hooks.run.tapPromise(PLUGIN_NAME, async (runOptions: IHeftTaskRunHookOptions) => {
const builder: TypeScriptBuilder | false = await this._getTypeScriptBuilderAsync(
taskSession,
heftConfiguration
);
if (builder) {
await builder.invokeAsync();
}
});
let incrementalBuilder: TypeScriptBuilder | undefined | false;
taskSession.hooks.runIncremental.tapPromise(
PLUGIN_NAME,
async (runIncrementalOptions: IHeftTaskRunIncrementalHookOptions) => {
if (incrementalBuilder === undefined) {
// eslint-disable-next-line require-atomic-updates
incrementalBuilder = await this._getTypeScriptBuilderAsync(taskSession, heftConfiguration);
}
if (incrementalBuilder) {
await incrementalBuilder.invokeAsync(runIncrementalOptions.requestRun);
}
}
);
}
private async _getStaticAssetCopyOperationsAsync(
taskSession: IHeftTaskSession,
heftConfiguration: HeftConfiguration
): Promise<ICopyOperation[]> {
const { typeScriptConfigurationJson, partialTsconfigFile } = await this._loadConfigAsync(
taskSession,
heftConfiguration
);
// We only care about the copy if static assets were specified.
const copyOperations: ICopyOperation[] = [];
const staticAssetsConfig: IStaticAssetsCopyConfiguration | undefined =
typeScriptConfigurationJson?.staticAssetsToCopy;
if (
staticAssetsConfig &&
(staticAssetsConfig.fileExtensions?.length ||
staticAssetsConfig.includeGlobs?.length ||
staticAssetsConfig.excludeGlobs?.length)
) {
const destinationFolderPaths: Set<string> = new Set<string>();
// Add the output folder and all additional module kind output folders as destinations
const tsconfigOutDir: string | undefined = partialTsconfigFile?.compilerOptions?.outDir;
if (tsconfigOutDir) {
destinationFolderPaths.add(tsconfigOutDir);
}
for (const emitModule of typeScriptConfigurationJson?.additionalModuleKindsToEmit || []) {
destinationFolderPaths.add(emitModule.outFolderName);
}
copyOperations.push({
...staticAssetsConfig,
// For now - these may need to be revised later
sourcePath: path.resolve(heftConfiguration.buildFolderPath, 'src'),
destinationFolders: Array.from(destinationFolderPaths),
flatten: false,
hardlink: false
});
}
return copyOperations;
}
private async _getTypeScriptBuilderAsync(
taskSession: IHeftTaskSession,
heftConfiguration: HeftConfiguration
): Promise<TypeScriptBuilder | false> {
const { typeScriptConfigurationJson, partialTsconfigFile } = await this._loadConfigAsync(
taskSession,
heftConfiguration
);
if (!partialTsconfigFile) {
// There is no tsconfig file, we can exit early
// This check may need watch mode to break on tsconfig addition/deletion
return false;
}
// Build out the configuration
const typeScriptBuilderConfiguration: ITypeScriptBuilderConfiguration = {
buildFolderPath: heftConfiguration.buildFolderPath,
// Build metadata is just another build output, but we put it in the temp folder because it will
// usually be discarded when published.
buildMetadataFolderPath: taskSession.tempFolderPath,
heftConfiguration,
buildProjectReferences: typeScriptConfigurationJson?.buildProjectReferences,
useTranspilerWorker: typeScriptConfigurationJson?.useTranspilerWorker,
onlyResolveSymlinksInNodeModules: typeScriptConfigurationJson?.onlyResolveSymlinksInNodeModules,
tsconfigPath: getTsconfigFilePath(heftConfiguration, typeScriptConfigurationJson?.project),
additionalModuleKindsToEmit: typeScriptConfigurationJson?.additionalModuleKindsToEmit,
emitCjsExtensionForCommonJS: !!typeScriptConfigurationJson?.emitCjsExtensionForCommonJS,
emitMjsExtensionForESModule: !!typeScriptConfigurationJson?.emitMjsExtensionForESModule,
scopedLogger: taskSession.logger,
emitChangedFilesCallback: (
program: TTypescript.Program,
changedFiles?: Set<TTypescript.SourceFile>
) => {
// Provide the typescript program dependent plugins
if (this.accessor.onChangedFilesHook.isUsed()) {
this.accessor.onChangedFilesHook.call({ program, changedFiles });
}
}
};
// Run the builder
const typeScriptBuilder: TypeScriptBuilder = new TypeScriptBuilder(typeScriptBuilderConfiguration);
return typeScriptBuilder;
}
private async _loadConfigAsync(
taskSession: IHeftTaskSession,
heftConfiguration: HeftConfiguration
): Promise<ITypeScriptConfigurationJsonAndPartialTsconfigFile> {
const terminal: ITerminal = taskSession.logger.terminal;
const typeScriptConfigurationJson: ITypeScriptConfigurationJson | undefined =
await loadTypeScriptConfigurationFileAsync(heftConfiguration, terminal);
const partialTsconfigFile: IPartialTsconfig | undefined = await loadPartialTsconfigFileAsync(
heftConfiguration,
terminal,
typeScriptConfigurationJson
);
return {
typeScriptConfigurationJson,
partialTsconfigFile
};
}
}