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

add "edge-light" exports condition #263

Merged
merged 2 commits into from
Apr 3, 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
39 changes: 33 additions & 6 deletions packages/client-react-streaming/package-shape.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"InMemoryCache",
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"registerApolloClient"
"registerApolloClient",
"built_for_rsc"
],
"browser": [
"ApolloClient",
Expand All @@ -16,7 +17,8 @@
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"WrapApolloProvider",
"resetApolloSingletons"
"resetApolloSingletons",
"built_for_browser"
],
"node": [
"ApolloClient",
Expand All @@ -26,12 +28,37 @@
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"WrapApolloProvider",
"resetApolloSingletons"
"resetApolloSingletons",
"built_for_ssr"
],
"edge-light,worker,browser": [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the order of conditions that Next.js is using, so we check for that explicitly.

See

https://github.com/vercel/next.js/blob/bbd79ac9977e03010e09ff90d933f2ec614b031f/packages/next/src/build/webpack-config.ts#L96-L103

"ApolloClient",
"DataTransportContext",
"DebounceMultipartResponsesLink",
"InMemoryCache",
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"WrapApolloProvider",
"resetApolloSingletons",
"built_for_ssr"
]
},
"@apollo/client-react-streaming/manual-transport": {
"react-server": [],
"browser": ["buildManualDataTransport", "resetManualSSRApolloSingletons"],
"node": ["buildManualDataTransport", "resetManualSSRApolloSingletons"]
"react-server": ["built_for_other"],
"browser": [
"buildManualDataTransport",
"resetManualSSRApolloSingletons",
"built_for_browser"
],
"node": [
"buildManualDataTransport",
"resetManualSSRApolloSingletons",
"built_for_ssr"
],
"edge-light,worker,browser": [
"buildManualDataTransport",
"resetManualSSRApolloSingletons",
"built_for_ssr"
]
}
}
4 changes: 4 additions & 0 deletions packages/client-react-streaming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
"require": {
"types": "./dist/combined.d.cts",
"react-server": "./dist/index.rsc.cjs",
"edge-light": "./dist/index.ssr.cjs",
"browser": "./dist/index.browser.cjs",
"node": "./dist/index.ssr.cjs"
},
"import": {
"types": "./dist/combined.d.ts",
"react-server": "./dist/index.rsc.js",
"edge-light": "./dist/index.ssr.js",
"browser": "./dist/index.browser.js",
"node": "./dist/index.ssr.js"
}
Expand All @@ -62,12 +64,14 @@
"require": {
"types": "./dist/manual-transport.ssr.d.cts",
"react-server": "./dist/empty.cjs",
"edge-light": "./dist/manual-transport.ssr.cjs",
"browser": "./dist/manual-transport.browser.cjs",
"node": "./dist/manual-transport.ssr.cjs"
},
"import": {
"types": "./dist/manual-transport.ssr.d.ts",
"react-server": "./dist/empty.js",
"edge-light": "./dist/manual-transport.ssr.js",
"browser": "./dist/manual-transport.browser.js",
"node": "./dist/manual-transport.ssr.js"
}
Expand Down
5 changes: 5 additions & 0 deletions packages/client-react-streaming/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export default defineConfig((options) => {
entry: {
[output]: input,
},
footer(ctx) {
return {
js: ctx.format === 'esm' ? `export const built_for_${env} = true;` : `exports.built_for_${env} = true;`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm understanding the significance of the build_for_<env> variable here. Could you explain that?

Copy link
Member Author

@phryneas phryneas Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes the SSR and Browser bundle have the same shape, so I'm deliberately giving them different shapes by adding an additional environment-specific export, so I can safely distinguish between them in the package-shape.json test.

Especially for the edge-light condition, I needed a way to safely distinguish between the SSR build and the Browser build, because they "looked the same".

}
},
};
}

Expand Down
31 changes: 25 additions & 6 deletions packages/experimental-nextjs-app-support/package-shape.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"react-server": [
"DebounceMultipartResponsesLink",
"RemoveMultipartDirectivesLink",
"SSRMultipartLink"
"SSRMultipartLink",
"built_for_rsc"
],
"browser": [
"ApolloNextAppProvider",
Expand All @@ -17,7 +18,8 @@
"useFragment",
"useQuery",
"useReadQuery",
"useSuspenseQuery"
"useSuspenseQuery",
"built_for_browser"
],
"node": [
"ApolloNextAppProvider",
Expand All @@ -31,12 +33,29 @@
"useFragment",
"useQuery",
"useReadQuery",
"useSuspenseQuery"
"useSuspenseQuery",
"built_for_ssr"
],
"edge-light,worker,browser": [
"ApolloNextAppProvider",
"DebounceMultipartResponsesLink",
"NextSSRApolloClient",
"NextSSRInMemoryCache",
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"resetNextSSRApolloSingletons",
"useBackgroundQuery",
"useFragment",
"useQuery",
"useReadQuery",
"useSuspenseQuery",
"built_for_ssr"
]
},
"@apollo/experimental-nextjs-app-support/rsc": {
"react-server": ["registerApolloClient"],
"browser": [],
"node": []
"react-server": ["registerApolloClient", "built_for_rsc"],
"browser": ["built_for_other"],
"node": ["built_for_other"],
"edge-light,worker,browser": ["built_for_other"]
}
}
8 changes: 4 additions & 4 deletions packages/experimental-nextjs-app-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
"require": {
"types": "./dist/rsc/index.d.cts",
"react-server": "./dist/rsc/index.cjs",
"browser": "./dist/empty.cjs",
"node": "./dist/empty.cjs"
"default": "./dist/empty.cjs"
},
"import": {
"types": "./dist/rsc/index.d.ts",
"react-server": "./dist/rsc/index.js",
"browser": "./dist/empty.js",
"node": "./dist/empty.js"
"default": "./dist/empty.js"
}
},
"./ssr": {
"require": {
"types": "./dist/ssr/index.ssr.d.cts",
"react-server": "./dist/ssr/index.rsc.cjs",
"edge-light": "./dist/ssr/index.ssr.cjs",
"browser": "./dist/ssr/index.browser.cjs",
"node": "./dist/ssr/index.ssr.cjs"
},
"import": {
"types": "./dist/ssr/index.ssr.d.ts",
"react-server": "./dist/ssr/index.rsc.js",
"edge-light": "./dist/ssr/index.ssr.js",
"browser": "./dist/ssr/index.browser.js",
"node": "./dist/ssr/index.ssr.js"
}
Expand Down
8 changes: 8 additions & 0 deletions packages/experimental-nextjs-app-support/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export default defineConfig((options) => {
entry: {
[output]: input,
},
footer(ctx) {
return {
js:
ctx.format === "esm"
? `export const built_for_${env} = true;`
: `exports.built_for_${env} = true;`,
};
},
};
}

Expand Down
16 changes: 12 additions & 4 deletions scripts/verify-package-shape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ async function checkPackage(/** @type {string} */ pkg) {
* @param {string[]} shape
*/
async function verifyESM(condition, entryPoint, pkg, shape) {
console.log(`Checking ESM: ${entryPoint} with condition ${condition}`);
const conditionFlags = condition
.split(",")
.map((c) => `--conditions ${c}`)
.join(" ");
console.log(`Checking ESM: ${entryPoint} with ${conditionFlags}`);
const child = exec(
`node --input-type=module --conditions ${condition} --eval 'console.log(JSON.stringify(Object.keys(await import("${entryPoint}"))))'`,
`node --input-type=module ${conditionFlags} --eval 'console.log(JSON.stringify(Object.keys(await import("${entryPoint}"))))'`,
{
cwd: dirname(pkg),
}
Expand All @@ -61,9 +65,13 @@ async function verifyESM(condition, entryPoint, pkg, shape) {
* @param {string[]} shape
*/
async function verifyCJS(condition, entryPoint, pkg, shape) {
console.log(`Checking CJS: ${entryPoint} with condition ${condition}`);
const conditionFlags = condition
.split(",")
.map((c) => `--conditions ${c}`)
.join(" ");
console.log(`Checking CJS: ${entryPoint} with ${conditionFlags}`);
const child = exec(
`node --input-type=commonjs --conditions ${condition} --eval 'console.log(JSON.stringify(Object.keys(require("${entryPoint}"))))'`,
`node --input-type=commonjs ${conditionFlags} --eval 'console.log(JSON.stringify(Object.keys(require("${entryPoint}"))))'`,
{
cwd: dirname(pkg),
}
Expand Down
Loading