build.js now rename attribut as flask variable

This commit is contained in:
Jordan Blasenhauer 2024-06-04 16:06:20 +02:00
parent e199cae258
commit 8bec560b53
2 changed files with 55 additions and 9 deletions

View file

@ -19,7 +19,7 @@ function runCommand(dir, command) {
isErr = true;
console.log(`exec error: ${err}`);
}
},
}
);
} catch (err) {
isErr = true;
@ -72,19 +72,62 @@ function updateClientDir() {
// Copy file to move it from /template/page to /template
fs.copyFileSync(
`${currPath}/${subdir}.html`,
resolve(`./templates/${subdir}.html`),
resolve(`./templates/${subdir}.html`)
);
// Delete useless dir
fs.rmSync(currPath, { recursive: true, force: true });
fs.rmSync(`./${clientBuildDir}/templates/`, { recursive: true, force: true });
});
});
// Delete useless dir
fs.rmSync(currPath, { recursive: true, force: true });
fs.rmSync(`./${clientBuildDir}/templates/`, {
recursive: true,
force: true,
});
} catch (err) {
isErr = true;
}
return isErr;
}
function setFlaskData() {
// Run all files in /templates and get data
fs.readdir(resolve("./templates"), (err, files) => {
// Read content
files.forEach((file) => {
let updateData = "";
const data = fs.readFileSync(resolve(`./templates/${file}`), {
encoding: "utf8",
flag: "r",
});
try {
// match every attribute starting with data- and ending with a ' or a "
const matches = data.match(/data-[^"']+["']/g);
// remove content between <body> and </body>
updateData = data.replace(/<body>[\s\S]*<\/body>/g, "");
// get the <body> index to insert the new content
const bodyIndex = data.indexOf("<body>");
let attributs = "";
matches.forEach((match) => {
const matchFormat = match.replace('="', "").replace("='", "");
attributs += `<div class="hidden" ${matchFormat}={{${matchFormat}}}></div>\n`;
});
// insert the new content
updateData =
data.slice(0, bodyIndex) +
`\n<body>\n` +
attributs +
`<div id="app"></div>\n</body>\n</html>`;
} catch (e) {
console.log(e);
updateData = "";
}
// Write the new content to the file
if (updateData)
fs.writeFileSync(resolve(`./templates/${file}`), updateData, "utf8");
});
});
}
// SETUP : rename and move to /static as html file
function setSetup() {
let isErr = false;
@ -100,14 +143,14 @@ function setSetup() {
return isErr;
}
// Build client and setup
const buildClientErr = buildVite("/client");
if (buildClientErr)
return console.log("Error while building client. Impossible to continue.");
console.log("Error while building client. Impossible to continue.");
// Change client dir structure
const isUpdateDirErr = updateClientDir();
if (isUpdateDirErr)
return console.log(
"Error while changing client dir structure. Impossible to continue.",
console.log(
"Error while changing client dir structure. Impossible to continue."
);
const setFlskData = setFlaskData();

View file

@ -23,11 +23,14 @@ export default defineConfig({
},
},
build: {
chunkSizeWarningLimit: 1024,
outDir: "../static",
emptyOutDir: "../static",
rollupOptions: {
input: {
test: resolve(__dirname, "./src/pages/test/index.html"),
home: resolve(__dirname, "./src/pages/home/index.html"),
instances: resolve(__dirname, "./src/pages/instances/index.html"),
},
},
},