A friendly Jamstack-focused build tool for Svelte apps
I created this utility to scaffold build configurations for my Svelte projects. Because I focus on modern static sites and client-side apps that can be deployed with Netlify, Sapper includes a lot of infrastructure for server-side development that I don't need. Svelte Kit is around the corner as a next-gen replacement for Sapper, but it may not be production-ready in the immediate future, so I will be maintaining this CLI tool until I have a solid replacement.
Here are some of the features that svelte-render
adds on top of a starter Rollup configuration:
require()
again!npm i @metamodern/svelte-render
The CLI script is released as an ES module only. Minimum Node.js version is 14 (latest LTS as of release date).
npx svelte-render [context] [--key=value]
# default to process.cwd() as context
cd project
npx svelte-render [--key=value]
# skip production optimizations
npx svelte-render --development
# just output HTML from the entry file
npx svelte-render --client=0 --noCss
# specify a custom directory structure
npx svelte-render --src=. --dist=public
# specify the path to your config file
# ** if not at ./render.config.js **
npx svelte-render --configFile=./config/svelte-render.js
Options may be specified using a configuration file. The file should use ES module import/export syntax. Its default export should be a function that takes an object containing command-line options as its arugment and returns an object specifying additional options to pass to the rendering function.
The config file is expected to be found at ./render.config.js
(relative to context
), but a custom path can be specified from the command line as shown above.
See below for a list of all options that may be passed to the rendering function.
The JavaScript API is released as an ES module only. CommonJS require()
is not supported.
The module's default export is a function with the following parameters:
async function(context: string, {
src = 'src',
assets = 'assets',
dist = 'dist',
entry = 'index.svelte',
client = 'client.js',
noCss = false,
development = false,
transpile = !development,
rollupInputPlugins = [],
rollupInputOptions = {},
compilerOptions = {},
sveltePreprocess = {},
svelteOptions = {},
terserOptions = {},
browsers = 'defaults',
babelPresets = [['@babel/preset-env', {
targets: browsers,
corejs: 3,
useBuiltIns: 'usage',
}]],
babelPlugins = [],
babelOptions = {},
before,
onRender,
after,
} = {}): Promise<number> // returns 0 on success
Note: The context
argument is only required when using the JavaScript API. When using the CLI script, context
defaults to process.cwd()
.
The following options may be specified as file names or paths and will be resolved relative to context
.
entry
and client
source filesdist
development
flag is on)client
to false
and don't use the development
flag)<style>
blocksdevelopment
flag is passed)rollup-plugin-svelte
(under the compilerOptions
key)rollup-plugin-svelte
(under the preprocess
key)rollup-plugin-svelte
rollup-plugin-terser
Note: These options are ignored when transpile
is set to false
Render hooks are functions to execute in tandem with the main rendering function. Each function will be passed the resolved context
and the full options object. Async functions are supported.
© 2020 Daniel C. Narey
ISC License