mirror of
https://github.com/DustinBrett/daedalOS.git
synced 2026-01-15 12:15:02 +00:00
Embed workers into index
This commit is contained in:
@@ -14,6 +14,30 @@ const JS_MINIFIER_CONFIG = {
|
||||
sourceMap: false,
|
||||
};
|
||||
|
||||
const workerRegEx =
|
||||
/new Worker\(\w+\.\w+\(new URL\(\w+\.\w+\+\w+\.\w+\(\d+\),\w+\.\w+\)\),\{name:"(\w+)"\}\)/;
|
||||
|
||||
const inlineIndexWorkers = (code) => {
|
||||
const [, workerName] = code.match(workerRegEx) || [];
|
||||
|
||||
if (workerName) {
|
||||
const workerFilename = readdirSync(
|
||||
join(OUT_PATH, "_next/static/chunks")
|
||||
).find((entry) => entry.startsWith(`${workerName}.`));
|
||||
const workerSource = readFileSync(
|
||||
join(OUT_PATH, "_next/static/chunks", workerFilename)
|
||||
);
|
||||
const base64Worker = Buffer.from(workerSource).toString("base64");
|
||||
|
||||
return code.replace(
|
||||
workerRegEx,
|
||||
`new Worker("data:application/javascript;base64,${base64Worker}",{name:"${workerName}"})`
|
||||
);
|
||||
}
|
||||
|
||||
return code;
|
||||
};
|
||||
|
||||
const minifyJsFiles = (path) =>
|
||||
Promise.all(
|
||||
readdirSync(path).map(async (entry) => {
|
||||
@@ -24,12 +48,22 @@ const minifyJsFiles = (path) =>
|
||||
minifyJsFiles(fullPath);
|
||||
} else if (extname(entry).toLowerCase() === ".js") {
|
||||
const js = readFileSync(fullPath);
|
||||
const { code: minifiedJs, error } = await minify(
|
||||
let { code: minifiedJs, error } = await minify(
|
||||
js.toString(),
|
||||
JS_MINIFIER_CONFIG
|
||||
);
|
||||
|
||||
if (!error && minifiedJs?.length > 0) {
|
||||
if (entry.startsWith("index-")) {
|
||||
const changedCode = inlineIndexWorkers(minifiedJs);
|
||||
|
||||
if (minifiedJs === changedCode) {
|
||||
throw new Error("Inlining worker failed!");
|
||||
}
|
||||
|
||||
minifiedJs = changedCode;
|
||||
}
|
||||
|
||||
writeFileSync(fullPath, minifiedJs);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user