UNPKG

@soleil-se/build-app

Version:

Script for building WebApps, RESTApps and Widgets with Svelte in Sitevision.

73 lines (61 loc) 2.26 kB
import fse from 'fs-extra'; const indexContent = `(function() { 'use strict'; var router = require('router'); router.get('/', function() {}); }());`; /** * Remove Svelte 5 hydration markers (<!---->, <!--]-->, <!--[-->) from the source file when they * aren't needed, for example in SSR. * @param {string} src - The path to the source file. * @returns {Promise<void>} */ async function removeHydrationMarkers(src) { if(fse.existsSync(src)) { const content = await fse.readFile(src); await fse.writeFile(src, content.toString().replace(/(\\x3c!----\\x3e|\\x3c!--\[--\\x3e|\\x3c!--]--\\x3e)/gm, '')); } } async function createMain(dest) { const src = `${dest}/src/main.js`; if (!fse.existsSync(src)) { await removeHydrationMarkers(`${dest}/src/index.js`); await fse.writeFile(src, ''); } } async function createIndex(dest) { const src = `${dest}/src/index.js`; if (!fse.existsSync(src)) { await fse.writeFile(src, indexContent); } } async function hasSameContent(path1, path2) { if (!fse.existsSync(path1) || !fse.existsSync(path2)) return false; const [content1, content2] = await Promise.all([fse.readFile(path1), fse.readFile(path2)]); return Buffer.compare(content1, content2) === 0; } async function moveFileIfExists(src, dest) { if (!fse.existsSync(src)) return Promise.resolve(); return fse.move(src, dest, { overwrite: true }); } async function manageCss({ type, dest }) { const serverOutput = `${dest}/src/index.css`; const clientOutput = `${dest}/src/main.css`; const hooksOutput = `${dest}/src/hooks.css`; const cssDest = ['webapp', 'widget'].includes(type) ? `${dest}/src/css` : `${dest}/src/resource/client`; if (await hasSameContent(serverOutput, clientOutput)) { await fse.remove(serverOutput); } return Promise.all([ moveFileIfExists(serverOutput, `${cssDest}/index.css`), moveFileIfExists(clientOutput, `${cssDest}/main.css`), moveFileIfExists(hooksOutput, `${cssDest}/hooks.css`), ]); } export default function files({ manifest, dest }) { const type = manifest.type.toLowerCase(); if (type === 'restapp') return manageCss({ type, dest }); return () => Promise.all( [createMain(dest), createIndex(dest), manageCss({ type, dest })], ); }