const svelte = require("rollup-plugin-svelte"); const typescript = require("@rollup/plugin-typescript"); const { terser } = require("rollup-plugin-terser"); const html = require("@rollup/plugin-html"); const resolve = require("rollup-plugin-node-resolve"); const image = require("@rollup/plugin-image"); const sizes = require("rollup-plugin-sizes"); const visualise = require("rollup-plugin-visualizer"); const postcss = require("rollup-plugin-postcss"); const commonjs = require("@rollup/plugin-commonjs"); const livereload = require("rollup-plugin-livereload"); const { preprocess } = require("./svelte.config"); const VIEWS = ["Home", "Login", "Popup", "User"]; const dev = process.env.NODE_ENV !== "production"; const htmlTemplate = ({ attributes, meta, files, publicPath, title }) => { const makeHtmlAttributes = (attributes) => { if (!attributes) { return ""; } const keys = Object.keys(attributes); // eslint-disable-next-line no-param-reassign return keys.reduce( (result, key) => (result += ` ${key}="${attributes[key]}"`), "" ); }; const scripts = (files.js || []) .map(({ fileName }) => { const attrs = makeHtmlAttributes(attributes.script); return ``; }) .join("\n"); const links = (files.css || []) .map(({ fileName }) => { const attrs = makeHtmlAttributes(attributes.link); return ``; }) .join("\n"); const metas = meta .map((input) => { const attrs = makeHtmlAttributes(input); return ``; }) .join("\n"); return ` ${metas} ${title} ${links} ${scripts} `; }; export default VIEWS.map((view) => ({ input: `src/pages/${view}/main.ts`, output: [ dev ? { file: `build/${view}/bundle.js`, format: "iife", sourcemap: true, name: view, } : { file: `build/${view}/bundle.min.js`, format: "iife", name: view, plugins: [terser()], }, ], plugins: [ commonjs(), svelte({ emitCss: true, preprocess, }), typescript({ sourceMap: dev, inlineSources: dev }), html({ title: view, attributes: { html: { lang: "en" }, }, meta: [ { name: "viewport", content: "width=device-width", }, ], template: htmlTemplate, }), resolve({ browser: true, dedupe: ["svelte"] }), image(), sizes(), visualise({ filename: `build/stats/${view}.html`, title: `Rullup bundle for ${view}`, }), postcss({ extract: `bundle.css`, //TODO: Check if it should be enabled // inject: true, }), // dev && livereload(), ], }));