Skip to content

Instantly share code, notes, and snippets.

@nanodeath
Last active March 12, 2022 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanodeath/2b2c233501570d68b644c1f3e21ab7ce to your computer and use it in GitHub Desktop.
Save nanodeath/2b2c233501570d68b644c1f3e21ab7ce to your computer and use it in GitHub Desktop.
Svelte rollup.config.js with multiple bundles

The adjacent file is identical to the one provided by the Svelte 3.x library (3.46.x-ish) with the following distinctions:

  1. I updated the config with a little help from the docs to create a separate bundle for each .ts file in the src/ directory.
  2. I ran the provided script that adds TypeScript support.
  3. I updated the output.file property to point to the build directory in the parent (this is nested in a bigger Gradle project).

Note that you'll still need to create a separate .ts file (analogous to the initial main.ts) for each bundle.

Hope someone finds this useful.

import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';
import { readdirSync } from 'fs';
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
process.on('SIGTERM', toExit);
process.on('exit', toExit);
}
};
}
const entryPoints = readdirSync('src')
.filter(f => f.endsWith('.ts'))
.filter(f => !f.endsWith('.d.ts'))
.map(f => f.substring(0, f.indexOf('.ts')));
export default entryPoints.map(name => {
return {
input: `src/${name}.ts`,
output: {
sourcemap: !production,
format: 'iife',
name: 'app',
file: `../build/resources/main/public/${name}.js`
},
plugins: [
svelte({
preprocess: sveltePreprocess({ sourceMap: !production }),
compilerOptions: {
// enable run-time checks when not in production
dev: !production
}
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: `${name}.css` }),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
typescript({
sourceMap: !production,
inlineSources: !production
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment