You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/rollup.config.js

134 lines
3.8 KiB

import fs from 'fs';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import sucrase from '@rollup/plugin-sucrase';
import typescript from '@rollup/plugin-typescript';
import pkg from './package.json';
const is_publish = !!process.env.PUBLISH;
const external = (id) => id.startsWith('svelte/');
const ts_plugin = is_publish
? typescript({ include: 'src/**', typescript: require('typescript') })
: sucrase({ transforms: ['typescript'] });
// documented in src/ambient.ts
const globals = (name) =>
replace({
'var __DEV__: boolean': 'var rollup_removes_this',
'__DEV__': name === 'dev',
'var __TEST__: boolean': 'var rollup_removes_this',
'__TEST__': name === 'test',
'var __VERSION__: string': 'var rollup_removes_this',
'__VERSION__': `"${pkg.version}"`,
});
/**
*
*/
function map_outputs({ output, plugins, ...rest }) {
return Object.keys(output).flatMap((name) =>
['esm', 'cjs'].map((format) => ({
external,
...rest,
output: {
format,
file: `${name !== 'default' ? name + '/' : ''}${output[name].file}.${format === 'esm' ? 'mjs' : 'js'}`,
paths: (id) => external(id) && `${id.replace('svelte', output[name].path)}`,
},
plugins: [globals(name), ...(plugins || [])],
}))
);
}
function writeFileSync(...arr) {
arr.filter(Boolean).forEach(({ dir, content }) => fs.writeFileSync(dir, content));
}
fs.writeFileSync(`./compiler.d.ts`, `export { compile, parse, preprocess, version } from './types/compiler/index';`);
export default [
/* main exports */
...map_outputs({
input: `src/runtime/index.ts`,
output: {
/**
* Production main runtime
* -> 'svelte/index.js'
*/
default: { file: 'index', path: '.' },
/**
* Development main runtime
* -> 'svelte/dev/index.js'
* redirected to by the compiler
*/
dev: { file: 'index', path: '.' },
},
plugins: [ts_plugin],
}),
/* 'svelte/[library]' exports */
...fs
.readdirSync('src/runtime')
.filter((dir) => fs.statSync(`src/runtime/${dir}`).isDirectory())
.flatMap((library) =>
map_outputs({
input: `src/runtime/${library}/index.ts`,
output: {
/**
* Production runtime
* -> 'svelte/[library]/index.js'
*/
[library]: { file: 'index', path: '..' },
/**
* Development runtime
* -> 'svelte/dev/[library].js'
* Must be redirected to by user's bundler
*/
dev: { file: library, path: '.' },
},
plugins: [
ts_plugin,
{
writeBundle(bundle) {
writeFileSync(
/* gives internal function names to the compiler */
library === 'internal' &&
bundle['index.mjs'] && {
dir: `src/compiler/compile/internal_exports.ts`,
content: `
// This file is automatically generated
export default new Set(${JSON.stringify(bundle['index.mjs'].exports)});`,
},
/* adds package.json to every svelte/[library] */
{
dir: `${library}/package.json`,
content: `{
"main": "./index",
"module": "./index.mjs",
"types": "./index.d.ts"
}`,
},
/* exports types */
{
dir: `${library}/index.d.ts`,
content: `export * from '../types/runtime/${library}/index';`,
}
);
},
},
],
})
),
/* compiler.js */
{
input: 'src/compiler/index.ts',
plugins: [globals('compiler'), resolve(), commonjs({ include: ['node_modules/**'] }), json(), ts_plugin],
output: {
file: 'compiler.js',
format: is_publish ? 'umd' : 'cjs',
name: 'svelte',
sourcemap: true,
},
external: is_publish ? [] : (id) => id === 'acorn' || id === 'magic-string' || id.startsWith('css-tree'),
},
];