[13.x] Add support for Vite's chunk import map - #60874
Conversation
|
@julesjanssen Thanks for this! Any reason behind |
|
The |
|
Agent review surfaced a situation for CDN based deployments that may warrant consideration. Transform each Vite-generated import-map target through the same resolver used for module tags. the inline map should become: while leaving the stable specifier key unchanged, because that is what Vite writes into import statements. The actual version should pass the effective $buildDirectory into the helper, since __invoke() can override it. It should also add a test asserting that an https://cdn.example.com asset URL is present in both the entry <script> and import-map target. |
Adds support for Vite's
build.chunkImportMapoption.With
chunkImportMapenabled, Vite makes generated chunks import one another through stable identifiers instead of hashed filenames, and emits animportmap.jsonmapping those identifiers to the real files. This improves long-term caching: changing one chunk no longer cascades new hashes onto every chunk that imports it.For it to work, a
<script type="importmap">must appear before the module scripts. Vite injects it when it controls the HTML, but Laravel renders its own tags from the manifest. Currently, enablingchunkImportMapsilently breaks module resolution.This PR adds import-map injection to the
Viteclass:public/build/importmap.jsonexists,@viteemits<script type="importmap">…</script>ahead of the modulepreload links and module scripts.chunkImportMap).Vite::useImportMapFilename()for parity withuseManifestFilename(), in case the file is renamed in the Vite config.Usage:
Tests cover injection, ordering (import map before the module scripts), the CSP nonce, and the custom filename.
Note:
chunkImportMapis still marked experimental in Vite.