@inkandswitch/patchwork-bootloader 0.4.3 → 0.5.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.
@@ -1,105 +0,0 @@
1
- import { readFileSync } from "node:fs";
2
- import { createRequire } from "node:module";
3
- import { fileURLToPath } from "node:url";
4
- const require = createRequire(import.meta.url);
5
- /**
6
- * these dependencies will be built into the outdir,
7
- * and injected into the importmap
8
- */
9
- import externals from "../externals.js";
10
- export const builtins = externals.reduce((builtins, name) => ((builtins[name] = `/packages/${name}.js`), builtins), {});
11
- /**
12
- * pretend the import came from inside this package, so node_modules resolution
13
- * walks up from *our* directory and finds our copy of each external. that's why
14
- * a consuming site never has to install them or agree with us about versions.
15
- */
16
- const self = fileURLToPath(import.meta.url);
17
- /**
18
- * resolve an external from our node_modules rather than the site's.
19
- *
20
- * this goes through rollup's resolver rather than import.meta.resolve or
21
- * require.resolve because those apply node's conditions: subduction (and
22
- * others) would hand us `dist/esm/node.js`, which imports node:path and blows
23
- * up at bundle time. rollup applies the browser conditions vite configured.
24
- */
25
- async function resolveExternal(name) {
26
- const resolved = await this.resolve(name, self, { skipSelf: true });
27
- if (!resolved) {
28
- throw new Error(`@patchwork/vite: couldn't resolve the external "${name}" from ` +
29
- `@inkandswitch/patchwork-bootloader. it should be one of the ` +
30
- `bootloader's own dependencies.`);
31
- }
32
- return resolved.id;
33
- }
34
- /**
35
- * merge the importmap option with our builtins
36
- */
37
- function createImportMap(options) {
38
- const importmap = structuredClone(options?.importmap ?? { imports: {}, scopes: {} });
39
- importmap.imports ??= {};
40
- importmap.scopes ??= {};
41
- Object.assign(importmap.imports, builtins);
42
- return { importmap, builtins };
43
- }
44
- export function importmap(options) {
45
- const { importmap, builtins } = createImportMap(options);
46
- return {
47
- name: "@patchwork/vite",
48
- async buildStart() {
49
- for (const [id, fileName] of Object.entries(builtins)) {
50
- this.emitFile({
51
- type: "chunk",
52
- fileName: fileName.slice(1),
53
- id: await resolveExternal.call(this, id),
54
- preserveSignature: "strict",
55
- });
56
- }
57
- // Emit automerge, keyhive, and subduction wasm so the service worker can fetch them
58
- const automergeWasmPath = require.resolve("@automerge/automerge/automerge.wasm");
59
- this.emitFile({
60
- type: "asset",
61
- fileName: "automerge.wasm",
62
- source: readFileSync(automergeWasmPath),
63
- });
64
- const keyhiveWasmPath = require.resolve("@keyhive/keyhive/keyhive_wasm.wasm");
65
- this.emitFile({
66
- type: "asset",
67
- fileName: "keyhive_wasm.wasm",
68
- source: readFileSync(keyhiveWasmPath),
69
- });
70
- // Emit subduction wasm so the service worker can fetch it
71
- const subdWasmPath = require.resolve("@automerge/automerge-subduction/wasm");
72
- this.emitFile({
73
- type: "asset",
74
- fileName: "subduction.wasm",
75
- source: readFileSync(subdWasmPath),
76
- });
77
- },
78
- async resolveId(id) {
79
- if (id in builtins) {
80
- // point the site's own imports at the same copy we emit as a chunk,
81
- // otherwise rollup bundles a second one out of the site's node_modules
82
- // and you end up with two automerges racing to init the same wasm
83
- return resolveExternal.call(this, id);
84
- }
85
- if (id in importmap.imports) {
86
- return { id: importmap.imports[id], external: true };
87
- }
88
- },
89
- transformIndexHtml: {
90
- order: "pre",
91
- handler(html) {
92
- return {
93
- html,
94
- tags: [
95
- {
96
- tag: "script",
97
- attrs: { type: "importmap" },
98
- children: JSON.stringify(importmap, null, 2),
99
- },
100
- ],
101
- };
102
- },
103
- },
104
- };
105
- }
@@ -1,14 +0,0 @@
1
- export default function patchwork(options?: PatchworkVitePluginOptions): import("vite").Plugin<any>[];
2
- type Imports = {
3
- [name: string]: string;
4
- };
5
- export type ImportMap = {
6
- imports: Imports;
7
- scopes?: {
8
- [scope: string]: Imports;
9
- };
10
- };
11
- export interface PatchworkVitePluginOptions {
12
- importmap?: ImportMap;
13
- }
14
- export {};
@@ -1,8 +0,0 @@
1
- // todo this is now not patchwork-specific, it's just a fun little importmap thing
2
- // and a separate thing that returns the service worker which doesn't really
3
- // need to be a plugin at all
4
- import { importmap } from "./importmap-plugin.js";
5
- import { serviceworker } from "./service-worker-plugin.js";
6
- export default function patchwork(options) {
7
- return [importmap(options), serviceworker()];
8
- }
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "vite";
2
- export declare function serviceworker(): Plugin;
@@ -1,42 +0,0 @@
1
- import { builtins } from "./importmap-plugin.js";
2
- // The service worker and the automerge shared worker are emitted as their
3
- // own chunks. Their heavy imports are marked external and resolved to
4
- // /packages/... URLs (both workers are created with type:"module", so the
5
- // browser fetches those as regular network requests).
6
- const workers = [
7
- {
8
- specifier: "@inkandswitch/patchwork-bootloader/service-worker",
9
- fileName: "service-worker.js",
10
- },
11
- {
12
- specifier: "@inkandswitch/patchwork-bootloader/automerge-worker",
13
- fileName: "automerge-worker.js",
14
- },
15
- {
16
- specifier: "@inkandswitch/patchwork-bootloader/module-loader-worker",
17
- fileName: "module-loader-worker.js",
18
- },
19
- ];
20
- export function serviceworker() {
21
- const entryIds = new Set();
22
- return {
23
- name: "@patchwork/service-worker",
24
- enforce: "pre",
25
- async buildStart() {
26
- for (const { specifier, fileName } of workers) {
27
- const resolved = await this.resolve(specifier);
28
- entryIds.add(resolved.id);
29
- this.emitFile({
30
- type: "chunk",
31
- id: resolved.id,
32
- fileName,
33
- });
34
- }
35
- },
36
- resolveId(source, importer) {
37
- if (importer && entryIds.has(importer) && source in builtins) {
38
- return { id: builtins[source], external: true };
39
- }
40
- },
41
- };
42
- }