diff --git a/package.json b/package.json index 6e7d162..69025af 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "vite dev", - "build": "vite build", + "build": "vite build & node ./replaceAssets.js", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch", diff --git a/replaceAssets.js b/replaceAssets.js new file mode 100644 index 0000000..e2b205a --- /dev/null +++ b/replaceAssets.js @@ -0,0 +1,24 @@ +import fs from "fs"; +import path from 'path'; + +const readDirRecursive = async (filePath) => { + const dir = await fs.promises.readdir(filePath); + const files = await Promise.all(dir.map(async relativePath => { + const absolutePath = path.join(filePath, relativePath); + const stat = await fs.promises.lstat(absolutePath); + return stat.isDirectory() ? readDirRecursive(absolutePath) : absolutePath; + })); + return files.flat(); +} + +const files = await readDirRecursive('./build'); +Array.from(files).forEach((file) => { + if (!(file.endsWith('.js') || file.endsWith('.html') || file.endsWith('.map') || file.endsWith('.css'))) { + return; + } + fs.readFile(file,'utf8',(err,data) => { + fs.writeFile(file,data.replace(/http:\/\/REPLACEME/g,'.'),'utf8',() => { + console.log("Wrote file '" + file + "'"); + }) + }); +}); diff --git a/svelte.config.js b/svelte.config.js index d9241c1..c173174 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -15,6 +15,10 @@ const config = { precompress: false, strict: true, }), + paths: { + assets: "http://REPLACEME", + relative: true + } }, preprocess: vitePreprocess() };