From 5249294a166c901469eeac1d3297e913b4a125e7 Mon Sep 17 00:00:00 2001 From: f0x52 Date: Mon, 3 Oct 2022 16:46:38 +0200 Subject: [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 --- web/source/lib/split-css.js | 94 ++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 44 deletions(-) (limited to 'web/source/lib/split-css.js') diff --git a/web/source/lib/split-css.js b/web/source/lib/split-css.js index da5602e1c..732223bb7 100644 --- a/web/source/lib/split-css.js +++ b/web/source/lib/split-css.js @@ -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(); + } + }); + }; }; -- cgit v1.2.3