Skip to content

@ogygia/barrels

Rules

Which barrels barrels() goes around, and the options that change it. A pure barrel in your workspace is rewritten with no setup; force, keep, packages, followPackages and importers handle impure barrels, dependencies and the files to leave alone.

On this page

barrels() only goes around a barrel when doing so can’t change what your app does. For your own code it decides that by reading each barrel. For everything else, you tell it with options.

import { 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({
      packages: ['@acme/ui'],
      force: ['$lib/components'],
      keep: ['$lib/stores']
    }),
    sveltekit()
  ]
});

Pure barrels

A barrel is pure when it contributes no code of its own. Every top-level statement is one of:

  • an import (but not a bare import 'x', which runs something);
  • a re-export: export { A } from './a', export * from './b', export * as c from './c';
  • an export { A } of something it imported;
  • a type: type, interface, declare, and export type;
  • export {}.

Skipping a pure barrel changes nothing, so barrels() goes around it. A barrel with anything else (a const, a function call, an export default of an expression, a namespace import it doesn’t re-export) is impure: going around it would skip that code. An import of an impure barrel is left as written, unless you force it.

// pure: only re-exports and types
export { default as Button } from './Button.svelte';
export { default as Card } from './Card.svelte';
export * from './icon';
export type { ButtonProps } from './types';

force

force lists impure barrels to rewrite anyway. The names move to their files as usual, and the importer keeps a bare import 'barrel' so the barrel’s own code still runs. Use it when that code is cheap or must run once anyway, such as a call that registers custom elements.

// you wrote, from an impure barrel listed in force:
import { Button } from '$lib/components';

// barrels() rewrote it to:
import Button from '/…/src/lib/components/Button.svelte';
import '$lib/components';

keep

keep lists modules whose imports are never rewritten, whatever else says, such as a barrel that creates a store the app depends on being imported from that one place. It wins over force and packages.

packages

Dependencies, the packages in node_modules, are opt-in, through packages. In dev, Vite pre-bundles a package’s bare imports, so a leaf import that skips the optimizer while another importer gets the pre-bundled copy loads two instances of one module. List a package when you know that’s safe: an ESM component library that’s excluded from optimizeDeps.

A plain string matches the specifier and every subpath ('@acme/ui' covers @acme/ui/components).

In a monorepo, packages linked from your workspace aren’t in node_modules by their real path, so they’re your own code: no listing needed. The workspace root is found from Vite’s root, the way Vite finds it.

import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { barrels } from '@ogygia/barrels';
import { defineConfig } from 'vite';

export default defineConfig({
  optimizeDeps: { exclude: ['@acme/ui'] },
  plugins: [
    ogygia(),
    barrels({ packages: ['@acme/ui'] }),
    sveltekit()
  ]
});

followPackages

While building a barrel’s map, export * from can lead into a package. With followPackages: true (the default), barrels() reads through it, so a name a package re-exports is placed at its real file. With false, a star that reaches node_modules makes the barrel opaque for the names it can’t see: those stay on the barrel import, and the rest still move.

importers

importers: { include, exclude } picks the files whose imports are rewritten. By default that’s every script and component outside node_modules. exclude takes files out of that set. include, when given, replaces it: only the files it matches are rewritten, node_modules included if it matches them.

Use one or the other. With include set, exclude is ignored today, so leave the files you don’t want out of include instead (#2313).

import { 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({
      importers: { exclude: [/\.test\.ts$/] }
    }),
    sveltekit()
  ]
});

Matchers

packages, force, keep, importers.include and importers.exclude each take a list of matchers. A matcher is tested against the module’s resolved file path and against the specifier as you wrote it:

  • A plain string matches the specifier exactly or as a prefix ('@acme/ui' matches @acme/ui/components), or a path segment of the file ('components' matches /…/components/index.ts).
  • A string with * is a glob over the whole specifier or path, where * matches anything ('@acme/*').
  • A regular expression is tested against both.
  • A function gets each in turn and returns true to match.

For importers, the file path is both, since there’s no specifier.

import { 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({
      packages: ['@acme/*', /\/components\/index\.js$/],
      keep: [(id) => id.endsWith('/stores/index.ts')]
    }),
    sveltekit()
  ]
});