Heavily inspired by eleventy-plugin-vue.
npm install eleventy-plugin-svelte
ELEVENTY_EXPERIMENTAL=true npx @11ty/eleventy
.*.svelte
single file components.data
function (module context) feeds into the data cascade.dataFn
which defines what will be provided as props at runtime. (see example)const eleventySvelte = require('eleventy-plugin-svelte')
module.exports = function (eleventyConfig) {
// Use Defaults
eleventyConfig.addPlugin(eleventySvelte)
}
const eleventySvelte = require('eleventy-plugin-svelte')
module.exports = function (eleventyConfig) {
// Use Defaults
eleventyConfig.addPlugin(eleventySvelte, {
// Directory to emit client side JS code
assetDir: 'assets',
// If false client side bundle is not generated
outputClient: true,
// Options for the rollup-plugin-svelte for prerendering
rollupPluginSvelteSSROptions: {},
// Options for the rollup-plugin-svelte for the client side code
rollupPluginSvelteClientOptions: {},
// Additional rollup plugins for prerendering
rollupSSRPlugins: [],
// Additional rollup plugins for the client side code
rollupClientPlugins: [],
})
}
const eleventySvelte = require('eleventy-plugin-svelte')
const postcss = require('rollup-plugin-postcss')
const terser = require('rollup-plugin-terser').terser
const dev = process.env.NODE_ENV === 'development'
// Example with prerendering the styles and omitting them in the client bundle.
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventySvelte, {
rollupSSRPlugins: [postcss()],
rollupPluginSvelteClientOptions: {
emitCss: false,
compilerOptions: {
css: false
}
},
rollupClientPlugins: [!dev && terser()],
})
}
<!DOCTYPE html>
<html lang="en">
<head>
...
<!-- Adds content from svelte:head -->
{{ content.head | safe }}
<!-- Adds prerendered css -->
<style>
{{ content.css | safe }}
</style>
</head>
<body>
....
<!-- Adds prerendered html -->
{{ content | safe }}
</body>
<script>
// Provides the data used on the client side (dataFn is a function defining the used data)
{{ dataFn | svelteData | safe }}
</script>
<!-- Gets the svelte client side code for browsers which support es modules ("app" is the id of the HTMLElement the app is going to mount on) -->
{% svelteClient 'app' %}
<!-- The legacy bundle needs systemjs to be loaded -->
<script nomodule src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/6.3.2/s.min.js"></script>
<!-- Gets the svelte client side code for browsers do not support es modules ("app" is the id of the HTMLElement the app is going to mount on) -->
{% svelteClientLegacy 'app' %}
</html>