Skip to content

Commit

Permalink
remove web/desktop target imports of non-explicit deps
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Aug 10, 2024
1 parent fe3007c commit 2633f3d
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 124 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/build/hook/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,17 @@ export default components;`;

const write_bootstrap = async (build_number, app, mode) => {
const build_start_script = `
import file from "@rcompat/fs/file";
import serve from "@primate/core/serve";
import serve from "primate/serve";
import config from "./${config_filename}";
const files = {};
${app.server_build.map(name =>
`import ${name} from "./${build_number}/${name}.js";
files.${name} = ${name};`,
).join("\n")}
import components from "./${build_number}/components.js";
import * as target from "./target.js";
import target from "./target.js";
await serve(file(import.meta.url).directory, {
await serve(import.meta.url, {
...target,
config,
files,
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import config from "#config";
import config_filename from "#config-filename";
import empty_config_file from "#error/empty-config-file";
import error_in_config_file from "#error/error-in-config-file";
import log from "@primate/core/log";
import tryreturn from "@rcompat/async/tryreturn";
import empty from "@rcompat/object/empty";
import override from "@rcompat/object/override";
Expand All @@ -16,7 +15,7 @@ const empty_config = config => config === undefined || empty(config);

const get_config = async project_root => {
const local_config = project_root.join(config_filename);
const exists = await local_config.exists()
const exists = await local_config.exists();
if (exists) {
try {
const imported = await local_config.import("default");
Expand Down
64 changes: 18 additions & 46 deletions packages/core/src/build/targets/web.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collect from "@rcompat/fs/collect";
import join from "@rcompat/fs/join";
import webpath from "@rcompat/fs/webpath";

const html = /^.*.html$/u;
Expand All @@ -15,84 +16,55 @@ export default async app => {
return {
src,
path,
code: `await file(asset${i}).text()`,
code: `await load_text(asset${i})`,
type,
};
});
const d = app.runpath(location.pages);
const pages = await Promise.all((await collect(d, html, { recursive: true }))
.map(async file => `${file.debase(d)}`.slice(1)));
const pages_str = pages.map(page =>
`"${page}": await join(import.meta.url, "${webpath(`../${location.pages}/${page}`)}").text(),`).join("\n");
`"${page}": await load_text(import.meta.url,
"${webpath(`../${location.pages}/${page}`)}"),`).join("\n");

const assets_scripts = `
import file from "@rcompat/fs/file";
import join from "@rcompat/fs/join";
import stringify from "@rcompat/object/stringify";
import crypto from "@rcompat/crypto";
import { OK } from "@rcompat/http/status";
import { resolve } from "@rcompat/http/mime";
const encoder = new TextEncoder();
const hash = async (data, algorithm = "sha-384") => {
const bytes = await crypto.subtle.digest(algorithm, encoder.encode(data));
const prefix = algorithm.replace("-", _ => "");
return \`\${prefix}-\${btoa(String.fromCharCode(...new Uint8Array(bytes)))}\`;
};
import loader from "primate/loader";
import load_text from "primate/load-text";
${$imports.map(({ path }, i) =>
`const asset${i} = await join(import.meta.dirname, "${path}").text();`)
`const asset${i} = await load_text(import.meta.dirname, "${path}");`)
.join("\n ")}
const assets = [${$imports.map(($import, i) => `{
src: "${$import.src}",
code: asset${i},
type: "${$import.type}",
inline: false,
integrity: await hash(asset${i}),
}`).join(",\n ")}];
const imports = {
app: join("${http.static.root}", "${$imports.find($import =>
$import.src.includes("app") && $import.src.endsWith(".js")).src}").webpath(),
app: "${join(http.static.root, $imports.find($import =>
$import.src.includes("app") && $import.src.endsWith(".js")).src).webpath()}"
};
// importmap
assets.push({
inline: true,
code: stringify({ imports }),
code: { imports },
type: "importmap",
integrity: await hash(stringify({ imports })),
});
const pages = {
${pages_str}
};
const buildroot = file(import.meta.url).join("..");
const serve_asset = asset => new Response(asset.stream(), {
status: OK,
headers: {
"Content-Type": resolve(asset.name),
},
});
const loader = {
page(name) {
return pages[name] ?? pages["${app.get("pages.app")}"];
},
async asset(pathname) {
const root_asset = buildroot.join(\`client/\${pathname}\`);
if (await await root_asset.isFile()) {
return serve_asset(root_asset);
}
const static_asset = buildroot.join(\`client/static/\${pathname}\`);
if (await static_asset.isFile()) {
return serve_asset(static_asset);
}
},
export default {
assets,
loader: loader({
pages,
rootfile: import.meta.url,
pages_app: "${app.get("pages.app")}"
}),
target: "web",
};
const target = "web";
export { assets, loader, target };
`;
await app.path.build.join("target.js").write(assets_scripts);

Expand Down
32 changes: 23 additions & 9 deletions packages/core/src/serve/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import double_extension from "#error/double-extension";
import crypto from "@rcompat/crypto";
import file from "@rcompat/fs/file";
import join from "@rcompat/fs/join";
import { html } from "@rcompat/http/mime";
import { OK } from "@rcompat/http/status";
import is from "@rcompat/invariant/is";
import empty from "@rcompat/object/empty";
import get from "@rcompat/object/get";
import stringify from "@rcompat/object/stringify";
import valmap from "@rcompat/object/valmap";
import module_loader from "./module_loader.js";
import to_sorted from "./to_sorted.js";
Expand All @@ -21,8 +23,6 @@ const to_csp = (config_csp, assets, csp) => config_csp
.map(([key, directives]) => `${key} ${directives.join(" ")}`)
.join(";");

const encoder = new TextEncoder();

const attribute = attributes => empty(attributes)
? ""
: " ".concat(Object.entries(attributes)
Expand Down Expand Up @@ -61,8 +61,26 @@ const render_head = (assets, fonts, head) =>
tags.font({ href, type: "font/woff2" }),
).join("\n"));

export default async (root, { config, assets, files, components, loader, target, mode }) => {
const { http } = config;
const encoder = new TextEncoder();
const hash = async (data, algorithm = "sha-384") => {
const bytes = await crypto.subtle.digest(algorithm, encoder.encode(data));
const prefix = algorithm.replace("-", _ => "");
return`${prefix}-${btoa(String.fromCharCode(...new Uint8Array(bytes)))}`;
};

export default async (rootfile, build) => {
const root = file(rootfile).directory;
const { config, files, components, loader, target, mode } = build;
const assets = await Promise.all(build.assets.map(async asset => {
const code = asset.type === "importmap" ? stringify(asset.code) : asset.code;
return {
...asset,
code,
integrity: await hash(code),
};
}));

const { http } = build.config;
const secure = http?.ssl !== undefined;
const path = valmap(config.location, value => root.join(value));

Expand Down Expand Up @@ -174,11 +192,7 @@ export default async (root, { config, assets, files, components, loader, target,
this.handlers[extension] !== undefined && double_extension(extension);
this.handlers[extension] = handle;
},
async hash(data, algorithm = "sha-384") {
const bytes = await crypto.subtle.digest(algorithm, encoder.encode(data));
const prefix = algorithm.replace("-", _ => "");
return `${prefix}-${btoa(String.fromCharCode(...new Uint8Array(bytes)))}`;
},
hash,
// noop
target(name, handler) {},
build_target: target,
Expand Down
5 changes: 3 additions & 2 deletions packages/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"type": "module",
"exports": {
"./worker": "./src/worker.js",
".": "./src/index.js"
".": "./src/index.js",
"./*": "./src/public/*.js",
"./platform/*": "./src/platform/*.js"
}
}
85 changes: 27 additions & 58 deletions packages/native/src/desktop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collect from "@rcompat/fs/collect";
import join from "@rcompat/fs/join";
import webpath from "@rcompat/fs/webpath";

const html = /^.*.html$/u;
Expand All @@ -9,9 +10,9 @@ export default async app => {
const client = app.runpath(location.client);
const re = /app..*(?:js|css)$/u;

const import_statics = (await client.collect()).map((path, i) => `
const static_imports = (await client.collect()).map((path, i) => `
import static${i} from "${webpath(`./client${path.debase(client)}`)}" with { type: "file" };
statics["${webpath(path.debase(client))}"] = await file(static${i});`)
static_imports["${webpath(path.debase(client))}"] = static${i};`)
.join("\n");

const $imports = (await Promise.all((await client.collect(re, { recursive: false }))
Expand All @@ -22,7 +23,7 @@ export default async app => {
return {
src,
path,
code: `await file(asset${i}).text()`,
code: `await load_text(asset${i})`,
type,
empty: (await file.text()).length === 0,
};
Expand All @@ -33,83 +34,51 @@ export default async app => {
const app_js = $imports.find($import => $import.src.endsWith(".js"));

const assets_scripts = `
import Webview from "@rcompat/webview/worker/${app.build_target}";
import join from "@rcompat/fs/join";
import file from "@rcompat/fs/file";
import stringify from "@rcompat/object/stringify";
import crypto from "@rcompat/crypto";
import { OK } from "@rcompat/http/status";
import { resolve } from "@rcompat/http/mime";
import Webview from "@primate/native/platform/${app.build_target}";
import loader from "@primate/native/loader";
import load_text from "primate/load-text";
const encoder = new TextEncoder();
const hash = async (data, algorithm = "sha-384") => {
const bytes = await crypto.subtle.digest(algorithm, encoder.encode(data));
const prefix = algorithm.replace("-", _ => "");
return \`\${prefix}-\${btoa(String.fromCharCode(...new Uint8Array(bytes)))}\`;
};
const statics = {};
${import_statics}
const static_imports = {};
${static_imports}
${$imports.map(({ path }, i) =>
`import asset${i} from "${path}" with { type: "file" };
const file${i} = await file(asset${i}).text();`).join("\n ")}
const file${i} = await load_text(asset${i});`).join("\n ")}
const assets = [${$imports.map(($import, i) => `{
src: "${$import.src}",
code: file${i},
type: "${$import.type}",
inline: false,
integrity: await hash(file${i}),
}`).join(",\n ")}];
${app_js === undefined ? "" :
`const imports = {
app: join("${http.static.root}", "${$imports.find($import =>
$import.src.includes("app") && $import.src.endsWith(".js")).src}").webpath(),
`
const imports = {
app: "${join(http.static.root, $imports.find($import =>
$import.src.includes("app") && $import.src.endsWith(".js")).src).webpath()}"
};
// importmap
assets.push({
inline: true,
code: stringify({ imports }),
code: { imports },
type: "importmap",
integrity: await hash(stringify({ imports })),
});`}
const page_imports = {};
${pages.map((page, i) =>
`import i_page${i} from "${webpath(`./${location.pages}/${page}`)}" with { type: "file" };
const page${i} = await file(i_page${i}).text();`).join("\n ")}
`import page${i} from "${webpath(`./${location.pages}/${page}`)}" with { type: "file" };
page_imports["${page}"] = page${i};`).join("\n ")}
const pages = {
${pages.map((page, i) => `"${page}": page${i},`).join("\n ")}
export default {
assets,
loader: await loader({
page_imports,
static_imports,
pages_app: "${app.get("pages.app")}",
Webview,
}),
target: "${app.build_target}",
};
const serve_asset = asset => new Response(asset.stream(), {
status: OK,
headers: {
"Content-Type": resolve(asset.name),
},
});
const loader = {
page(name) {
return pages[name] ?? pages["${app.get("pages.app")}"];
},
asset(pathname) {
const root_asset = statics[pathname];
if (root_asset !== undefined) {
return serve_asset(root_asset);
}
const static_asset = statics[\`/static\${pathname}\`];
if (static_asset !== undefined) {
return serve_asset(static_asset);
}
},
webview() {
return Webview;
}
};
const target = "${app.build_target}";
export { assets, loader, target };
`;
await app.path.build.join("target.js").write(assets_scripts);

Expand Down
1 change: 1 addition & 0 deletions packages/native/src/platform/darwin-arm64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "@rcompat/webview/worker/darwin-arm64";
1 change: 1 addition & 0 deletions packages/native/src/platform/darwin-x64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "@rcompat/webview/worker/darwin-x64";
1 change: 1 addition & 0 deletions packages/native/src/platform/linux-x64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "@rcompat/webview/worker/linux-x64";
1 change: 1 addition & 0 deletions packages/native/src/platform/windows-x64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "@rcompat/webview/worker/windows-x64";
Loading

0 comments on commit 2633f3d

Please sign in to comment.