Skip to content

Commit aae7d2a

Browse files
authored
Introduce a Heft SWC plugin. (#5149)
* Introduce a Heft SWC plugin. * Updates from testing repo. * Refactor tsconfig loading. * Remove an unused module. * fixup! Refactor tsconfig loading. * fixup! Introduce a Heft SWC plugin. * fixup! Introduce a Heft SWC plugin. * Introduce an accessor for tweaking the SWC options. * Rename heft-isolated-transpile-plugin to heft-isolated-typescript-transpile-plugin. * fixup! Rename heft-isolated-transpile-plugin to heft-isolated-typescript-transpile-plugin. * Convert to using child_process. * Add a 'numberOfCores' property to 'HeftConfiguration' * fixup! Introduce a Heft SWC plugin. * fixup! Add a 'numberOfCores' property to 'HeftConfiguration' * Include missing package.json properties. * Remove a ts-ignore
1 parent 224e157 commit aae7d2a

34 files changed

+1361
-198
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ These GitHub repositories provide supplementary resources for Rush Stack:
194194
| [/build-tests/rush-redis-cobuild-plugin-integration-test](./build-tests/rush-redis-cobuild-plugin-integration-test/) | Tests connecting to an redis server |
195195
| [/build-tests/set-webpack-public-path-plugin-test](./build-tests/set-webpack-public-path-plugin-test/) | Building this project tests the set-webpack-public-path-plugin |
196196
| [/eslint/local-eslint-config](./eslint/local-eslint-config/) | An ESLint configuration consumed projects inside the rushstack repo. |
197+
| [/heft-plugins/heft-isolated-typescript-transpile-plugin](./heft-plugins/heft-isolated-typescript-transpile-plugin/) | Heft plugin for transpiling TypeScript with SWC |
197198
| [/libraries/rush-themed-ui](./libraries/rush-themed-ui/) | Rush Component Library: a set of themed components for rush projects |
198199
| [/libraries/rushell](./libraries/rushell/) | Execute shell commands using a consistent syntax on every platform |
199200
| [/repo-scripts/doc-plugin-rush-stack](./repo-scripts/doc-plugin-rush-stack/) | API Documenter plugin used with the rushstack.io website |

apps/heft/src/cli/HeftActionRunner.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,16 @@ export class HeftActionRunner {
188188
private readonly _parallelism: number;
189189

190190
public constructor(options: IHeftActionRunnerOptions) {
191-
this._action = options.action;
192-
this._internalHeftSession = options.internalHeftSession;
193-
this._heftConfiguration = options.heftConfiguration;
194-
this._loggingManager = options.loggingManager;
195-
this._terminal = options.terminal;
196-
this._metricsCollector = options.metricsCollector;
197-
198-
const numberOfCores: number = os.availableParallelism?.() ?? os.cpus().length;
191+
const { action, internalHeftSession, heftConfiguration, loggingManager, terminal, metricsCollector } =
192+
options;
193+
this._action = action;
194+
this._internalHeftSession = internalHeftSession;
195+
this._heftConfiguration = heftConfiguration;
196+
this._loggingManager = loggingManager;
197+
this._terminal = terminal;
198+
this._metricsCollector = metricsCollector;
199+
200+
const numberOfCores: number = heftConfiguration.numberOfCores;
199201

200202
// If an explicit parallelism number wasn't provided, then choose a sensible
201203
// default.

apps/heft/src/cli/HeftCommandLineParser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
import os from 'node:os';
45
import {
56
CommandLineParser,
67
type AliasCommandLineAction,
@@ -87,9 +88,11 @@ export class HeftCommandLineParser extends CommandLineParser {
8788
InternalError.breakInDebugger = true;
8889
}
8990

91+
const numberOfCores: number = os.availableParallelism?.() ?? os.cpus().length;
9092
this._heftConfiguration = HeftConfiguration.initialize({
9193
cwd: process.cwd(),
92-
terminalProvider: this._terminalProvider
94+
terminalProvider: this._terminalProvider,
95+
numberOfCores
9396
});
9497

9598
this._metricsCollector = new MetricsCollector();

apps/heft/src/configuration/HeftConfiguration.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,31 @@ export interface IHeftConfigurationInitializationOptions {
2222
* Terminal instance to facilitate logging.
2323
*/
2424
terminalProvider: ITerminalProvider;
25+
26+
/**
27+
* The number of CPU cores available to the process. This is used to determine how many tasks can be run in parallel.
28+
*/
29+
numberOfCores: number;
30+
}
31+
32+
interface IHeftConfigurationOptions extends IHeftConfigurationInitializationOptions {
33+
buildFolderPath: string;
2534
}
2635

2736
/**
2837
* @public
2938
*/
3039
export class HeftConfiguration {
31-
private _buildFolderPath!: string;
3240
private _slashNormalizedBuildFolderPath: string | undefined;
3341
private _projectConfigFolderPath: string | undefined;
3442
private _tempFolderPath: string | undefined;
3543
private _rigConfig: IRigConfig | undefined;
36-
private _globalTerminal!: Terminal;
37-
private _terminalProvider!: ITerminalProvider;
38-
private _rigPackageResolver!: RigPackageResolver;
44+
private _rigPackageResolver: RigPackageResolver | undefined;
3945

4046
/**
4147
* Project build folder path. This is the folder containing the project's package.json file.
4248
*/
43-
public get buildFolderPath(): string {
44-
return this._buildFolderPath;
45-
}
49+
public readonly buildFolderPath: string;
4650

4751
/**
4852
* {@link HeftConfiguration.buildFolderPath} with all path separators converted to forward slashes.
@@ -75,7 +79,7 @@ export class HeftConfiguration {
7579
*/
7680
public get tempFolderPath(): string {
7781
if (!this._tempFolderPath) {
78-
this._tempFolderPath = path.join(this._buildFolderPath, Constants.tempFolderName);
82+
this._tempFolderPath = path.join(this.buildFolderPath, Constants.tempFolderName);
7983
}
8084

8185
return this._tempFolderPath;
@@ -104,22 +108,19 @@ export class HeftConfiguration {
104108
rigConfig: this.rigConfig
105109
});
106110
}
111+
107112
return this._rigPackageResolver;
108113
}
109114

110115
/**
111116
* Terminal instance to facilitate logging.
112117
*/
113-
public get globalTerminal(): ITerminal {
114-
return this._globalTerminal;
115-
}
118+
public readonly globalTerminal: ITerminal;
116119

117120
/**
118121
* Terminal provider for the provided terminal.
119122
*/
120-
public get terminalProvider(): ITerminalProvider {
121-
return this._terminalProvider;
122-
}
123+
public readonly terminalProvider: ITerminalProvider;
123124

124125
/**
125126
* The Heft tool's package.json
@@ -135,7 +136,18 @@ export class HeftConfiguration {
135136
return PackageJsonLookup.instance.tryLoadPackageJsonFor(this.buildFolderPath)!;
136137
}
137138

138-
private constructor() {}
139+
/**
140+
* The number of CPU cores available to the process. This can be used to determine how many tasks can be run
141+
* in parallel.
142+
*/
143+
public readonly numberOfCores: number;
144+
145+
private constructor({ terminalProvider, buildFolderPath, numberOfCores }: IHeftConfigurationOptions) {
146+
this.buildFolderPath = buildFolderPath;
147+
this.terminalProvider = terminalProvider;
148+
this.numberOfCores = numberOfCores;
149+
this.globalTerminal = new Terminal(terminalProvider);
150+
}
139151

140152
/**
141153
* Performs the search for rig.json and initializes the `HeftConfiguration.rigConfig` object.
@@ -144,7 +156,7 @@ export class HeftConfiguration {
144156
public async _checkForRigAsync(): Promise<void> {
145157
if (!this._rigConfig) {
146158
this._rigConfig = await RigConfig.loadForProjectFolderAsync({
147-
projectFolderPath: this._buildFolderPath
159+
projectFolderPath: this.buildFolderPath
148160
});
149161
}
150162
}
@@ -153,26 +165,26 @@ export class HeftConfiguration {
153165
* @internal
154166
*/
155167
public static initialize(options: IHeftConfigurationInitializationOptions): HeftConfiguration {
156-
const configuration: HeftConfiguration = new HeftConfiguration();
157-
158168
const packageJsonPath: string | undefined = PackageJsonLookup.instance.tryGetPackageJsonFilePathFor(
159169
options.cwd
160170
);
171+
let buildFolderPath: string;
161172
if (packageJsonPath) {
162-
let buildFolderPath: string = path.dirname(packageJsonPath);
173+
buildFolderPath = path.dirname(packageJsonPath);
163174
// On Windows it is possible for the drive letter in the CWD to be lowercase, but the normalized naming is uppercase
164175
// Force it to always be uppercase for consistency.
165176
buildFolderPath =
166177
process.platform === 'win32'
167178
? buildFolderPath.charAt(0).toUpperCase() + buildFolderPath.slice(1)
168179
: buildFolderPath;
169-
configuration._buildFolderPath = buildFolderPath;
170180
} else {
171181
throw new Error('No package.json file found. Are you in a project folder?');
172182
}
173183

174-
configuration._terminalProvider = options.terminalProvider;
175-
configuration._globalTerminal = new Terminal(options.terminalProvider);
184+
const configuration: HeftConfiguration = new HeftConfiguration({
185+
...options,
186+
buildFolderPath
187+
});
176188
return configuration;
177189
}
178190
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-isolated-typescript-transpile-plugin",
5+
"comment": "Initial release.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-isolated-typescript-transpile-plugin"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-typescript-plugin",
5+
"comment": "Expose some internal APIs to be used by `@rushstack/heft-isolated-typescript-transpile-plugin`.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-typescript-plugin"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft",
5+
"comment": "Add a `numberOfCores` property to `HeftConfiguration`.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft"
10+
}

common/config/rush/nonbrowser-approved-packages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@
366366
"name": "@storybook/theming",
367367
"allowedCategories": [ "tests" ]
368368
},
369+
{
370+
"name": "@swc/core",
371+
"allowedCategories": [ "libraries" ]
372+
},
369373
{
370374
"name": "@tsconfig/node14",
371375
"allowedCategories": [ "tests" ]

0 commit comments

Comments
 (0)