@inkandswitch/patchwork-bootloader 0.4.1 → 0.4.3

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.
@@ -5,6 +5,7 @@ import type {
5
5
  } from "./patchwork-plugin.js";
6
6
  import { readFileSync } from "node:fs";
7
7
  import { createRequire } from "node:module";
8
+ import { fileURLToPath } from "node:url";
8
9
  const require = createRequire(import.meta.url);
9
10
 
10
11
  /**
@@ -18,6 +19,36 @@ export const builtins = externals.reduce(
18
19
  {} as Record<string, string>
19
20
  );
20
21
 
22
+ /**
23
+ * pretend the import came from inside this package, so node_modules resolution
24
+ * walks up from *our* directory and finds our copy of each external. that's why
25
+ * a consuming site never has to install them or agree with us about versions.
26
+ */
27
+ const self = fileURLToPath(import.meta.url);
28
+
29
+ /**
30
+ * resolve an external from our node_modules rather than the site's.
31
+ *
32
+ * this goes through rollup's resolver rather than import.meta.resolve or
33
+ * require.resolve because those apply node's conditions: subduction (and
34
+ * others) would hand us `dist/esm/node.js`, which imports node:path and blows
35
+ * up at bundle time. rollup applies the browser conditions vite configured.
36
+ */
37
+ async function resolveExternal(
38
+ this: import("rollup").PluginContext,
39
+ name: string
40
+ ) {
41
+ const resolved = await this.resolve(name, self, { skipSelf: true });
42
+ if (!resolved) {
43
+ throw new Error(
44
+ `@patchwork/vite: couldn't resolve the external "${name}" from ` +
45
+ `@inkandswitch/patchwork-bootloader. it should be one of the ` +
46
+ `bootloader's own dependencies.`
47
+ );
48
+ }
49
+ return resolved.id;
50
+ }
51
+
21
52
  /**
22
53
  * merge the importmap option with our builtins
23
54
  */
@@ -40,7 +71,7 @@ export function importmap(options?: PatchworkVitePluginOptions): Plugin {
40
71
  this.emitFile({
41
72
  type: "chunk",
42
73
  fileName: fileName.slice(1),
43
- id,
74
+ id: await resolveExternal.call(this, id),
44
75
  preserveSignature: "strict",
45
76
  });
46
77
  }
@@ -72,8 +103,14 @@ export function importmap(options?: PatchworkVitePluginOptions): Plugin {
72
103
  source: readFileSync(subdWasmPath),
73
104
  });
74
105
  },
75
- resolveId(id) {
76
- if (id in importmap.imports && !(id in builtins)) {
106
+ async resolveId(id) {
107
+ if (id in builtins) {
108
+ // point the site's own imports at the same copy we emit as a chunk,
109
+ // otherwise rollup bundles a second one out of the site's node_modules
110
+ // and you end up with two automerges racing to init the same wasm
111
+ return resolveExternal.call(this, id);
112
+ }
113
+ if (id in importmap.imports) {
77
114
  return { id: importmap.imports[id], external: true };
78
115
  }
79
116
  },