@ogygia/barrels
Barrels
@ogygia/barrels rewrites barrel imports to the files they name while Vite transforms your code, so dev transforms only the modules a file uses and an island's chunk holds only the components it imports.
A barrel is a module that only re-exports others: an index.ts beside forty components, or an export * from fan-out. Importing one name from it pulls in its whole graph. In dev, Vite transforms and serves every module behind it. In a build, the chunk graph follows it, and a package that isn’t sideEffects: false keeps all of it. For an island, that can mean shipping the code of thirty components to use one.
barrels() from @ogygia/barrels fixes that at the source. While Vite transforms each file, it rewrites import { Button } from '$lib/components' to an import of the file that defines Button, before Svelte or ogygia see the file. The graph is then only the leaves the file uses.
// you wrote:
import { Button, Icon } from '$lib/components';
// barrels() rewrote it to:
import Button from '/…/src/lib/components/Button.svelte';
import { Icon } from '/…/src/lib/components/icon.ts';Installation
Install the package and list its plugin beside ogygia(), before sveltekit(). Note the import path: the factory comes from the package root, not /vite.
Your own barrels need no listing. Every module in your workspace outside node_modules is a candidate, and barrels() decides from each barrel’s code whether it’s safe to go around (Rules). barrels(true) is the same as barrels(). The old ogygia({ barrels }) option is gone: it fails the build as an unknown option (option_unknown), whose page names barrels().
pnpm add @ogygia/barrelsimport { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { barrels } from '@ogygia/barrels';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [ogygia(), barrels(), sveltekit()]
});What gets rewritten
An import is rewritten when it names things a barrel re-exports, and the barrel is pure: every top-level statement is an import, a re-export, a type, or export {}. Each name then goes to the file that defines it, one import line per file:
- the local names stay as you wrote them, so the rest of the file doesn’t change;
- a name re-exported as a default (
export { default as Button } from './Button.svelte') becomes a default import; typemodifiers and import attributes are kept on every new line;- re-exports through several barrels are followed to the end;
- once every name has moved, the barrel import is gone.
Names the barrel can’t place stay on it, in one import of their own, while the rest move.
What stays
These imports are never rewritten:
- Region imports. An import with region marks (
with { wake: 'load' }), on a default or a named import, keeps its specifier, so the region means what it meant. import * as ns, which takes the module whole by design.import 'x', which runs the module for its effects.import type, which is erased anyway.- Dynamic
import(). - Dependencies that you don’t list in
packages(packages).
And these names stay on their barrel: a name the barrel defines itself, a name two export * lines both give, and a name behind a module that can’t be read (JSON, CommonJS, a ?raw import).
<script lang="ts">
// rewritten: Button goes to its own file
import { Button } from '$lib/components';
// kept as written: this import marks a region
import { Chart } from '$lib/components' with {
wake: 'visible'
};
// kept as written: a namespace import
import * as icons from '$lib/icons';
</script>
<Button>Save</Button>
<Chart />
<icons.Star />Dev and build
barrels() runs in dev and in the build, on every file outside node_modules that’s a script (.js, .ts, .jsx, .tsx, and their .m/.c forms), a .svelte component or a .svelte.ts module. In a component it reads the <script> blocks only. In dev it watches each barrel it went around, so an edit to the barrel still updates the files that imported through it.
At the end of each build leg, it prints a short report of what it moved (Build report). The devtools use the same pass to say why an island still imports a barrel whole.
Integrations
- Devtools. The
island-barrelfinding names a barrel an island still imports whole. Withbarrels()listed, it also says why the pass left it and the fix (Page findings). - Client islands. A region marked on a barrel import stays as written; import the region from its own file if you want its barrel gone too.
- Performance. Cutting barrel imports is one of the settings that ship less JavaScript.
Package contents
| Page | What it covers |
|---|---|
| Rules | Pure and impure barrels, force, keep, packages, followPackages, importers, and how names match. |
| Build report | The end-of-build report, report, debug, skipped components, and why a barrel stayed. |
@ogygia/barrels | The generated API reference for barrels() and its options. |