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

Build tooling 3/? - export conditions #12385

Draft
wants to merge 24 commits into
base: release-4.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
work around api extractor package resolution
  • Loading branch information
phryneas committed Feb 28, 2025
commit 5f1ac789da7d2e6bfbeec0e9cc4d6f50cd56ea10
3 changes: 2 additions & 1 deletion .api-reports/api-report-utilities_globals.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

```ts

import { __DEV__ as __DEV___2 } from '@apollo/client/utilities/globals/environment';
import { InvariantError } from 'ts-invariant';

// @public (undocumented)
const __DEV___2: boolean;
export { __DEV___2 as __DEV__ }

// @public (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"projectFolder": ".",
"mainEntryPointFilePath": "<projectFolder>/dist/index.d.ts",
"bundledPackages": [],
"bundledPackages": ["@apollo/client"],
"newlineKind": "lf",
"enumMemberOrder": "preserve",

Expand Down
21 changes: 12 additions & 9 deletions config/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { parseArgs } from "node:util";
import fs from "node:fs";
import { entryPoints, buildDocEntryPoints } from "./entryPoints.ts";
import { readFileSync } from "fs";
import { withPseudoNodeModules } from "./helpers.ts";

const parsed = parseArgs({
options: {
Expand Down Expand Up @@ -61,29 +62,29 @@ try {
})
);

buildReport(entryPointFile, "docModel");
await buildReport(entryPointFile, "docModel");
}

if (parsed.values.generate?.includes("apiReport")) {
entryPoints.map((entryPoint) => {
for (const entryPoint of entryPoints) {
const path = entryPoint.dirs.join("/");
const mainEntryPointFilePath =
`<projectFolder>/dist/${path}/index.d.ts`.replace("//", "/");
console.log(
"\n\nCreating API extractor report for " + mainEntryPointFilePath
);
buildReport(
await buildReport(
mainEntryPointFilePath,
"apiReport",
`api-report${path ? "-" + path.replace(/\//g, "_") : ""}.api.md`
);
});
}
}
} finally {
fs.rmSync(tempDir, { recursive: true });
}

function buildReport(
async function buildReport(
mainEntryPointFilePath: string,
mode: "apiReport" | "docModel",
reportFileName = ""
Expand Down Expand Up @@ -117,10 +118,12 @@ function buildReport(
configObjectFullPath,
});

const extractorResult = Extractor.invoke(extractorConfig, {
localBuild: process.env.CI === undefined || process.env.CI === "false",
showVerboseMessages: true,
});
const extractorResult = await withPseudoNodeModules(() =>
Extractor.invoke(extractorConfig, {
localBuild: process.env.CI === undefined || process.env.CI === "false",
showVerboseMessages: true,
})
);

let succeededAdditionalChecks = true;
if (fs.existsSync(extractorConfig.reportFilePath)) {
Expand Down
2 changes: 1 addition & 1 deletion config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const buildSteps = {
typescript: compileTs,
babelTransform,
updateVersion,
//inlineInheritDoc,
inlineInheritDoc,
processInvariants,
postprocessDist,
verifyVersion,
Expand Down
6 changes: 2 additions & 4 deletions config/entryPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export const buildDocEntryPoints = (
) => {
const acc = entryPoints.map((entryPoint) => {
return `export * from "${path.join(
options.rootDir,
options.targetDir,
...entryPoint.dirs,
`index.${options.jsExt}`
"@apollo/client",
...entryPoint.dirs
)}";`;
});
acc.push(
Expand Down
26 changes: 24 additions & 2 deletions config/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as path from "path";
import * as recast from "recast";
import * as parser from "recast/parsers/babel.js";
import * as tsParser from "recast/parsers/typescript.js";
import glob from "glob";
import { glob as nodeGlob } from "node:fs/promises";
import { glob as nodeGlob, unlink } from "node:fs/promises";
import { readFile, rm } from "node:fs/promises";
import * as assert from "node:assert";
// @ts-ignore unfortunately we don't have types for this as it's JS with JSDoc
// eslint-disable-next-line import/no-unresolved
import * as sorcery from "sorcery";
import { relative } from "node:path";
import { mkdir, rmdir, symlink } from "node:fs/promises";

export const distDir = path.resolve(import.meta.dirname, "..", "dist");

Expand Down Expand Up @@ -100,3 +100,25 @@ export async function applyRecast({
await chain.write();
}
}

/**
* creates a pseudo "dist/node_modules" folder with
* "dist/node_modules/@apollo/client" symlinked to "dist"
* so that tools can pick up the client package as an "external" package
*/
export async function withPseudoNodeModules<T>(fn: () => T) {
const dist = path.join(import.meta.dirname, "..", "dist");
const node_modules = path.join(dist, "node_modules");
const parent = path.join(node_modules, "@apollo");
const link = path.join(parent, "client");

try {
await mkdir(parent, { recursive: true });
await unlink(link).catch(() => {});
await symlink(dist, link);

return await fn();
} finally {
await rmdir(node_modules, { recursive: true });
}
}
13 changes: 10 additions & 3 deletions config/inlineInheritDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import { ApiModel, ApiDocumentedItem } from "@microsoft/api-extractor-model";
import { DeclarationReference } from "@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference.js";
import { StringBuilder, TSDocEmitter } from "@microsoft/tsdoc";

import { applyRecast } from "./helpers.ts";
import { applyRecast, withPseudoNodeModules } from "./helpers.ts";
import { visit } from "recast";
import type { BuildStep, BuildStepOptions } from "./build.ts";

import fs from "node:fs";
import fs, { mkdirSync, symlinkSync } from "node:fs";
import path from "node:path";
import {
Extractor,
Expand All @@ -46,7 +46,7 @@ export const inlineInheritDoc: BuildStep = async (options) => {
"Processing {@inheritDoc <canonicalReference>} comments in .d.ts files."
);

const model = loadApiModel(options);
const model = await withPseudoNodeModules(() => loadApiModel(options));
await processComments(model, options);
};

Expand Down Expand Up @@ -78,6 +78,13 @@ function loadApiModel(options: BuildStepOptions) {
try {
const entryPointFile = path.join(tempDir, "entry.d.ts");
fs.writeFileSync(entryPointFile, buildDocEntryPoints(options));
mkdirSync(path.join(tempDir, "node_modules", "@apollo"), {
recursive: true,
});
symlinkSync(
options.packageRoot,
path.join(tempDir, "node_modules", "@apollo", "client")
);

// Load and parse the api-extractor.json file
const configObjectFullPath = path.resolve(
Expand Down
Loading