[chore] Bundler restructure (#880)

* re-structure bundler, settings panel files

* add more info logging

* tidy up CSS syntax errors

* split into lib/ files

* livereloading server

* fix factor function for production builds

* remove testing console.log

* default to production env, saves 300kb bundle size
This commit is contained in:
f0x52 2022-10-03 16:46:38 +02:00 committed by GitHub
commit 5249294a16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 503 additions and 434 deletions

View file

@ -22,55 +22,61 @@ const fs = require("fs");
const path = require("path");
const {Writable} = require("stream");
const {out} = require("../index.js");
const out = require("./output-path");
const fromRegex = /\/\* from (.+?) \*\//;
module.exports = function splitCSS() {
let chunks = [];
return new Writable({
write: function(chunk, encoding, next) {
chunks.push(chunk);
next();
},
final: function() {
let stream = chunks.join("");
let input;
let content = [];
module.exports = function splitCSS(outputEmitter) {
return function() {
let chunks = [];
return new Writable({
write: function(chunk, encoding, next) {
chunks.push(chunk);
next();
},
function write() {
if (content.length != 0) {
if (input == undefined) {
throw new Error("Got CSS content without filename, can't output: ", content);
} else {
console.log("writing to", out(input));
fs.writeFileSync(out(input), content.join("\n"));
}
content = [];
}
}
const cssDir = path.join(__dirname, "../css");
stream.split("\n").forEach((line) => {
if (line.startsWith("/* from")) {
let found = fromRegex.exec(line);
if (found != null) {
write();
let parts = path.parse(found[1]);
if (path.relative(cssDir, path.join(process.cwd(), parts.dir)) == "") {
input = parts.base;
final: function() {
let stream = chunks.join("");
let input;
let content = [];
function write() {
if (content.length != 0) {
if (input == undefined) {
if (content[0].length != 0) {
throw new Error("Got CSS content without filename, can't output: ", content);
}
} else {
// prefix filename with path
let relative = path.relative(path.join(__dirname, "../"), path.join(process.cwd(), found[1]));
input = relative.replace(/\//g, "-");
outputEmitter.emit("update", {type: "CSS", updates: [input]});
fs.writeFileSync(out(input), content.join("\n"));
}
content = [];
}
} else {
content.push(line);
}
});
write();
}
});
const cssDir = path.join(__dirname, "../css");
stream.split("\n").forEach((line) => {
if (line.startsWith("/* from")) {
let found = fromRegex.exec(line);
if (found != null) {
write();
let parts = path.parse(found[1]);
if (path.relative(cssDir, path.join(process.cwd(), parts.dir)) == "") {
input = parts.base;
} else {
// prefix filename with path
let relative = path.relative(path.join(__dirname, "../"), path.join(process.cwd(), found[1]));
input = relative.replace(/\//g, "-");
}
}
} else {
content.push(line);
}
});
write();
}
});
};
};