Skip to content

Instantly share code, notes, and snippets.

@akoepcke
Created January 4, 2023 17:01
Show Gist options
  • Save akoepcke/1441319bf6e283069559e39d528a8ea2 to your computer and use it in GitHub Desktop.
Save akoepcke/1441319bf6e283069559e39d528a8ea2 to your computer and use it in GitHub Desktop.
Astro relative base path
{
...
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"postbuild": "node ./postbuild.mjs -path ./",
"preview": "astro preview",
"astro": "astro"
},
...
}
import fs from "fs";
import path from "path";
import astroConfig from "./astro.config.mjs";
const PUBLIC_DIR = astroConfig.dist || "dist";
const argvs = process.argv.slice(2);
if ((argvs[0] === "--p" || argvs[0] === "-path") && argvs[1]) {
let files = [];
const extensions = [".html", ".css", ".js", ".json"];
const PRODUCTION_URL = argvs[1];
const replaceUrlsInFiles = function (dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);
files.forEach(function (file) {
const current = fs.statSync(dirPath + "/" + file);
if (current.isDirectory()) {
replaceUrlsInFiles(dirPath + "/" + file, arrayOfFiles);
} else {
const filePath = path.join(path.dirname(import.meta.url.replace(/file\:/, "")), dirPath, "/", file);
if (extensions.includes(path.extname(filePath))) {
let content = fs.readFileSync(filePath);
content = String(content).replace(/src="\//g, `src="${PRODUCTION_URL}`);
content = String(content).replace(/href="\//g, `href="${PRODUCTION_URL}`);
content = String(content).replace(/url\("\//g, `url("${PRODUCTION_URL}`);
content = String(content).replace(/src='\//g, `src='${PRODUCTION_URL}`);
content = String(content).replace(/href='\//g, `href='${PRODUCTION_URL}`);
content = String(content).replace(/url\('\//g, `url('${PRODUCTION_URL}`);
fs.writeFileSync(filePath, content);
}
}
});
};
// if (PRODUCTION_URL && process.env.NODE_ENV === "production") {
replaceUrlsInFiles(PUBLIC_DIR, files);
// } else {
// console.log("skip postbuild");
// process.exit(0);
// }
} else {
console.error('please provide a path argument "-path", "--p"');
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment