@flanksource/page-runtime 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -0
- package/README.md +494 -2
- package/dist/build/README.md +96 -0
- package/dist/build/cjs-export-names.mjs +24 -0
- package/dist/build/dependencies.mjs +279 -0
- package/dist/build/dependencies.mjs.map +1 -0
- package/dist/build/dependency-cache-Zg-FGasE.mjs +2130 -0
- package/dist/build/dependency-cache-Zg-FGasE.mjs.map +1 -0
- package/dist/build/index.mjs +42 -0
- package/dist/build/index.mjs.map +1 -0
- package/dist/build/package.json +19 -0
- package/dist/build/types/activity-schema.d.ts +62 -0
- package/dist/build/types/build/barrels.d.ts +70 -0
- package/dist/build/types/build/base.d.ts +2 -0
- package/dist/build/types/build/browser-runtime-exports.d.ts +19 -0
- package/dist/build/types/build/check-fragment.d.ts +5 -0
- package/dist/build/types/build/commonjs-exports.d.ts +5 -0
- package/dist/build/types/build/convert-commonjs.d.ts +12 -0
- package/dist/build/types/build/dependencies.d.ts +6 -0
- package/dist/build/types/build/dependency-barrel-index.d.ts +5 -0
- package/dist/build/types/build/dependency-barrels.d.ts +7 -0
- package/dist/build/types/build/dependency-cache.d.ts +7 -0
- package/dist/build/types/build/dependency-optional.d.ts +4 -0
- package/dist/build/types/build/dependency-plugin.d.ts +8 -0
- package/dist/build/types/build/dependency-runtime.d.ts +20 -0
- package/dist/build/types/build/dependency-workers.d.ts +8 -0
- package/dist/build/types/build/errors.d.ts +9 -0
- package/dist/build/types/build/fragment.d.ts +13 -0
- package/dist/build/types/build/index.d.ts +7 -0
- package/dist/build/types/build/library-runtime.d.ts +21 -0
- package/dist/build/types/build/module-utils.d.ts +3 -0
- package/dist/build/types/build/recipe-dependencies.d.ts +2 -0
- package/dist/build/types/build/runtime-entries.d.ts +4 -0
- package/dist/build/types/build/runtime-layer.d.ts +17 -0
- package/dist/build/types/build/trace.d.ts +140 -0
- package/dist/build/types/design-schema.d.ts +98 -0
- package/dist/build/types/host/agent-source.d.ts +2 -0
- package/dist/build/types/host/agent-validation.d.ts +3 -0
- package/dist/build/types/host/design-validation.d.ts +2 -0
- package/dist/build/types/host/message-values.d.ts +8 -0
- package/dist/build/types/host/review/validation.d.ts +5 -0
- package/dist/build/types/host/validation.d.ts +6 -0
- package/dist/build/types/link/page-sources.d.ts +31 -0
- package/dist/build/types/link/types.d.ts +62 -0
- package/dist/build/types/link/validation.d.ts +9 -0
- package/dist/build/types/markers.d.ts +10 -0
- package/dist/build/types/review-schema.d.ts +60 -0
- package/dist/build/types/schema.d.ts +319 -0
- package/dist/build/types/sw/barrels.d.ts +9 -0
- package/dist/build/types/sw/compiler-plugins.d.ts +6 -0
- package/dist/build/types/sw/composition.d.ts +8 -0
- package/dist/build/types/sw/module-transform.d.ts +68 -0
- package/dist/build/types/sw/profile.d.ts +7 -0
- package/dist/build/types/sw/source-provider.d.ts +7 -0
- package/dist/build/types/sw/versions.d.ts +10 -0
- package/dist/node/catalog.mjs +1 -0
- package/dist/node/chunks/publication-RchcQvxD.mjs +229 -0
- package/dist/node/chunks/publication-RchcQvxD.mjs.map +1 -0
- package/dist/node/chunks/schema-CaRTfg2x.mjs +12 -0
- package/dist/node/chunks/schema-CaRTfg2x.mjs.map +1 -0
- package/dist/node/dev.mjs +410 -0
- package/dist/node/dev.mjs.map +1 -0
- package/dist/node/publication/cli.mjs +13 -0
- package/dist/node/publication/cli.mjs.map +1 -0
- package/dist/node/publication.mjs +1 -0
- package/dist/node/schema.mjs +1 -0
- package/dist/node/styles.mjs +49 -0
- package/dist/node/styles.mjs.map +1 -0
- package/dist/static/boot.js +40938 -0
- package/dist/static/boot.js.map +1 -0
- package/dist/static/bootstrap.html +1 -0
- package/dist/static/devtools-hook.js +118 -0
- package/dist/static/document-bundle.js +1 -0
- package/dist/static/document.js +2621 -0
- package/dist/static/editing.js +37 -0
- package/dist/static/export-compiler.js +81521 -0
- package/dist/static/export-compiler.js.map +1 -0
- package/dist/static/page-agent.js +17193 -0
- package/dist/static/page-agent.js.map +1 -0
- package/dist/static/sw.js +86112 -0
- package/dist/static/sw.js.map +1 -0
- package/package.json +110 -3
- package/scripts/build-builder.ts +27 -0
- package/scripts/build-library-runtime.ts +9 -0
- package/scripts/build-node.ts +26 -0
- package/scripts/build-static.ts +86 -0
- package/scripts/build.ts +7 -0
- package/scripts/check-fragment.ts +12 -0
- package/scripts/local-workspaces.ts +55 -0
- package/scripts/react-externals.ts +18 -0
- package/scripts/release-version.ts +47 -0
- package/scripts/run-package-build.ts +8 -0
- package/scripts/serve-packages.ts +8 -0
- package/src/activity-schema.ts +92 -0
- package/src/build/README.md +96 -0
- package/src/build/barrels.ts +306 -0
- package/src/build/base.ts +2 -0
- package/src/build/browser-runtime-exports.ts +52 -0
- package/src/build/check-fragment.ts +45 -0
- package/src/build/cjs-export-names.mjs +24 -0
- package/src/build/commonjs-exports.ts +24 -0
- package/src/build/convert-commonjs.ts +183 -0
- package/src/build/dependencies.ts +6 -0
- package/src/build/dependency-barrel-index.ts +48 -0
- package/src/build/dependency-barrels.ts +80 -0
- package/src/build/dependency-cache.ts +40 -0
- package/src/build/dependency-optional.ts +15 -0
- package/src/build/dependency-plugin.ts +58 -0
- package/src/build/dependency-runtime.ts +182 -0
- package/src/build/dependency-workers.ts +41 -0
- package/src/build/errors.ts +9 -0
- package/src/build/fragment.ts +89 -0
- package/src/build/index.ts +7 -0
- package/src/build/library-runtime.ts +101 -0
- package/src/build/module-utils.ts +11 -0
- package/src/build/recipe-dependencies.ts +14 -0
- package/src/build/runtime-entries.ts +10 -0
- package/src/build/runtime-layer.ts +99 -0
- package/src/build/trace.ts +431 -0
- package/src/design-schema.ts +25 -0
- package/src/dev/README.md +78 -0
- package/src/dev/catalog.ts +79 -0
- package/src/dev/cli.ts +25 -0
- package/src/dev/index.ts +118 -0
- package/src/dev/runtime-origin.ts +63 -0
- package/src/editing.ts +25 -0
- package/src/form/entry-source.ts +68 -0
- package/src/form/form-page.ts +63 -0
- package/src/form/index.ts +2 -0
- package/src/form/source-update.ts +17 -0
- package/src/frame/activity.browser.ts +34 -0
- package/src/frame/activity.ts +52 -0
- package/src/frame/agent/README.md +47 -0
- package/src/frame/agent/agent.browser.ts +127 -0
- package/src/frame/agent/design.ts +62 -0
- package/src/frame/agent/frame-layout.ts +54 -0
- package/src/frame/agent/index.ts +151 -0
- package/src/frame/agent/overlay.ts +104 -0
- package/src/frame/agent/owner.ts +61 -0
- package/src/frame/agent/provider.tsx +14 -0
- package/src/frame/agent/react-override.ts +55 -0
- package/src/frame/agent/review.browser.ts +65 -0
- package/src/frame/agent/review.ts +108 -0
- package/src/frame/agent/testing/callsite.tsx +5 -0
- package/src/frame/agent/testing/client.ts +99 -0
- package/src/frame/agent/testing/library.ts +3 -0
- package/src/frame/agent/testing/modal.tsx +5 -0
- package/src/frame/agent/testing/review-client.ts +43 -0
- package/src/frame/agent/testing/review-server.ts +31 -0
- package/src/frame/agent/testing/server.ts +72 -0
- package/src/frame/agent/testing/vocabulary-host.ts +20 -0
- package/src/frame/agent/testing/vocabulary.tsx +10 -0
- package/src/frame/agent/vitest.browser.config.ts +6 -0
- package/src/frame/agent/vocabulary.browser.ts +30 -0
- package/src/frame/agent/vocabulary.ts +13 -0
- package/src/frame/agent.ts +3 -0
- package/src/frame/boot-styles.ts +50 -0
- package/src/frame/boot.ts +269 -0
- package/src/frame/bridge.ts +27 -0
- package/src/frame/devtools-hook.ts +1 -0
- package/src/frame/document-assets.ts +76 -0
- package/src/frame/document-entry.ts +68 -0
- package/src/frame/document-modules.ts +113 -0
- package/src/frame/document-mount.ts +32 -0
- package/src/frame/entry.ts +4 -0
- package/src/frame/export-compiler.ts +18 -0
- package/src/frame/export-page.ts +61 -0
- package/src/frame/export.ts +89 -0
- package/src/frame/index.ts +8 -0
- package/src/frame/local-runtime.browser.ts +36 -0
- package/src/frame/mount-page.ts +36 -0
- package/src/frame/page-agent.ts +2 -0
- package/src/frame/readiness.ts +51 -0
- package/src/frame/render-job.ts +101 -0
- package/src/frame/review-integration.browser.ts +48 -0
- package/src/frame/runtime.browser.ts +90 -0
- package/src/frame/tailwind/README.md +49 -0
- package/src/frame/tailwind/boot-styles.browser.ts +18 -0
- package/src/frame/tailwind/cache.ts +20 -0
- package/src/frame/tailwind/load.ts +75 -0
- package/src/frame/tailwind/recipe.ts +76 -0
- package/src/frame/tailwind/runtime.ts +113 -0
- package/src/frame/tailwind/store.ts +55 -0
- package/src/frame/tailwind/stylesheets.ts +42 -0
- package/src/frame/tailwind/tailwind.browser.ts +71 -0
- package/src/frame/tailwind/testing/client.ts +133 -0
- package/src/frame/tailwind/testing/compare.ts +91 -0
- package/src/frame/tailwind/testing/pages.ts +14 -0
- package/src/frame/tailwind/testing/server.ts +89 -0
- package/src/frame/tailwind/vitest.browser.config.ts +7 -0
- package/src/frame/testing/boot-layout.ts +3 -0
- package/src/frame/testing/boot-page.ts +3 -0
- package/src/frame/testing/host.ts +78 -0
- package/src/frame/testing/local-host.tsx +33 -0
- package/src/frame/testing/server.ts +135 -0
- package/src/host/BuildLog.tsx +28 -0
- package/src/host/DEBUG_CONSOLE.md +13 -0
- package/src/host/PageForm.tsx +63 -0
- package/src/host/PageFrame.tsx +49 -0
- package/src/host/RuntimeActivity.tsx +77 -0
- package/src/host/RuntimeDebugPanel.tsx +18 -0
- package/src/host/activity-state.ts +69 -0
- package/src/host/agent-source.ts +14 -0
- package/src/host/agent-validation.ts +73 -0
- package/src/host/bridge.ts +56 -0
- package/src/host/build-log.ts +29 -0
- package/src/host/clear-cache.ts +66 -0
- package/src/host/debug-runtime.tsx +17 -0
- package/src/host/design-validation.ts +96 -0
- package/src/host/index.ts +12 -0
- package/src/host/link/index.ts +66 -0
- package/src/host/link/transport.ts +68 -0
- package/src/host/link/watch.ts +62 -0
- package/src/host/message-values.ts +55 -0
- package/src/host/origin.ts +7 -0
- package/src/host/review/README.md +32 -0
- package/src/host/review/index.ts +43 -0
- package/src/host/review/validation.ts +55 -0
- package/src/host/runtime-diagnostics.ts +41 -0
- package/src/host/validation.ts +148 -0
- package/src/link/README.md +115 -0
- package/src/link/barrels.ts +5 -0
- package/src/link/build-cli.ts +12 -0
- package/src/link/build.ts +99 -0
- package/src/link/commonjs.ts +39 -0
- package/src/link/examples/server.ts +18 -0
- package/src/link/index.ts +6 -0
- package/src/link/link.browser.ts +66 -0
- package/src/link/local-relay.browser.ts +22 -0
- package/src/link/overrides.ts +69 -0
- package/src/link/page-sources.ts +69 -0
- package/src/link/source.ts +25 -0
- package/src/link/storage.ts +32 -0
- package/src/link/testing/client.ts +96 -0
- package/src/link/testing/folder.ts +19 -0
- package/src/link/testing/local-client.ts +30 -0
- package/src/link/testing/local-server.ts +26 -0
- package/src/link/testing/local-worker.ts +17 -0
- package/src/link/testing/server.ts +131 -0
- package/src/link/testing/worker.ts +32 -0
- package/src/link/testing-cache.ts +20 -0
- package/src/link/testing-fixtures.ts +16 -0
- package/src/link/types.ts +27 -0
- package/src/link/validation.ts +44 -0
- package/src/link/vitest.browser.config.ts +3 -0
- package/src/markers.ts +18 -0
- package/src/publication/build.ts +27 -0
- package/src/publication/fixtures/build.mjs +2 -0
- package/src/publication/index.ts +3 -0
- package/src/publication/lock.ts +32 -0
- package/src/publication/manifest.ts +14 -0
- package/src/publication/publish.ts +80 -0
- package/src/publication/runtime.ts +28 -0
- package/src/publication/schema.ts +13 -0
- package/src/review-schema.ts +17 -0
- package/src/schema.ts +175 -0
- package/src/storage/db.ts +64 -0
- package/src/styles.ts +40 -0
- package/src/sw/activity.ts +66 -0
- package/src/sw/barrels.ts +44 -0
- package/src/sw/candidates.ts +14 -0
- package/src/sw/compile.ts +92 -0
- package/src/sw/compiler-columns.ts +44 -0
- package/src/sw/compiler-internals.d.ts +8 -0
- package/src/sw/compiler-plugins.ts +18 -0
- package/src/sw/compiler.ts +93 -0
- package/src/sw/composition.ts +86 -0
- package/src/sw/content-types.ts +28 -0
- package/src/sw/converted.browser.ts +31 -0
- package/src/sw/converted.ts +64 -0
- package/src/sw/editable-sites.ts +42 -0
- package/src/sw/errors.ts +30 -0
- package/src/sw/esm-sh-fixtures/browser-client.ts +18 -0
- package/src/sw/esm-sh-fixtures/browser-server.ts +104 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-entry.headers +15 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-module.headers +13 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-module.mjs.txt +142 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-entry.headers +15 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-module.headers +11 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-module.mjs.txt +222 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-entry.headers +14 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-module.headers +11 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-module.mjs.txt +76 -0
- package/src/sw/esm-sh-fixtures/fixtures.ts +54 -0
- package/src/sw/esm-sh-fixtures/redact.ts +1 -0
- package/src/sw/index.ts +7 -0
- package/src/sw/integrity.ts +10 -0
- package/src/sw/legacy-storage.ts +18 -0
- package/src/sw/link-activity.ts +21 -0
- package/src/sw/link-cache.ts +19 -0
- package/src/sw/link-compiler.ts +120 -0
- package/src/sw/link-folder.ts +29 -0
- package/src/sw/link-relay.ts +40 -0
- package/src/sw/link-route.ts +239 -0
- package/src/sw/link-source.ts +41 -0
- package/src/sw/markers.ts +128 -0
- package/src/sw/module-transform.ts +364 -0
- package/src/sw/page-clients.ts +45 -0
- package/src/sw/profile.ts +90 -0
- package/src/sw/resolver.ts +224 -0
- package/src/sw/router.ts +360 -0
- package/src/sw/runtime-exports.ts +39 -0
- package/src/sw/runtime-module.ts +13 -0
- package/src/sw/source-hash.ts +37 -0
- package/src/sw/source-provider.ts +20 -0
- package/src/sw/versions.ts +96 -0
- package/src/sw/worker.ts +91 -0
- package/static/README.md +15 -0
- package/static/profile.example.json +15 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
2
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
4
|
+
import type { BarrelIndex, Fragment } from "../schema.ts";
|
|
5
|
+
import { libraryBarrelIndex } from "./barrels.ts";
|
|
6
|
+
import { convertCommonJs } from "./convert-commonjs.ts";
|
|
7
|
+
import { BaseSetRemapError, CommonJsConversionError, NodeBuiltinError } from "./errors.ts";
|
|
8
|
+
import { composeImportMap, fragmentOf } from "./fragment.ts";
|
|
9
|
+
import { traceRecipeDependencies } from "./recipe-dependencies.ts";
|
|
10
|
+
import { validateCompilerPlugins } from "../sw/compiler-plugins.ts";
|
|
11
|
+
import { ClosureTracer, createResolver, EMPTY_MODULE, exportEntries, IMPORT_CONDITIONS, packageName, resolveEntry, type FragmentSpec } from "./trace.ts";
|
|
12
|
+
|
|
13
|
+
import { BASE_PACKAGES, BASE_ENTRIES } from "./base.ts";
|
|
14
|
+
export { BASE_PACKAGES, BASE_ENTRIES } from "./base.ts";
|
|
15
|
+
export type LibraryRuntimeOptions = { libraryDir: string; outDir: string; base?: Fragment; aliases?: Record<string, string>; exclude?: string[] };
|
|
16
|
+
export type BuiltLibraryRuntime = { fragment: Fragment; barrels: BarrelIndex };
|
|
17
|
+
|
|
18
|
+
export function librarySpec(options: { libraryDir: string; base?: Fragment; exclude?: string[] }): FragmentSpec {
|
|
19
|
+
const dir = realpathSync(options.libraryDir);
|
|
20
|
+
const manifest = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")) as { name?: string; version?: string; pageRuntime?: { compiler?: unknown } };
|
|
21
|
+
if (!manifest.name || !manifest.version) throw new Error(`${dir}/package.json must declare name and version`);
|
|
22
|
+
const exported = exportEntries(dir);
|
|
23
|
+
const compiler = manifest.pageRuntime?.compiler === undefined ? undefined : validateCompilerPlugins(manifest.pageRuntime.compiler, { library: manifest.name, path: `${manifest.name} package.json pageRuntime.compiler` });
|
|
24
|
+
const entries = exported.filter(({ path, specifier }) => !options.exclude?.includes(specifier) && path.startsWith(`${join(dir, "dist")}${sep}`) && !path.startsWith(`${join(dir, "dist/runtime")}${sep}`));
|
|
25
|
+
if (!entries.length) throw new Error(`${manifest.name}: no browser exports in dist; build the library first`);
|
|
26
|
+
for (const plugin of compiler?.remark ?? []) {
|
|
27
|
+
const entry = exported.find(({ specifier }) => specifier === plugin.module);
|
|
28
|
+
if (!entry) throw new Error(`${manifest.name}: compiler plugin ${plugin.module} is not in package.json exports`);
|
|
29
|
+
if (!entries.some(({ specifier }) => specifier === entry.specifier)) entries.push(entry);
|
|
30
|
+
}
|
|
31
|
+
// Sweep only directories the package exports into: computed dynamic imports load chunks next to exported modules,
|
|
32
|
+
// while other dist artifacts (a CLI's inlined classic-script bundle) are never served as modules.
|
|
33
|
+
const exportedDirs = [...new Set(entries.filter(({ path }) => /\.m?js$/.test(path)).map(({ path }) => dirname(path)))];
|
|
34
|
+
const excluded = exported.filter(({ specifier }) => options.exclude?.includes(specifier));
|
|
35
|
+
const excludedDirs = excluded.map(({ path }) => dirname(path)).filter(dir => !entries.some(entry => entry.path.startsWith(`${dir}${sep}`)));
|
|
36
|
+
const files = readdirSync(join(dir, "dist"), { recursive: true, encoding: "utf8" })
|
|
37
|
+
.filter((path) => !path.startsWith(`runtime${sep}`) && /\.(m?js)$/.test(path))
|
|
38
|
+
.map((path) => join(dir, "dist", path))
|
|
39
|
+
.filter(path => !excluded.some(entry => entry.path === path) && !excludedDirs.some(dir => path.startsWith(`${dir}${sep}`)))
|
|
40
|
+
.filter((path) => exportedDirs.some((exported) => path.startsWith(`${exported}${sep}`)));
|
|
41
|
+
const providesBase = !options.base && manifest.name === "@flanksource/clicky-ui";
|
|
42
|
+
if (providesBase) {
|
|
43
|
+
const resolver = createResolver(IMPORT_CONDITIONS);
|
|
44
|
+
for (const specifier of BASE_ENTRIES) {
|
|
45
|
+
// scheduler is a react-dom dependency and may not be hoisted by pnpm.
|
|
46
|
+
const from = specifier === "scheduler" ? realpathSync(join(dir, "node_modules/react-dom")) : dir;
|
|
47
|
+
entries.push(resolveEntry(resolver, from, specifier));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { name: manifest.name, resolveFrom: dir, runtimeBase: `${manifest.name}@${manifest.version}/dist/runtime/`, entries, files, topLevel: providesBase ? BASE_PACKAGES : [], inherits: options.base ? [...new Set(Object.keys(options.base.imports).map(packageName))] : [], ...(compiler ? { compiler } : {}) };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function assertTrace(tracer: ClosureTracer): void {
|
|
54
|
+
if (!tracer.failures.length) return;
|
|
55
|
+
const message = tracer.failures.map(({ name, detail }) => `${name}: ${detail}`).join("\n");
|
|
56
|
+
if (/Node built-in|requires node:/.test(message)) throw new NodeBuiltinError(message);
|
|
57
|
+
if (/still calls require/.test(message)) throw new CommonJsConversionError(message);
|
|
58
|
+
throw new Error(message);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function assertBase(options: LibraryRuntimeOptions, fragment: Fragment): void {
|
|
62
|
+
if (!options.base) return;
|
|
63
|
+
const inherited = new Set(Object.keys(options.base.imports).map(packageName));
|
|
64
|
+
for (const table of [fragment.imports, ...Object.values(fragment.scopes), fragment.aliases]) {
|
|
65
|
+
for (const specifier of Object.keys(table)) {
|
|
66
|
+
if (inherited.has(packageName(specifier))) throw new BaseSetRemapError(`${fragment.name} remaps base-set specifier ${specifier} from ${options.base.name}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (const url of Object.keys(fragment.remaps)) {
|
|
70
|
+
if ([...inherited].some((name) => url.startsWith(`${name}@`))) throw new BaseSetRemapError(`${fragment.name} remaps base-set URL ${url} from ${options.base.name}`);
|
|
71
|
+
}
|
|
72
|
+
for (const specifier of fragment.inherits) {
|
|
73
|
+
if (!options.base.imports[specifier]) throw new BaseSetRemapError(`${fragment.name} inherits ${specifier}, which ${options.base.name} does not provide`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function buildLibraryRuntime(options: LibraryRuntimeOptions): Promise<BuiltLibraryRuntime> {
|
|
78
|
+
const tracer = new ClosureTracer(librarySpec(options)).trace();
|
|
79
|
+
await traceRecipeDependencies(tracer);
|
|
80
|
+
assertTrace(tracer);
|
|
81
|
+
assertBase(options, fragmentOf(tracer, options.aliases));
|
|
82
|
+
await rm(options.outDir, { recursive: true, force: true });
|
|
83
|
+
await mkdir(options.outDir, { recursive: true });
|
|
84
|
+
try {
|
|
85
|
+
await convertCommonJs(tracer, resolve(options.outDir));
|
|
86
|
+
} catch (cause) {
|
|
87
|
+
assertTrace(tracer);
|
|
88
|
+
if (cause instanceof CommonJsConversionError) throw cause;
|
|
89
|
+
throw new CommonJsConversionError(`${tracer.spec.name}: converting ${[...tracer.commonJs.values()].map((entry) => entry.specifier).join(", ")}: ${String(cause)}`, { cause });
|
|
90
|
+
}
|
|
91
|
+
assertTrace(tracer);
|
|
92
|
+
const fragment = fragmentOf(tracer, options.aliases);
|
|
93
|
+
if (fragment.integrity[`${tracer.spec.runtimeBase}empty.js`]) await writeFile(join(options.outDir, "empty.js"), EMPTY_MODULE);
|
|
94
|
+
assertBase(options, fragment);
|
|
95
|
+
const composed = composeImportMap(options.base ? [options.base, fragment] : [fragment], "/npm/");
|
|
96
|
+
if (composed.failures.length) throw new BaseSetRemapError(composed.failures.map((failure) => failure.detail).join("\n"));
|
|
97
|
+
const barrels = libraryBarrelIndex({ files: tracer.files, remapped: new Set(Object.keys(fragment.remaps)), map: composed.map }).index;
|
|
98
|
+
await writeFile(join(options.outDir, "importmap.json"), `${JSON.stringify(fragment, null, 2)}\n`);
|
|
99
|
+
await writeFile(join(options.outDir, "barrels.json"), `${JSON.stringify(barrels, null, 2)}\n`);
|
|
100
|
+
return { fragment, barrels };
|
|
101
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { isBuiltin } from "node:module";
|
|
2
|
+
|
|
3
|
+
export function specifierKind(specifier: string): "relative" | "url" | "builtin" | "bare" {
|
|
4
|
+
if (/^(\.{1,2}\/|\/)/.test(specifier)) return "relative";
|
|
5
|
+
if (isBuiltin(specifier) || specifier.startsWith("node:")) return "builtin";
|
|
6
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(specifier)) return "url";
|
|
7
|
+
return "bare";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const NPM_PREFIX = "/npm/";
|
|
11
|
+
export const CANONICAL_CANDIDATES = ["", ".mjs", ".js", ".cjs", ".json", "/index.mjs", "/index.js", "/index.cjs"];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import type { PageStylesRecipe } from "../schema.ts";
|
|
3
|
+
import type { ClosureTracer } from "./trace.ts";
|
|
4
|
+
|
|
5
|
+
export async function traceRecipeDependencies(tracer: ClosureTracer): Promise<void> {
|
|
6
|
+
const entry = tracer.spec.entries.find(({ specifier }) => specifier === `${tracer.spec.name}/page-styles`);
|
|
7
|
+
if (!entry) return;
|
|
8
|
+
const loaded = await import(pathToFileURL(entry.path).href) as { pageStyles?: PageStylesRecipe; default?: PageStylesRecipe };
|
|
9
|
+
const recipe = loaded.pageStyles ?? loaded.default;
|
|
10
|
+
if (!recipe || !Array.isArray(recipe.runtimeDependencies) || recipe.runtimeDependencies.some((value) => typeof value !== "string" || !value || /^[a-z][a-z0-9+.-]*:/i.test(value))) {
|
|
11
|
+
throw new Error(`${entry.specifier}: runtimeDependencies must declare package specifiers or relative files`);
|
|
12
|
+
}
|
|
13
|
+
tracer.traceDependencies({ path: entry.path, dependencies: recipe.runtimeDependencies });
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { BASE_ENTRIES } from "./base.ts";
|
|
4
|
+
|
|
5
|
+
export function runtimeEntries(options: { root: string; libraries: string[] }): string[] {
|
|
6
|
+
return [...new Set([...BASE_ENTRIES, ...options.libraries.flatMap(name => {
|
|
7
|
+
const fragment = JSON.parse(readFileSync(join(options.root, "node_modules", name, "dist/runtime/importmap.json"), "utf8")) as { imports: Record<string, string> };
|
|
8
|
+
return Object.entries(fragment.imports).filter(([, path]) => /\.m?js$/.test(path)).map(([source]) => source);
|
|
9
|
+
})])];
|
|
10
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, join } from "node:path";
|
|
3
|
+
import { ResolverFactory } from "rolldown/experimental";
|
|
4
|
+
import type { Fragment } from "../schema.ts";
|
|
5
|
+
import { BASE_ENTRIES, BASE_PACKAGES } from "./base.ts";
|
|
6
|
+
import { createResolver, exportEntries, IMPORT_CONDITIONS, packageName, packageOf } from "./trace.ts";
|
|
7
|
+
|
|
8
|
+
export interface RuntimeExport {
|
|
9
|
+
browser: string;
|
|
10
|
+
node: string;
|
|
11
|
+
version: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const browserResolver = createResolver(IMPORT_CONDITIONS);
|
|
15
|
+
const nodeResolver = new ResolverFactory({ conditionNames: ["node", "import", "default"], mainFields: ["module", "main"], extensions: [".mjs", ".js", ".cjs", ".json"], symlinks: true });
|
|
16
|
+
const javaScript = new Set([".js", ".mjs", ".cjs"]);
|
|
17
|
+
|
|
18
|
+
function readManifest(root: string): { name: string; version: string; exports?: unknown } {
|
|
19
|
+
return JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function libraryRuntime(root: string, library: string) {
|
|
23
|
+
const dir = realpathSync(join(root, "node_modules", library));
|
|
24
|
+
const { version } = readManifest(dir);
|
|
25
|
+
const fragment: Fragment = JSON.parse(readFileSync(join(dir, "dist/runtime/importmap.json"), "utf8"));
|
|
26
|
+
return { dir, version, fragment, observed: fragment.scopes[`${library}@${version}/`] ?? {} };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function runtimeExportPins(options: { root: string; library: string; inherited: Record<string, string> }): Record<string, string> {
|
|
30
|
+
const { dir, observed } = libraryRuntime(options.root, options.library);
|
|
31
|
+
const pins: Record<string, string> = {};
|
|
32
|
+
for (const [specifier, target] of Object.entries(observed)) {
|
|
33
|
+
const name = packageName(specifier);
|
|
34
|
+
if (name === options.library || BASE_PACKAGES.includes(name) || Object.hasOwn(options.inherited, name)) continue;
|
|
35
|
+
const owner = target.indexOf(`${name}@`);
|
|
36
|
+
if (owner < 0 || (owner > 0 && target[owner - 1] !== "/")) continue;
|
|
37
|
+
const version = target.slice(owner + name.length + 1).split("/")[0]!;
|
|
38
|
+
const browser = browserResolver.sync(dir, specifier).path;
|
|
39
|
+
if (browser && packageOf(browser).version === version) continue;
|
|
40
|
+
if (pins[name] && pins[name] !== version) throw new Error(`${options.library} exposes multiple versions of ${name}`);
|
|
41
|
+
pins[name] = version;
|
|
42
|
+
}
|
|
43
|
+
return pins;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function packageExports(options: { dir: string; from: string; observed: string[] }): Record<string, RuntimeExport> {
|
|
47
|
+
const { dir, from, observed } = options;
|
|
48
|
+
const manifest = readManifest(dir);
|
|
49
|
+
const candidates = manifest.exports === undefined ? [manifest.name, ...observed] : exportEntries(dir).map(entry => entry.specifier);
|
|
50
|
+
const result: Record<string, RuntimeExport> = {};
|
|
51
|
+
for (const specifier of new Set(candidates)) {
|
|
52
|
+
const browser = browserResolver.sync(from, specifier).path;
|
|
53
|
+
if (!browser || !javaScript.has(extname(browser))) continue;
|
|
54
|
+
if (packageOf(browser).dir !== dir) continue;
|
|
55
|
+
const node = nodeResolver.sync(from, specifier).path;
|
|
56
|
+
result[specifier] = { browser, node: node && javaScript.has(extname(node)) ? node : browser, version: manifest.version };
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function discoverRuntimeExports(options: { root: string; library?: string; packages?: string[]; pinnedRoot?: string; pins?: Record<string, string> }): Record<string, RuntimeExport> {
|
|
62
|
+
const { root, library, packages = [], pinnedRoot, pins = {} } = options;
|
|
63
|
+
const result: Record<string, RuntimeExport> = {};
|
|
64
|
+
for (const name of packages) {
|
|
65
|
+
const dir = realpathSync(join(root, "node_modules", name));
|
|
66
|
+
Object.assign(result, packageExports({ dir, from: root, observed: [] }));
|
|
67
|
+
}
|
|
68
|
+
if (!library) return result;
|
|
69
|
+
const { dir, version, fragment, observed } = libraryRuntime(root, library);
|
|
70
|
+
for (const [specifier, target] of Object.entries(fragment.imports)) {
|
|
71
|
+
if (packageName(specifier) !== library || !javaScript.has(extname(target))) continue;
|
|
72
|
+
const browser = browserResolver.sync(root, specifier).path;
|
|
73
|
+
if (!browser) throw new Error(`${library}: cannot resolve public runtime export ${specifier}`);
|
|
74
|
+
result[specifier] = { browser, node: nodeResolver.sync(root, specifier).path ?? browser, version };
|
|
75
|
+
}
|
|
76
|
+
const byOwner = new Map<string, string[]>();
|
|
77
|
+
for (const specifier of Object.keys(observed)) {
|
|
78
|
+
const name = packageName(specifier);
|
|
79
|
+
if (name === library || BASE_PACKAGES.includes(name)) continue;
|
|
80
|
+
const specifiers = byOwner.get(name) ?? [];
|
|
81
|
+
specifiers.push(specifier);
|
|
82
|
+
byOwner.set(name, specifiers);
|
|
83
|
+
}
|
|
84
|
+
for (const [name, specifiers] of byOwner) {
|
|
85
|
+
const from = pins[name] && pinnedRoot ? pinnedRoot : dir;
|
|
86
|
+
const entry = specifiers.map(name => browserResolver.sync(from, name).path).find(path => path && existsSync(path));
|
|
87
|
+
if (!entry) throw new Error(`${library}: cannot resolve runtime dependency ${specifiers.join(", ")}`);
|
|
88
|
+
Object.assign(result, packageExports({ dir: packageOf(entry).dir, from, observed: specifiers }));
|
|
89
|
+
}
|
|
90
|
+
if (library === "@flanksource/clicky-ui") {
|
|
91
|
+
for (const specifier of new Set([...BASE_ENTRIES, ...Object.keys(fragment.imports).filter(name => BASE_PACKAGES.includes(packageName(name)))])) {
|
|
92
|
+
const from = specifier === "scheduler" ? dirname(browserResolver.sync(dir, "react-dom").path!) : dir;
|
|
93
|
+
const browser = browserResolver.sync(from, specifier).path;
|
|
94
|
+
if (!browser) throw new Error(`${library}: cannot resolve shared runtime dependency ${specifier}`);
|
|
95
|
+
result[specifier] = { browser, node: nodeResolver.sync(from, specifier).path ?? browser, version: packageOf(browser).version };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}
|