The crates of Dodi’s booking app: load each page with a static import, import() or import(variable). A model of Rolldown’s cut held to 27 Vite 8 builds splits…
Why it matters
A fat first crate means a slow first view, especially on a phone on a slow network. Splitting per page with import() (and React.lazy) gives a lot for a little, as long as what is split off is big and rarely opened, and the bundler can read where to cut.
The key idea
An import() with a fixed string is the line where the bundler cuts
Common mistakes
The Network tab fills with tiny requests, slower than before splitting Each crate is a request, and a crate only known after another has run waits one more trip. Leave what is small and almost always opened in the first crate; split off what is big and rarely opened.
A blank screen for a moment each time a lazy crate is fetched Until its crate arrives and its modules run, a lazy page is not there yet. Wrap the React.lazy component in <Suspense fallback={…}> and give it a skeleton or a spinner.
import(path) works in dev, but the bundler does not cut it into a crate A bundler only cuts at an import() whose text it can read. import('./routes/history.js') works; ` import(./routes/${name}.js) works in Vite (the pattern is read into a list of files); import(FILES[name])` does not: the file is left out of the build, and Vite says nothing.
Try it in this playground
See everything in one crate Without import(), the app is one big crate.
Open a lazy page Open a page loaded by import(): its crate is only sent then.
Make a shared crate appear Two lazy pages that use format.js.
Make two trips in a row Split Home too, then watch the first page wait.