-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: import maps #11615
base: main
Are you sure you want to change the base?
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -612,8 +614,16 @@ async function kit({ svelte_config }) { | |||
input, | |||
output: { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could probably use a comment here about how we're controlling the hashing when import map is enabled and how it interacts with that
@@ -414,6 +414,15 @@ declare module '@sveltejs/kit' { | |||
*/ | |||
errorTemplate?: string; | |||
}; | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.log result:
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 😀 |
This adds import map support to SvelteKit. If you add the relevant config...
...SvelteKit will generate an import map for your
.js
files, and rewrite import declarations (and dynamic imports) to point to the identifiers in the import map.It will also disable
modulepreload
HTTP 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 test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits