update build script for debug boilerplate and possibility for appcache
This commit is contained in:
parent
610e83f2dd
commit
e372914e7e
3 changed files with 46 additions and 4 deletions
|
@ -2,6 +2,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no">
|
||||
<meta name="application-name" content="Brawl Chat"/>
|
||||
<link rel="stylesheet" type="text/css" href="src/ui/web/css/main.css">
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "brawl-chat",
|
||||
"version": "1.0.0",
|
||||
"version": "0.0.1",
|
||||
"description": "A javascript matrix client prototype, trying to minize RAM usage by offloading as much as possible to IndexedDB",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
|
|
|
@ -14,7 +14,34 @@ const __dirname = dirname(__filename);
|
|||
const projectDir = path.join(__dirname, "../");
|
||||
const targetDir = path.join(projectDir, "target");
|
||||
|
||||
const jsPostfix = `
|
||||
window.DEBUG = true;
|
||||
let buf = "";
|
||||
console.error = (...params) => {
|
||||
const lastLines = "...\\n" + buf.split("\\n").slice(-10).join("\\n");
|
||||
// buf = buf + "ERR " + params.join(" ") + "\\n";
|
||||
// const location = new Error().stack.split("\\n")[2];
|
||||
alert(params.join(" ") +"\\n...\\n" + lastLines);
|
||||
};
|
||||
console.log = console.info = console.warn = (...params) => {
|
||||
buf = buf + params.join(" ") + "\\n";
|
||||
};
|
||||
`;
|
||||
|
||||
const jsSuffix = `
|
||||
setTimeout(() => {
|
||||
const showlogs = document.getElementById("showlogs");
|
||||
showlogs.addEventListener("click", () => {
|
||||
const lastLines = "...\\n" + buf.split("\\n").slice(-20).join("\\n");
|
||||
alert(lastLines);
|
||||
}, true);
|
||||
showlogs.innerText = "Show last 20 log lines";
|
||||
}, 10000);
|
||||
`;
|
||||
|
||||
async function build() {
|
||||
// get version number
|
||||
const version = JSON.parse(await fs.readFile(path.join(projectDir, "package.json"), "utf8")).version;
|
||||
// clear target dir
|
||||
await removeDirIfExists(targetDir);
|
||||
await fs.mkdir(targetDir);
|
||||
|
@ -22,9 +49,10 @@ async function build() {
|
|||
const devHtml = await fs.readFile(path.join(projectDir, "index.html"), "utf8");
|
||||
const doc = cheerio.load(devHtml);
|
||||
doc("link[rel=stylesheet]").attr("href", "brawl.css");
|
||||
// doc("html").attr("manifest", "manifest.appcache");
|
||||
doc("script").replaceWith(
|
||||
`<script type="text/javascript" src="brawl.js"></script>` +
|
||||
`<script type="text/javascript">main(document.body);</script>`);
|
||||
`<script type="text/javascript">${jsPostfix} main(document.body); ${jsSuffix}</script>`);
|
||||
await fs.writeFile(path.join(targetDir, "index.html"), doc.html(), "utf8");
|
||||
// create js bundle
|
||||
const rollupConfig = {
|
||||
|
@ -43,6 +71,17 @@ async function build() {
|
|||
const cssBundler = postcss([postcssImport]);
|
||||
const postCss = await cssBundler.process(preCss, {from: cssMainFile});
|
||||
await fs.writeFile(path.join(targetDir, "brawl.css"), postCss, "utf8");
|
||||
// write appcache manifest
|
||||
const manifestLines = [
|
||||
`CACHE MANIFEST`,
|
||||
`# v${version}`,
|
||||
"brawl.js",
|
||||
"brawl.css",
|
||||
"index.html",
|
||||
""
|
||||
];
|
||||
await fs.writeFile(path.join(targetDir, "manifest.appcache"), manifestLines.join("\n"), "utf8");
|
||||
console.log(`built brawl ${version} successfully`);
|
||||
}
|
||||
|
||||
async function removeDirIfExists(targetDir) {
|
||||
|
@ -51,8 +90,9 @@ async function removeDirIfExists(targetDir) {
|
|||
await Promise.all(files.map(filename => fs.unlink(path.join(targetDir, filename))));
|
||||
await fs.rmdir(targetDir);
|
||||
} catch (err) {
|
||||
// err.code === ENOENT
|
||||
console.log(err);
|
||||
if (err.code !== "ENOENT") {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue