@soleil-se/build-app
Version:
Script for building WebApps, RESTApps and Widgets with Svelte in Sitevision.
66 lines (57 loc) • 1.95 kB
JavaScript
import { rollup } from 'rollup';
import json from '@rollup/plugin-json';
import fse from 'fs-extra';
import onwarn from './utils/onwarn.js';
import { getReplaceServerPlugin } from './api/getReplacePlugin.js';
import { getAliasServerPlugin } from './api/getAliasPlugin.js';
import { getBabelServerPlugin } from './api/getBabelPlugin.js';
import getCommonjsPlugin from './api/getCommonjsPlugin.js';
import getNodeResolvePlugin from './api/getNodeResolvePlugin.js';
import getSveltePlugin from './api/getSveltePlugin.js';
import getPostcssPlugin from './api/getPostcssPlugin.js';
import getTerserPlugin from './api/getTerserPlugin.js';
import getStringPlugin from './api/getStringPlugin.js';
import sitevision from './plugins/sitevision.js';
export default function rollupServer({
debug,
input,
output,
extractCss = false,
cache = true,
} = {}) {
let bundleCache;
let plugins;
return async () => {
if (!fse.existsSync(input)) return Promise.resolve();
plugins = plugins || [
getReplaceServerPlugin(),
getAliasServerPlugin(),
sitevision({ context: 'server' }),
json(),
getStringPlugin(),
getNodeResolvePlugin({ input, debug }),
getCommonjsPlugin(),
await getSveltePlugin({ input, extractCss, ssr: true }),
getPostcssPlugin({ extractCss, debug }),
getBabelServerPlugin(),
getTerserPlugin({ debug }),
];
try {
const bundle = await rollup({
input,
plugins,
onwarn,
cache: bundleCache,
});
bundleCache = cache && bundle.cache;
return bundle.write({
file: output,
intro: 'var Promise = Promise || { resolve: () => {} }; var global = global || this; var self = self || global; var globalThis = globalThis || global; var setTimeout = setTimeout || function(cb) { cb() };',
format: 'iife',
});
} catch (e) {
bundleCache = undefined;
throw e;
}
};
}