Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 25, 2025

@hi-ogawa
Copy link
Contributor

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;
    },
  };
}

Copy link
Contributor Author

Copilot AI commented Jul 26, 2025

requirements

I've implemented the validateImportPlugin with the requirements you specified:

New plugin option: Added validateImports?: boolean to RscPluginOptions (enabled by default)
Reference implementation: Based on the code you provided, adapted for the RSC plugin structure

Implementation details:

  • Validates client-only imports are blocked in server environments (rsc, ssr)
  • Validates server-only imports are blocked in client environment
  • Skips validation during dependency scanning to avoid build issues
  • Uses existing isScanBuild variable for scan detection
  • Includes comprehensive unit tests

Usage:

rsc({
  validateImports: true, // default
  // ... other options
})

The plugin will now throw descriptive errors like:

  • 'client-only' is included in server build (importer: unknown)
  • 'server-only' is included in client build (importer: unknown)

Commit: 48f0449

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

Add e2e test. See packages/plugin-rsc/CONTRIBUTING.md

Copilot AI requested a review from hi-ogawa July 26, 2025 01:34
Copilot finished work on behalf of hi-ogawa July 26, 2025 01:34
Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

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

Add e2e test. See packages/plugin-rsc/CONTRIBUTING.md

Copy link
Contributor Author

Copilot AI commented Jul 26, 2025

Add e2e test. See packages/plugin-rsc/CONTRIBUTING.md

Added comprehensive e2e tests for the validateImportPlugin functionality. The tests cover:

  1. Build fails when server-only is imported in client component
  2. Build fails when client-only is imported in server component
  3. Build succeeds with valid imports
  4. Build succeeds with invalid imports when validation is disabled

The e2e tests use setupInlineFixture pattern as recommended in the CONTRIBUTING.md guide and validate the expected error messages and build behavior.

Commit: 30c8fc0

Copilot AI requested a review from hi-ogawa July 26, 2025 01:50
Copilot finished work on behalf of hi-ogawa July 26, 2025 01:50
Copilot finished work on behalf of hi-ogawa July 26, 2025 01:55
Comment on lines 13 to 22
'package.json': {
edit: (content) => {
const pkg = JSON.parse(content)
pkg.dependencies = {
...pkg.dependencies,
'server-only': '^0.0.1',
}
return JSON.stringify(pkg, null, 2)
},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copilot AI requested a review from hi-ogawa July 26, 2025 02:23
Copilot finished work on behalf of hi-ogawa July 26, 2025 02:23
@hi-ogawa hi-ogawa changed the title [WIP] fix @vitejs/vite-plugin-react/issues/620 feat(rsc): validate client-only and server-only import during build Jul 26, 2025
@hi-ogawa hi-ogawa marked this pull request as ready for review July 26, 2025 06:57
@hi-ogawa hi-ogawa changed the title feat(rsc): validate client-only and server-only import during build feat(rsc): validate client-only and server-only import during resolve Jul 26, 2025
@hi-ogawa hi-ogawa added the trigger: preview Trigger pkg.pr.new label Jul 26, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jul 26, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@vitejs/plugin-react@624
npm i https://pkg.pr.new/@vitejs/plugin-react-oxc@624
npm i https://pkg.pr.new/@vitejs/plugin-rsc@624
npm i https://pkg.pr.new/@vitejs/plugin-react-swc@624

commit: e8879d9

hi-ogawa added a commit to hi-ogawa/waku that referenced this pull request Jul 26, 2025
@hi-ogawa hi-ogawa merged commit 47d02d0 into main Jul 26, 2025
25 checks passed
@hi-ogawa hi-ogawa deleted the copilot/fix-d57593a5-2c47-481f-9623-1a4fd3279339 branch July 26, 2025 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

trigger: preview Trigger pkg.pr.new

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support built-time client-only and server-only import validation

2 participants