undefinedNote: This plugin is still under development and could have bugs. Current version offers only basic functionality.
Extend and override any Astro project files using a layered architecture - perfect for themes, white-labeling, and feature variations.
This package allows you to create multiple layers of files that override your base Astro application, similarly on how ti works at Nuxt.js. Think of it like CSS cascading - each layer can override any file from your source code or previous layers, while keeping the rest intact. This includes pages, components, layouts, styles, public assets, and any other project files.
undefinedKey Features:undefined
For example, you can have a base e-commerce site and create different layers for:
Install the plugin using your preferred package manager:
pnpm install astro-layers
Add the plugin to your astro.config.mjs:
import layers from 'astro-layers';
export default defineConfig({
plugins: [layers()],
//...
});
Add .layers to your .gitignore:
# Astro Layers
.layers
Now, create a layers directory in the root of your project and add some layers to it. Every file you put in a layer will override the default one in src folder.
project-root/
layers/
layer-1/
pages/
index.astro # This will override default index.astro
src/
pages/
index.astro
Layers are processed in alphabetical order. To control the priority, prefix your layer directories with numbers:
your-project/
βββ layers/
β βββ 1.base/
β β βββ pages/
β β βββ index.astro
β βββ 2.theme/
β β βββ pages/
β β βββ index.astro
β βββ 3.premium/
β βββ pages/
β βββ index.astro
βββ src/
βββ pages/
βββ index.astro
In this example:
1.base will be checked first2.theme will be checked second3.premium will be checked lastThis naming convention makes it easy to:
1.5.feature)layers/
βββ 1.base/ # Base components and layouts
βββ 2.theme/ # Theme-specific overrides
βββ 3.features/ # Feature-specific changes
βββ 4.customization/ # Customer-specific customizations
You can use layers from external sources like npm packages or git repositories. External layers follow the same naming convention as local layers to control priority.
Configure external layers in your astro.config.mjs:
import layers from 'astro-layers';
export default defineConfig({
plugins: [
layers({
external: {
'1.base-theme': 'npm:astro-base-theme',
'2.premium': 'git:username/repo',
'3.custom': 'git:username/repo#branch'
}
})
],
});
The keys (e.g., 1.base-theme) determine the layerβs priority, following the same numbering convention as local layers. Sources can be prefixed with:
npm: for npm packagesgit: for GitHub repositoriesundefinedImportant: External layers must only contain a
srcdirectory structure as Astro Layers only overrides files within thesrcfolder. Any other files or directories will be ignored.
your-project/
βββ layers/
β βββ 1.core/
β βββ 4.customization/
βββ .layers/
βββ .external/
βββ 2.base-theme/ # from npm:astro-base-theme
βββ 3.premium/ # from git:username/repo
In this example, layers will be applied in this order:
1.core2.base-theme3.premium4.customizationWhile the original idea for this package is originating from article I wrote long time ago on Vue School, the name is borrowed from Nuxt.js Layers.
Huge thanks to Anthony Fu for creating amazing TS library starter that this package is using.
MIT License Β© 2024-PRESENT Filip Rakowski