summaryrefslogtreecommitdiff
path: root/web/source/lib/split-css.js
diff options
context:
space:
mode:
authorLibravatar f0x52 <f0x@cthu.lu>2022-10-03 16:46:38 +0200
committerLibravatar GitHub <noreply@github.com>2022-10-03 16:46:38 +0200
commit5249294a166c901469eeac1d3297e913b4a125e7 (patch)
treed68ff7bebbc0135d23c18520417c668155aa56f2 /web/source/lib/split-css.js
parent[performance] add user cache and database (#879) (diff)
downloadgotosocial-5249294a166c901469eeac1d3297e913b4a125e7.tar.xz
[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
Diffstat (limited to 'web/source/lib/split-css.js')
-rw-r--r--web/source/lib/split-css.js94
1 files changed, 50 insertions, 44 deletions
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();
+ }
+ });
+ };
};