Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement(build-tools): fixes for ESM builds #19625

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build-tools/packages/build-tools/src/common/tscUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ function createTscUtil(tsLib: typeof ts) {
parseCommandLine: (command: string) => {
// TODO: parse the command line for real, split space for now.
const args = command.split(" ");
if (command.includes("&&")) {
console.warn("Warning: '&&' is not supported in tsc command.");
}

const parsedCommand = tsLib.parseCommandLine(args.slice(1));
if (parsedCommand.errors.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,29 @@ export abstract class TscDependentTask extends LeafWithDoneFileTask {
protected abstract getToolVersion(): Promise<string>;
}

interface TscMultiConfig {
targets: {
extName?: string;
packageOverrides?: Record<string, unknown>;
}[];
projects: string[];
}

// This function is mimiced from tsc-multi.
function configKeyForPackageOverrides(overrides: Record<string, unknown> | undefined) {
if (overrides === undefined) return "";

const str = JSON.stringify(overrides);

// An implementation of DJB2 string hashing algorithm
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) + hash + str.charCodeAt(i); // hash * 33 + c
hash |= 0; // Convert to 32bit integer
}
return `.${hash}`;
}

/**
* A fluid-build task definition for tsc-multi.
*
Expand All @@ -484,32 +507,41 @@ export class TscMultiTask extends LeafWithDoneFileTask {
protected async getDoneFileContent(): Promise<string | undefined> {
const command = this.command;

/**
* A list of files that should be considered part of the cache input, if they exist
*/
const commonFiles = [
"tsconfig.json",
"src/test/tsconfig.json",
"tsc-multi.json",
"tsc-multi.test.json",
]
.map((file) => path.resolve(this.package.directory, file))
.filter((file) => existsSync(file));

try {
// The path to the tsbuildinfo file differs based on if it's a CJS vs. ESM build. Use the presence of "esnext" in
// the command string to determine which file to use.
const tsbuildinfoPath = this.getPackageFileFullPath(
command.includes("tsc-multi.esm.json")
? "tsconfig.mjs.tsbuildinfo"
: "tsconfig.cjs.tsbuildinfo",
const commandArgs = command.split(/\s+/);
jason-ha marked this conversation as resolved.
Show resolved Hide resolved
const configArg = commandArgs.findIndex((arg) => arg === "--config");
if (configArg === -1) {
throw new Error(`no --config argument for tsc-multi command: ${command}`);
}
const tscMultiConfigFile = path.resolve(
this.package.directory,
commandArgs[configArg + 1],
);
const tscMultiConfig = JSON.parse(
await readFileAsync(tscMultiConfigFile, "utf-8"),
) as TscMultiConfig;

if (tscMultiConfig.targets.length !== 1 || tscMultiConfig.projects.length !== 1) {
throw new Error(
`TscMultiTask does not support ${tscMultiConfigFile} that does not have exactly one target and project.`,
);
}
const project = tscMultiConfig.projects[0];
const projectExt = path.extname(project);
const target = tscMultiConfig.targets[0];
const relTsBuildInfoPath = `${project.substring(
0,
project.length - projectExt.length,
)}${target.extName ?? ""}${configKeyForPackageOverrides(
target.packageOverrides,
)}.tsbuildinfo`;
const tsbuildinfoPath = this.getPackageFileFullPath(relTsBuildInfoPath);
if (!existsSync(tsbuildinfoPath)) {
// No tsbuildinfo file, so we need to build
throw new Error(`no tsbuildinfo file found: ${tsbuildinfoPath}`);
}

const files = [...commonFiles];
const files = [tscMultiConfigFile, path.resolve(this.package.directory, project)];

// Add src files
files.push(...(await getRecursiveFiles(path.resolve(this.package.directory, "src"))));
Expand Down
Loading
Loading