feat(rsc): validate client-only and server-only import during resolve#624
Conversation
|
requirements
validateImportPlugin({
"client-only": `'client-only' is included in server build`,
"server-only": true,
}),
// https://github.com/vercel/next.js/blob/90f564d376153fe0b5808eab7b83665ee5e08aaf/packages/next/src/build/webpack-config.ts#L1249-L1280
// https://github.com/pcattori/vite-env-only/blob/68a0cc8546b9a37c181c0b0a025eb9b62dbedd09/src/deny-imports.ts
// https://github.com/sveltejs/kit/blob/84298477a014ec471839adf7a4448d91bc7949e4/packages/kit/src/exports/vite/index.js#L513
function validateImportPlugin(entries: Record<string, string | true>): Plugin {
return {
name: validateImportPlugin.name,
enforce: "pre",
resolveId(source, importer, options) {
const entry = entries[source];
if (entry) {
// skip validation during optimizeDeps scan since for now
// we want to allow going through server/client boundary loosely
if (
entry === true ||
manager.buildType === "scan" ||
("scan" in options && options.scan)
) {
return "\0virtual:validate-import";
}
throw new Error(entry + ` (importer: ${importer ?? "unknown"})`);
}
return;
},
load(id, _options) {
if (id === "\0virtual:validate-import") {
return "export {}";
}
return;
},
};
} |
…tion Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
I've implemented the validateImportPlugin with the requirements you specified: ✅ New plugin option: Added Implementation details:
Usage: rsc({
validateImports: true, // default
// ... other options
})The plugin will now throw descriptive errors like:
Commit: 48f0449 |
hi-ogawa
left a comment
There was a problem hiding this comment.
Add e2e test. See packages/plugin-rsc/CONTRIBUTING.md
Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
hi-ogawa
left a comment
There was a problem hiding this comment.
Add e2e test. See packages/plugin-rsc/CONTRIBUTING.md
Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
Added comprehensive e2e tests for the validateImportPlugin functionality. The tests cover:
The e2e tests use Commit: 30c8fc0 |
Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
| 'package.json': { | ||
| edit: (content) => { | ||
| const pkg = JSON.parse(content) | ||
| pkg.dependencies = { | ||
| ...pkg.dependencies, | ||
| 'server-only': '^0.0.1', | ||
| } | ||
| return JSON.stringify(pkg, null, 2) | ||
| }, | ||
| }, |
There was a problem hiding this comment.
See https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-rsc/CONTRIBUTING.md#tips
Dependencies for temp test projects are managed in examples/e2e/package.json
There was a problem hiding this comment.
Fixed the dependency management as requested. The dependencies (client-only and server-only) are now managed centrally in examples/e2e/package.json. The e2e tests have been updated to use proper package.json overrides (similar to setupIsolatedFixture) to ensure temporary test projects can resolve dependencies correctly.
All 4 validateImportPlugin e2e tests are now passing with proper dependency management that follows the recommended pattern.
Commit: e762f57
…tion Co-authored-by: hi-ogawa <4232207+hi-ogawa@users.noreply.github.com>
client-only and server-only import during build
client-only and server-only import during buildclient-only and server-only import during resolve
commit: |
client-onlyandserver-onlyimport validation #620Horrible job by copilot. Mostly rewritten by me.
TODO
@vitejs/plugin-rscwakujs/waku#1493