Skip to content

fix(build): preserve preload deps in chunkImportMap when base is not / (fix #23350) - #23361

Open
santusht06 wants to merge 2 commits into
vitejs:mainfrom
santusht06:fix/chunk-importmap-base-23350
Open

fix(build): preserve preload deps in chunkImportMap when base is not / (fix #23350)#23361
santusht06 wants to merge 2 commits into
vitejs:mainfrom
santusht06:fix/chunk-importmap-base-23350

Conversation

@santusht06

Copy link
Copy Markdown

Description

Fixes #23350.

When build.chunkImportMap is enabled and base is configured to a non-root path (e.g. base: '/sub/'):

  1. Rolldown prefixes the chunk import map keys with baseUrl: base (e.g. /sub/assets/...).
  2. When reading the map back in getImportMapBaseUrl(), it checked options.build.rolldownOptions.experimental?.chunkImportMap?.baseUrl and defaulted to '/'.
  3. Stripping length 1 left keys as 'sub/assets/...', which mismatched bundle keys ('assets/...').
  4. As a result, the lookup in importAnalysisBuild.ts missed, dropping the __vite__mapDeps dependency list from lazy chunks and leaving lazy CSS un-preloaded.

Solution

  • Update getImportMapBaseUrl in packages/vite/src/node/plugins/html.ts to return (options as any).base ?? '/' when chunkImportMap.baseUrl is not explicitly overridden on rolldownOptions.
  • Added unit test in packages/vite/src/node/__tests__/build.spec.ts ensuring preload dependency mappings are preserved with non-root base.

Validation

  • pnpm run build
  • pnpm vitest run packages/vite/src/node/__tests__/build.spec.ts (68/68 passed)
  • pnpm run typecheck (0 errors)
  • pnpm run lint (0 errors)
  • Verified with minimal reproduction repository.

@official-burak official-burak left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fallback is the right idea. resolveRolldownOptions injects baseUrl: base only into the object it passes to Rolldown, so environment.config.build.rolldownOptions.experimental.chunkImportMap never gets it unless the user set it themselves. Reading chunkImportMap.baseUrl from that config will keep missing, which is why the strip falls back to '/' and the preload map drops.

ResolvedEnvironmentOptions already has base?: string, so this does not need as any:

return options.base ?? '/'

A second fixture with base: './' would be useful. That is the other value Vite uses, and k.slice(baseUrl.length) depends on the exact prefix Rolldown wrote.

@santusht06

Copy link
Copy Markdown
Author

Thanks for the review @official-burak!

Updated in latest commit:

  • Removed as any by typing options: ResolvedEnvironmentOptions & { base?: string }.
  • Added test case for base: './' (relative base).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

build.chunkImportMap drops module preload dependency lists when base is not /

2 participants