feat: import maps#11615
Conversation
🦋 Changeset detectedLatest commit: 0ef72f8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
|
Converted back to draft as there's a flaw in this approach — as far as Rollup is concerned, things are still unstable (i.e. if a chunk that imports So we need to make sure that we, rather than Rollup, control the hashing. Gah |
|
Alright, fixed. Rollup controls the hashing if we're not using import maps, otherwise we do. It works beautifully |
benmccann
left a comment
There was a problem hiding this comment.
We could probably leave the entry chunk out of the import map as I don't believe there'd be any benefit to including it
I also wonder a bit whether we want to include the nodes for each +page.svelte in the import map or not. Including them will invalidate the entry chunk less often, but makes the import map quite a bit larger, which might affect how often people use it and the number of bytes sent with each initial page load
| if (secondary_build_started) return; // only run this once | ||
| async handler(options, bundle) { | ||
| if (secondary_build_started) { | ||
| if (svelte_config.kit.importMap.enabled) { |
There was a problem hiding this comment.
would this new chunk of code fit in a separate function? It's hard to tell on github whether it's accessing many things from outside its scope
| format: 'esm', | ||
| entryFileNames: ssr ? '[name].js' : `${prefix}/[name].[hash].${ext}`, | ||
| chunkFileNames: ssr ? 'chunks/[name].js' : `${prefix}/chunks/[name].[hash].${ext}`, | ||
| entryFileNames: ssr |
There was a problem hiding this comment.
could probably use a comment here about how we're controlling the hashing when import map is enabled and how it interacts with that
| }; | ||
| importMap?: { | ||
| /** | ||
| * Whether to generate import maps. This will result in better long term cacheability, as changes to a single module will no longer invalidate all its dependents. |
There was a problem hiding this comment.
is there any guidance we can offer as to when this should be done? like maybe it's worth it until the site grows to a certain size?
|
There's a Vite PR that implements using importmaps too: vitejs/vite#15373. Could be useful to cross-reference and even work on something builtin. |
This already happens — the Though I just realised that the import{c as a}from'chunks/entry';export{a as start};The reason for that is so that other stuff in One related note — the entry chunk is always invalidated because it references
Ooh, interesting. One consideration here is that we always need to load the entry chunk before doing any other work, so making that chunk more stable is helpful. We could increase its stability (and decrease its size) relative to its current state (but not relative to the case where It's very hard to know exactly which levers to pull here! I guess we could make it configurable (which is why I went for
Oh, interesting. Would definitely be nice to use something built-in, especially since this PR is doing some slightly hacky stuff after the bundle has been created. Some thoughts on that PR:
|
Thanks for reviewing that. I haven't quite dig into it, but seems like you're right with that about the
I'm thinking Vite could expose some additional information in the SSR manifest that may help with this. But good point too so we make sure SSR frameworks can make use of this too. |
|
Thank you both for reviewing! 😀
Yes, I've checked it and it does work! The steps I followed to confirm
# Vite repo, switch to bhbs:chunkMap
# pnpm build
cd playground/dynamic-import
pnpm run build | sort > before.log
sed -i '' "s/'hello'/'hi'/" nested/hello.js
pnpm run build | sort > after.log
diff before.log after.logresult: From index.html, index.js is imported, and it has been confirmed that while the hash of index.js remains unchanged, only the hash of the modified file at the end of the file tree, hello.js, has changed 🎉 After replacing with static IDs, the hash calculation is performed on the altered content, creating a situation where the content hash of the entry itself does not change. This is my understanding 🤔
If we handle things in renderChunk inside Vite, as long as we properly process the sourcemap at that step, we can leave the rest to Rollup. For now, I've confirmed that the existing sourcemap tests in Vite are passing.
Sounds good 😀 |
|
@Rich-Harris I'm closing this because it has numerous conflicts and probably would be easier to start over with -- also, the Vite PR doing the same thing hasn't merged yet (last activity about a year ago). Happy to re-pursue this sometime in the future, or you can reopen it if there's a reason to keep it open. |
This adds import map support to SvelteKit. If you add the relevant config...
...SvelteKit will generate an import map for your
.jsfiles, and rewrite import declarations (and dynamic imports) to point to the identifiers in the import map.It will also disable
modulepreloadHTTP headers in favour of<link>elements in the HTML, since the import map must precede any preloads.The reason you'd want to enable this is that it makes your assets more cacheable. Today, if multiple pages import the same component or module from
src/lib(for example), then any changes to that module will cause all the page chunks to be invalidated as well as the module itself. With import maps this is no longer necessary, since the pages import the module via a stable identifier. (There's a caveat here in that Rollup can choose to chunk things up however it likes — if the overall structure of the bundle changes then things will be invalidated. Most changes aren't like that, however.)Demo here — right now,
styles are missing when you navigate client-side,the theme toggle switch doesn't work, for some reason.Closes #4482.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits