Skip to content

Commit

Permalink
🐴 kastro: add native web worker support
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Dec 28, 2024
1 parent 5ae336a commit 906c5dc
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 35 deletions.
12 changes: 6 additions & 6 deletions kastro/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,18 @@ const bundleTarget = (targetName, props) => props.BuildMode == BuildMode.Dev
/** @const {string} */
const targetFile = targetName.slice(1);
/** @const {string} */
const assetName = "build/crate/" + (props.assetName || `${hash.toStr(contentHash)}.${getExt(targetName)}`);
const bundleName = "build/crate/" + (props.bundleName || `${hash.toStr(contentHash)}.${getExt(targetName)}`);
/** @const {!Promise<void>} */
const bundle = mkdir("build/crate", { recursive: true }).then(() =>
CompressedMimes[getExt(targetName)]
? access(assetName).catch(() => cp(targetFile, assetName))
? access(bundleName).catch(() => cp(targetFile, bundleName))
: Promise.all([
access(assetName).catch(() => cp(targetFile, assetName)),
access(`${assetName}.br`).catch(() => brotli(targetFile, assetName)),
access(`${assetName}.gz`).catch(() => zopfli(targetFile, assetName))
access(bundleName).catch(() => cp(targetFile, bundleName)),
access(`${bundleName}.br`).catch(() => brotli(targetFile, bundleName)),
access(`${bundleName}.gz`).catch(() => zopfli(targetFile, bundleName))
])
);
return bundle.then(() => assetName.slice(12));
return bundle.then(() => bundleName.slice(12));
});

export default {
Expand Down
9 changes: 1 addition & 8 deletions kastro/compiler/loader/scriptLoader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { getGlobals } from "@kimlikdao/lib/kastro/compiler/pageGlobals";
import { Script } from "@kimlikdao/lib/kastro/script";

export default (props) => {
const globals = getGlobals();
for (const key in props) {
if (key.charCodeAt(0) < 91) globals[key] = props[key];
}
return Script({ ...props, src: "SOURCE" });
}
export default (props) => Script({ ...props, src: "SOURCE" });
11 changes: 6 additions & 5 deletions kastro/compiler/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { compile } from "../../kdjs/compile";
import { filterGlobalProps } from "../props";

/** @const {TargetFunction} */
const scriptTarget = (_, props) =>
compile({
const scriptTarget = (_, props) => {
const params = {
entry: props.src,
globals: filterGlobalProps(props),
},
props.checkFreshFn
);
};
if (props.strict) params.strict = true;
return compile(params, props.checkFreshFn);
}

export { scriptTarget };
10 changes: 1 addition & 9 deletions kastro/kastro.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ const setupKastro = () => {
contents: scriptLoader.replace("SOURCE", args.path.slice(cwdLen)),
loader: "js"
}));
build.onResolve({ filter: /./, namespace: "worker" }, ({ path, importer }) => ({
path: path.startsWith(".") ? "/" + combine(getDir(importer.replace("worker:", "")), path) : path,
namespace: "worker"
}));
build.onLoad({ filter: /.*/, namespace: "worker" }, (args) => ({
contents: workerLoader.replace("SOURCE", args.path.slice(cwdLen)),
loader: "js"
}));
},
});

Expand All @@ -94,7 +86,7 @@ const cratePageProps = (crate, buildMode) => {
Lang: lang,
CodebaseLang: crate.CodebaseLang,
Route: { ...page }, // Make a copy
assetName: page[lang],
bundleName: page[lang],
targetName: `/build/${name || page[crate.CodebaseLang]}/page-${lang}.html`,
};
delete pageProps.Route[lang];
Expand Down
24 changes: 18 additions & 6 deletions kastro/script.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { tagYaz } from "../util/html";
import compiler from "./compiler/compiler";
import { getGlobals } from "./compiler/pageGlobals";

/**
* @param {Object} props
* @return {!Promise<string>}
*/
const Script = (props) =>
compiler.bundleTarget(`/build/${props.src.slice(0, -3)}-${props.Lang}.js`, {
dynamicDeps: true,
childTargets: ["/" + props.src], // Used in BuildMOde.Dev only
...props
}).then((bundledName) => tagYaz("script", { type: "module", src: bundledName }, false) + "</script>");
const Script = (props) => {
const globals = getGlobals();
for (const key in props)
if (key.charCodeAt(0) < 91) globals[key] = props[key];

return Promise.all([].concat(props.children ?? [])).then(() =>
compiler.bundleTarget(`/build/${props.src.slice(0, -3)}-${props.Lang}.js`, {
dynamicDeps: true,
childTargets: ["/" + props.src], // Used in BuildMOde.Dev only
...props,
...globals
}).then((bundleName) => {
if (props.bundleKey) globals[props.bundleKey] = bundleName;
return tagYaz("script", { type: "module", src: bundleName }, false) + "</script>"
})
);
}

export { Script };
2 changes: 1 addition & 1 deletion kdjs/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const processJs = (isEntry, file, content, files, globals, unlinkedImports) => {
const t = PACKAGE_EXTERNS + sourceName.replaceAll(":", "/") + ".d.js";
if (existsSync(t)) {
nextFile = t;
addBack = true;
addBack = !file.endsWith(".d.js");
break;
}
if (!nextFile.startsWith("node_modules"))
Expand Down

0 comments on commit 906c5dc

Please sign in to comment.