Skip to content

Commit

Permalink
add a ugly workaround for relative base (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigh authored Jul 24, 2024
2 parents d0b6e59 + f249ce9 commit 0bbec83
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions replaceAssets.js
Original file line number Diff line number Diff line change
@@ -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 + "'");
})
});
});
4 changes: 4 additions & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const config = {
precompress: false,
strict: true,
}),
paths: {
assets: "http://REPLACEME",
relative: true
}
},
preprocess: vitePreprocess()
};
Expand Down

0 comments on commit 0bbec83

Please sign in to comment.