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
build working, jest not working
  • Loading branch information
phryneas committed Feb 19, 2025
commit fe8bbba3b5e177c2842ff40ef62945c99db136a3
5 changes: 5 additions & 0 deletions .changeset/late-trainers-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

dropped the deprecated `DEV` export from `@apollo/client/utilities` and `@apollo/client/utilities/globals`
4 changes: 3 additions & 1 deletion config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { postprocessDist } from "./postprocessDist.ts";
import { verifySourceMaps } from "./verifySourceMaps.ts";
import { prepareChangesetsRelease } from "./prepareChangesetsRelease.ts";
import { babelTransform } from "./babel.ts";
import { addExports } from "./exports.ts";

export interface BuildStepOptions {
type: "esm" | "cjs";
Expand All @@ -32,6 +33,8 @@ $.cwd = join(import.meta.dirname, "..");
$.verbose = true;

const buildSteps = {
prepareDist,
addExports,
typescript: compileTs,
babelTransform,
updateVersion,
Expand All @@ -40,7 +43,6 @@ const buildSteps = {
postprocessDist,
verifyVersion,
verifySourceMaps,
prepareDist,
} satisfies BuildSteps;
const additionalSteps = {
prepareChangesetsRelease,
Expand Down
3 changes: 2 additions & 1 deletion config/compileTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const compileTs: BuildStep = async (options) => {
if (options.type === "esm") {
await $`npx tsc --outDir ${options.targetDir}`;
} else {
await $`npx tsc --outDir ${options.targetDir} --module commonjs`;
// for a `commonjs` output, we have to specify `moduleResulution: node`, and as that will error because it cannot verify some imports, we add `--noCheck`
await $`npx tsc --outDir ${options.targetDir} --module commonjs --moduleResolution node --noCheck`;
await renameJsFilesToCjs(options);
}
};
Expand Down
78 changes: 78 additions & 0 deletions config/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { readFile, writeFile } from "node:fs/promises";
import type { BuildStep } from "./build.ts";
import { entryPoints } from "./entryPoints.ts";
import path, { join } from "node:path";
import assert from "node:assert";

type ConditionRoot = {
types: {
import?: string;
require?: string;
};
import?: string;
"module-sync"?: string;
module?: string;
require?: string;
default?: string;
};

export const addExports: BuildStep = async (options) => {
const pkgFileName = join(options.packageRoot, "package.json");
const pkg = JSON.parse(await readFile(pkgFileName, "utf-8"));
for (const entryPoint of entryPoints) {
if (typeof entryPoint.value === "string") {
pkg.exports[entryPoint.key] = processEntryPoint(
entryPoint.value,
pkg.exports[entryPoint.key]
);
} else {
for (const [key, value] of Object.entries(entryPoint.value)) {
if (!pkg.exports[entryPoint.key]) {
pkg.exports[entryPoint.key] = {};
}
assert(
typeof value === "string",
"nesting of this complexity is not supported yet"
);
pkg.exports[entryPoint.key][key] = processEntryPoint(
value,
pkg.exports[entryPoint.key][key]
);
}
}
await writeFile(pkgFileName, JSON.stringify(pkg, null, 2));
}

function processEntryPoint(
value: string,
existing: ConditionRoot = { types: {} }
) {
value = value.replace(
/^.\/src/,
`.${options.targetDir.replace(/^dist/, "")}`
);

if (options.type === "esm") {
existing.types.import = value.replace(/\.ts$/, `.d.${options.tsExt}`);
const target = value.replace(/\.ts$/, `.${options.jsExt}`);
existing.module = target;
existing["module-sync"] = target;
//existing.import = target;
existing.default = target;
} else {
existing.types.require = value.replace(/\.ts$/, `.d.${options.tsExt}`);
existing.require = value.replace(/\.ts$/, `.${options.jsExt}`);
}
return JSON.parse(
JSON.stringify(existing, [
// ensure the order of keys is consistent
"types",
"module",
"module-sync",
"require",
"import",
"default",
])
);
}
};
10 changes: 7 additions & 3 deletions config/prepareDist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ import path from "node:path";
/* @apollo/client */

import pkg from "../package.json" with { type: "json" };
import * as entryPoints from "./entryPoints.ts";
import type { JSONSchemaForNPMPackageJsonFiles } from "./schema.package.json.ts";
import type { BuildStep } from "./build.ts";
import { mkdir } from "node:fs/promises";

// the generated `Person` type is a bit weird - `author` as a string is valid
const packageJson: Omit<JSONSchemaForNPMPackageJsonFiles, "author"> & {
author: string;
} = pkg;

export const prepareDist: BuildStep = (options) => {
export const prepareDist: BuildStep = async (options) => {
if (!options.first) return;

await mkdir(options.packageRoot, { recursive: true });

// The root package.json is marked as private to prevent publishing
// from happening in the root of the project. This sets the package back to
// public so it can be published from the "dist" directory.
Expand All @@ -40,7 +42,9 @@ export const prepareDist: BuildStep = (options) => {
delete packageJson.devEngines;
delete packageJson.devDependencies;

packageJson.exports = {};
packageJson.exports = {
"./package.json": "./package.json",
};

// The root package.json points to the CJS/ESM source in "dist", to support
// on-going package development (e.g. running tests, supporting npm link, etc.).
Expand Down
8 changes: 7 additions & 1 deletion config/processInvariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ export async function processInvariants(options: BuildStepOptions) {

if (
![
osPathJoin("utilities", "globals", `index.${options.jsExt}`),
osPathJoin(
`utilities`,
`globals`,
`environment`,
`index.${options.jsExt}`
),
osPathJoin(`utilities`, `globals`, `index.${options.jsExt}`),
osPathJoin("config", "jest", `setup.js`),
].includes(relativeSourcePath)
)
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@
"./utilities": "./src/utilities/index.ts",
"./utilities/subscriptions/relay": "./src/utilities/subscriptions/relay/index.ts",
"./utilities/subscriptions/urql": "./src/utilities/subscriptions/urql/index.ts",
"./utilities/globals": "./src/utilities/globals/index.ts"
"./utilities/globals": "./src/utilities/globals/index.ts",
"./utilities/globals/environment": {
"production": "./src/utilities/globals/environment/index.production.ts",
"development": "./src/utilities/globals/environment/index.development.ts",
"default": "./src/utilities/globals/environment/index.ts"
}
},
"scripts": {
"prebuild": "npm run clean",
Expand Down
1 change: 1 addition & 0 deletions src/utilities/globals/environment/index.development.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const __DEV__ = true;
1 change: 1 addition & 0 deletions src/utilities/globals/environment/index.production.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const __DEV__ = false;
2 changes: 2 additions & 0 deletions src/utilities/globals/environment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-ignore
export const __DEV__ = globalThis.__DEV__ !== false;
14 changes: 2 additions & 12 deletions src/utilities/globals/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import {
export {
invariant,
newInvariantError,
InvariantError,
} from "./invariantWrappers.js";
export { __DEV__ } from "@apollo/client/utilities/globals/environment";

export { maybe } from "./maybe.js";
export { default as global } from "./global.js";
export { invariant, newInvariantError, InvariantError };

/**
* @deprecated we do not use this internally anymore,
* it is just exported for backwards compatibility
*/
// this file is extempt from automatic `__DEV__` replacement
// so we have to write it out here
// @ts-ignore
export const DEV = globalThis.__DEV__ !== false;
export { DEV as __DEV__ };
2 changes: 1 addition & 1 deletion src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { DEV, maybe } from "./globals/index.js";
export { maybe } from "./globals/index.js";

export type {
DirectiveInfo,
Expand Down
19 changes: 14 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@
"noUnusedParameters": false,
"noUnusedLocals": true,
"skipLibCheck": true,
"moduleResolution": "node",
"moduleResolution": "NodeNext",
"importHelpers": true,
"sourceMap": true,
"inlineSources": true,
"declaration": true,
"declarationMap": true,
"target": "ESNext",
"module": "ESNext",
"module": "NodeNext",
"esModuleInterop": true,
"experimentalDecorators": true,
"outDir": "./dist",
"lib": ["es2015", "esnext.asynciterable"],
"rootDir": "./src",
"lib": [
"es2015",
"esnext.asynciterable"
],
"jsx": "react",
"strict": true
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["src/**/__tests__/**/*"]
"include": [
"src/**/*.ts",
"src/**/*.tsx"
],
"exclude": [
"src/**/__tests__/**/*"
]
}
Loading