Skip to content

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Oct 19, 2025

Description

This PR adds import.meta.resolve support to bundle config loader, applying the same logic from #20260.

This PR only adds support for ESM configs (config files ending with .mjs / .mts or has type: module in the closest package.json. Supporting this in CJS files requires a bit more code as import.meta.resolve is not available in CJS files.

fixes #20326

@sheremet-va
Copy link
Member

sheremet-va commented Oct 19, 2025

Just a note, but in Vitest we noticed that module.register takes a long time and it would not be good for us to use the built-in import.meta.resolve support for tests, but I wonder if you could also support module.registerHooks which is faster?

@sapphi-red sapphi-red added this to the 7.2 milestone Oct 20, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for import.meta.resolve in ESM Vite configuration files by integrating the custom import meta resolver from the module runner. The implementation reuses the resolver logic previously added for runtime module evaluation and injects it into the bundled config files.

Key Changes

  • Integrates createImportMetaResolver and custom hook string from module-runner into the config bundler
  • Adds import.meta.resolve to the list of replaced import.meta properties during config bundling
  • Includes test coverage for the new functionality with an ESM config fixture

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/vite/src/node/config.ts Adds import.meta.resolve injection logic to bundleConfigFile with ESM/CJS detection
packages/vite/src/node/tests/fixtures/config/import-meta/vite.config.ts Adds test case using import.meta.resolve
packages/vite/src/node/tests/fixtures/config/import-meta/package.json Marks test fixture as ESM module
packages/vite/src/node/tests/config.spec.ts Updates test to verify import.meta.resolve behavior
packages/vite/src/module-runner/importMetaResolver.ts Exports reusable string template for import.meta.resolve injection
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

@sapphi-red sapphi-red merged commit f86789a into vitejs:main Oct 28, 2025
16 checks passed
@sapphi-red sapphi-red deleted the feat/import-meta-resolve-esm-config-bundle-support branch October 28, 2025 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-nice-to-have Not breaking anything but nice to have (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vite hijacks import.meta.resolve resulting in broken configuration files

2 participants