@chee/patchwork-bundles 0.1.0 → 0.2.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/dist/esbuild.d.ts +3 -0
- package/dist/esbuild.d.ts.map +1 -0
- package/{src/esbuild.ts → dist/esbuild.js} +1 -0
- package/dist/esbuild.js.map +1 -0
- package/dist/farm.d.ts +3 -0
- package/dist/farm.d.ts.map +1 -0
- package/{src/farm.ts → dist/farm.js} +1 -0
- package/dist/farm.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +98 -0
- package/dist/index.js.map +1 -0
- package/dist/rollup.d.ts +3 -0
- package/dist/rollup.d.ts.map +1 -0
- package/{src/rollup.ts → dist/rollup.js} +1 -0
- package/dist/rollup.js.map +1 -0
- package/dist/rspack.d.ts +3 -0
- package/dist/rspack.d.ts.map +1 -0
- package/{src/rspack.ts → dist/rspack.js} +1 -0
- package/dist/rspack.js.map +1 -0
- package/dist/test/integration.test.d.ts +2 -0
- package/dist/test/integration.test.d.ts.map +1 -0
- package/dist/test/integration.test.js +172 -0
- package/dist/test/integration.test.js.map +1 -0
- package/dist/types.d.ts +41 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +42 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +141 -0
- package/dist/utils.js.map +1 -0
- package/dist/vite.d.ts +3 -0
- package/dist/vite.d.ts.map +1 -0
- package/{src/vite.ts → dist/vite.js} +1 -0
- package/dist/vite.js.map +1 -0
- package/dist/webpack.d.ts +3 -0
- package/dist/webpack.d.ts.map +1 -0
- package/{src/webpack.ts → dist/webpack.js} +1 -0
- package/dist/webpack.js.map +1 -0
- package/package.json +4 -1
- package/.claude/settings.local.json +0 -27
- package/pnpm-workspace.yaml +0 -41
- package/src/index.ts +0 -133
- package/src/types.ts +0 -52
- package/src/utils.ts +0 -166
- package/test/integration.test.ts +0 -305
- package/tsconfig.json +0 -40
- package/vitest.config.d.ts +0 -3
- package/vitest.config.d.ts.map +0 -1
- package/vitest.config.js +0 -10
- package/vitest.config.js.map +0 -1
- package/vitest.config.ts +0 -10
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"esbuild.d.ts","sourceRoot":"","sources":["../src/esbuild.ts"],"names":[],"mappings":";AACA,wBAAwC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"esbuild.js","sourceRoot":"","sources":["../src/esbuild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,eAAe,gBAAgB,CAAC,OAAO,CAAC"}
|
package/dist/farm.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"farm.d.ts","sourceRoot":"","sources":["../src/farm.ts"],"names":[],"mappings":";AACA,wBAAqC"}
|
package/dist/farm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"farm.js","sourceRoot":"","sources":["../src/farm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,eAAe,gBAAgB,CAAC,IAAI,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type PatchworkBundleOptions } from "./types.js";
|
|
2
|
+
export declare const patchworkBundles: import("unplugin").UnpluginInstance<PatchworkBundleOptions | undefined, boolean>;
|
|
3
|
+
export default patchworkBundles;
|
|
4
|
+
export type { PatchworkBundleOptions } from "./types.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EAAkB,KAAK,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAUzE,eAAO,MAAM,gBAAgB,kFA0G5B,CAAC;AAEF,eAAe,gBAAgB,CAAC;AAChC,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createUnplugin } from "unplugin";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { readPackageJson, parseBareSpecifier, isBareSpecifier, resolveDepEntryPoint, collectModuleExports, ensureLexer, } from "./utils.js";
|
|
4
|
+
import { getImportableUrlFromAutomergeUrl } from "@inkandswitch/patchwork-filesystem";
|
|
5
|
+
import externals from "@inkandswitch/patchwork-bootloader/externals";
|
|
6
|
+
import { resolveOptions } from "./types.js";
|
|
7
|
+
// Prefix marking a virtual module we generate for
|
|
8
|
+
// `rewrite.automerge: "patchwork:cross-origin"`. The rest of the id is the
|
|
9
|
+
// original bare specifier. The leading NUL keeps other tooling from treating it
|
|
10
|
+
// as a real file path.
|
|
11
|
+
const CROSS_ORIGIN_PREFIX = "\0patchwork-bundle-cross-origin:";
|
|
12
|
+
const VALID_IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
13
|
+
export const patchworkBundles = createUnplugin((rawOptions) => {
|
|
14
|
+
const options = resolveOptions(rawOptions);
|
|
15
|
+
let deps;
|
|
16
|
+
return {
|
|
17
|
+
name: "patchwork-bundles",
|
|
18
|
+
enforce: "pre",
|
|
19
|
+
async buildStart() {
|
|
20
|
+
const pkgPath = resolve(process.cwd(), options.packageJsonPath);
|
|
21
|
+
const pkgJson = readPackageJson(pkgPath);
|
|
22
|
+
deps = pkgJson.dependencies ?? {};
|
|
23
|
+
if (options.rewrite.automerge === "patchwork:cross-origin") {
|
|
24
|
+
await ensureLexer();
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
resolveId(id) {
|
|
28
|
+
// Virtual modules we emitted below are handled in load().
|
|
29
|
+
if (id.startsWith(CROSS_ORIGIN_PREFIX))
|
|
30
|
+
return id;
|
|
31
|
+
if (!isBareSpecifier(id))
|
|
32
|
+
return undefined;
|
|
33
|
+
// Bootloader externals — always external (served via importmap)
|
|
34
|
+
if (externals.includes(id)) {
|
|
35
|
+
return { id, external: true };
|
|
36
|
+
}
|
|
37
|
+
const { pkgName, subpath } = parseBareSpecifier(id);
|
|
38
|
+
if (pkgName !== id && externals.includes(pkgName)) {
|
|
39
|
+
return { id, external: true };
|
|
40
|
+
}
|
|
41
|
+
const version = deps[pkgName];
|
|
42
|
+
if (!version)
|
|
43
|
+
return undefined;
|
|
44
|
+
// Automerge deps — rewrite to service worker URLs
|
|
45
|
+
if (version.startsWith("automerge:")) {
|
|
46
|
+
if (options.rewrite.automerge === "patchwork") {
|
|
47
|
+
const entryPoint = resolveDepEntryPoint(pkgName, subpath);
|
|
48
|
+
const url = getImportableUrlFromAutomergeUrl(version, entryPoint);
|
|
49
|
+
return { id: url, external: true };
|
|
50
|
+
}
|
|
51
|
+
// Cross-origin mode: defer URL construction to the browser so the
|
|
52
|
+
// service-worker path resolves against the *running document's*
|
|
53
|
+
// origin, not the (possibly cross-origin) module's. Emit a virtual
|
|
54
|
+
// re-export module; the code is generated in load().
|
|
55
|
+
if (options.rewrite.automerge === "patchwork:cross-origin") {
|
|
56
|
+
return CROSS_ORIGIN_PREFIX + id;
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
// npm fallback — optionally rewrite to esm.sh
|
|
61
|
+
if (options.rewrite.npm === "esm.sh") {
|
|
62
|
+
const cleanVersion = version.replace(/^[\^~>=<\s]+/, "");
|
|
63
|
+
const esmId = subpath === "." ? pkgName : id;
|
|
64
|
+
return {
|
|
65
|
+
id: `https://esm.sh/${esmId}@${cleanVersion}`,
|
|
66
|
+
external: true,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
},
|
|
71
|
+
load(id) {
|
|
72
|
+
if (!id.startsWith(CROSS_ORIGIN_PREFIX))
|
|
73
|
+
return undefined;
|
|
74
|
+
const spec = id.slice(CROSS_ORIGIN_PREFIX.length);
|
|
75
|
+
const { pkgName, subpath } = parseBareSpecifier(spec);
|
|
76
|
+
const version = deps[pkgName];
|
|
77
|
+
const entryPoint = resolveDepEntryPoint(pkgName, subpath);
|
|
78
|
+
// Enumerate the dep's named exports so the virtual module can re-export
|
|
79
|
+
// the same shape. Read the installed copy in node_modules — assumed to
|
|
80
|
+
// match the automerge doc the service worker serves.
|
|
81
|
+
const entryFile = resolve(process.cwd(), "node_modules", pkgName, entryPoint);
|
|
82
|
+
const exportNames = collectModuleExports(entryFile);
|
|
83
|
+
const hasDefault = exportNames.delete("default");
|
|
84
|
+
const named = [...exportNames].filter((n) => VALID_IDENT.test(n));
|
|
85
|
+
const lines = [
|
|
86
|
+
`import { getImportableUrlFromAutomergeUrl as __pwResolve } from "@inkandswitch/patchwork-filesystem";`,
|
|
87
|
+
`const __pwUrl = __pwResolve(${JSON.stringify(version)}, ${JSON.stringify(entryPoint)});`,
|
|
88
|
+
`const __pwMod = await import(/* @vite-ignore */ __pwUrl);`,
|
|
89
|
+
...named.map((n) => `export const ${n} = __pwMod[${JSON.stringify(n)}];`),
|
|
90
|
+
];
|
|
91
|
+
if (hasDefault)
|
|
92
|
+
lines.push(`export default __pwMod.default;`);
|
|
93
|
+
return { code: lines.join("\n"), moduleSideEffects: true };
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
export default patchworkBundles;
|
|
98
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,eAAe,EACf,oBAAoB,EACpB,oBAAoB,EACpB,WAAW,GACZ,MAA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IAAI,CAAC,iCAAiC,CAAC,CAAC;YAE9D,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;QAC7D,CAAC;KACF,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
package/dist/rollup.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup.d.ts","sourceRoot":"","sources":["../src/rollup.ts"],"names":[],"mappings":";AACA,wBAAuC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup.js","sourceRoot":"","sources":["../src/rollup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,eAAe,gBAAgB,CAAC,MAAM,CAAC"}
|
package/dist/rspack.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rspack.d.ts","sourceRoot":"","sources":["../src/rspack.ts"],"names":[],"mappings":";AACA,wBAAuC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rspack.js","sourceRoot":"","sources":["../src/rspack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,eAAe,gBAAgB,CAAC,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integration.test.d.ts","sourceRoot":"","sources":["../../src/test/integration.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
6
|
+
import { build as viteBuild } from "vite";
|
|
7
|
+
import * as esbuild from "esbuild";
|
|
8
|
+
const AUTOMERGE_URL = "automerge:kFcrzeDmr5zXE1jShvxUPsoToAN";
|
|
9
|
+
const ENCODED_URL = encodeURIComponent(AUTOMERGE_URL);
|
|
10
|
+
describe("patchwork-bundles integration", () => {
|
|
11
|
+
let tmpDir;
|
|
12
|
+
let originalCwd;
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
originalCwd = process.cwd();
|
|
15
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "patchwork-bundles-test-"));
|
|
16
|
+
// Phase 1: set up project with pnpm-plugin-patchwork as a configDependency
|
|
17
|
+
await fs.writeFile(path.join(tmpDir, "package.json"), JSON.stringify({
|
|
18
|
+
name: "test-patchwork-bundles",
|
|
19
|
+
version: "0.0.0",
|
|
20
|
+
type: "module",
|
|
21
|
+
dependencies: {
|
|
22
|
+
"my-sideboard-tool": AUTOMERGE_URL,
|
|
23
|
+
"solid-js": "^1.9.9",
|
|
24
|
+
},
|
|
25
|
+
}, null, 2));
|
|
26
|
+
// stub workspace packages that the automerge doc depends on via workspace:^
|
|
27
|
+
const bootloaderDir = path.join(tmpDir, "packages/patchwork-bootloader");
|
|
28
|
+
await fs.mkdir(bootloaderDir, { recursive: true });
|
|
29
|
+
await fs.writeFile(path.join(bootloaderDir, "package.json"), JSON.stringify({
|
|
30
|
+
name: "@inkandswitch/patchwork-bootloader",
|
|
31
|
+
version: "0.0.1",
|
|
32
|
+
type: "module",
|
|
33
|
+
exports: { "./externals": { import: "./externals.js" } },
|
|
34
|
+
}));
|
|
35
|
+
await fs.writeFile(path.join(bootloaderDir, "externals.js"), `const externals = [\n "solid-js",\n "solid-js/web",\n "solid-js/html",\n "solid-js/store",\n "solid-js/jsx-runtime",\n "solid-js/h"\n];\nexport default externals;\n`);
|
|
36
|
+
const elementsDir = path.join(tmpDir, "packages/patchwork-elements");
|
|
37
|
+
await fs.mkdir(elementsDir, { recursive: true });
|
|
38
|
+
await fs.writeFile(path.join(elementsDir, "package.json"), JSON.stringify({
|
|
39
|
+
name: "@inkandswitch/patchwork-elements",
|
|
40
|
+
version: "0.0.1",
|
|
41
|
+
type: "module",
|
|
42
|
+
}));
|
|
43
|
+
await fs.writeFile(path.join(tmpDir, "pnpm-workspace.yaml"), [
|
|
44
|
+
"packages:",
|
|
45
|
+
' - "packages/*"',
|
|
46
|
+
"configDependencies:",
|
|
47
|
+
' pnpm-plugin-patchwork: "0.1.0"',
|
|
48
|
+
"",
|
|
49
|
+
].join("\n"));
|
|
50
|
+
// .npmrc — avoid strict peer deps issues
|
|
51
|
+
await fs.writeFile(path.join(tmpDir, ".npmrc"), "strict-peer-dependencies=false\nauto-install-peers=true\n");
|
|
52
|
+
// install — plugin resolves automerge: deps automatically
|
|
53
|
+
// filter out parent pnpm's npm_config_* env vars to avoid contamination
|
|
54
|
+
const cleanEnv = Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith("npm_")));
|
|
55
|
+
execSync("npx pnpm@next-11 install --no-frozen-lockfile", {
|
|
56
|
+
cwd: tmpDir,
|
|
57
|
+
stdio: "pipe",
|
|
58
|
+
timeout: 120_000,
|
|
59
|
+
env: {
|
|
60
|
+
...cleanEnv,
|
|
61
|
+
PATCHWORK_SYNC_SERVER: "wss://sync3.automerge.org",
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
// Phase 2: write source file
|
|
65
|
+
const srcDir = path.join(tmpDir, "src");
|
|
66
|
+
await fs.mkdir(srcDir, { recursive: true });
|
|
67
|
+
await fs.writeFile(path.join(srcDir, "index.ts"), [
|
|
68
|
+
`import { createSignal } from "solid-js";`,
|
|
69
|
+
`import * as sideboard from "my-sideboard-tool";`,
|
|
70
|
+
`console.log(createSignal, sideboard);`,
|
|
71
|
+
"",
|
|
72
|
+
].join("\n"));
|
|
73
|
+
process.chdir(tmpDir);
|
|
74
|
+
}, 180_000);
|
|
75
|
+
afterAll(async () => {
|
|
76
|
+
process.chdir(originalCwd);
|
|
77
|
+
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
78
|
+
});
|
|
79
|
+
describe("pnpm plugin installed my-sideboard-tool from automerge", () => {
|
|
80
|
+
it("has a package.json with the sideboard package name", async () => {
|
|
81
|
+
const pkgPath = path.join(tmpDir, "node_modules/my-sideboard-tool/package.json");
|
|
82
|
+
const pkg = JSON.parse(await fs.readFile(pkgPath, "utf-8"));
|
|
83
|
+
expect(pkg.name).toBe("@chee/patchwork-sideboard");
|
|
84
|
+
});
|
|
85
|
+
it("has an exports field with a patchwork condition", async () => {
|
|
86
|
+
const pkgPath = path.join(tmpDir, "node_modules/my-sideboard-tool/package.json");
|
|
87
|
+
const pkg = JSON.parse(await fs.readFile(pkgPath, "utf-8"));
|
|
88
|
+
expect(pkg.exports).toBeDefined();
|
|
89
|
+
expect(pkg.exports["."]).toBeDefined();
|
|
90
|
+
expect(pkg.exports["."].patchwork).toBeDefined();
|
|
91
|
+
});
|
|
92
|
+
it("contains actual dist files from the automerge document", async () => {
|
|
93
|
+
const distDir = path.join(tmpDir, "node_modules/my-sideboard-tool/dist");
|
|
94
|
+
const files = await fs.readdir(distDir);
|
|
95
|
+
expect(files.length).toBeGreaterThan(0);
|
|
96
|
+
// should have js output
|
|
97
|
+
expect(files.some((f) => f.endsWith(".js"))).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
describe("vite build", () => {
|
|
101
|
+
let output;
|
|
102
|
+
beforeAll(async () => {
|
|
103
|
+
// Determine what entry point the plugin will resolve to
|
|
104
|
+
const pluginPath = path.resolve(originalCwd, "dist/vite.js");
|
|
105
|
+
const { default: vitePlugin } = await import(pluginPath);
|
|
106
|
+
const result = await viteBuild({
|
|
107
|
+
root: tmpDir,
|
|
108
|
+
logLevel: "silent",
|
|
109
|
+
build: {
|
|
110
|
+
lib: {
|
|
111
|
+
entry: path.join(tmpDir, "src/index.ts"),
|
|
112
|
+
formats: ["es"],
|
|
113
|
+
fileName: "index",
|
|
114
|
+
},
|
|
115
|
+
outDir: path.join(tmpDir, "dist-vite"),
|
|
116
|
+
write: true,
|
|
117
|
+
minify: false,
|
|
118
|
+
},
|
|
119
|
+
plugins: [vitePlugin()],
|
|
120
|
+
});
|
|
121
|
+
// vite 7 outputs .js, older versions output .mjs
|
|
122
|
+
const mjs = path.join(tmpDir, "dist-vite/index.mjs");
|
|
123
|
+
const js = path.join(tmpDir, "dist-vite/index.js");
|
|
124
|
+
output = await fs.readFile(await fs.access(mjs).then(() => mjs, () => js), "utf-8");
|
|
125
|
+
}, 60_000);
|
|
126
|
+
it("rewrites automerge dep to service-worker URL", () => {
|
|
127
|
+
expect(output).toContain(ENCODED_URL);
|
|
128
|
+
// Should be an import from /<encoded-url>/...
|
|
129
|
+
expect(output).toMatch(new RegExp(`from ["']/${ENCODED_URL}/`));
|
|
130
|
+
});
|
|
131
|
+
it("externalizes solid-js", () => {
|
|
132
|
+
expect(output).toMatch(/from ["']solid-js["']/);
|
|
133
|
+
// if solid-js were inlined, the output would be thousands of lines
|
|
134
|
+
const lines = output.split("\n").length;
|
|
135
|
+
expect(lines).toBeLessThan(20);
|
|
136
|
+
});
|
|
137
|
+
it("does not contain bundled automerge dep code", () => {
|
|
138
|
+
// The automerge dep should be external, not inlined
|
|
139
|
+
expect(output).not.toContain("my-sideboard-tool");
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
describe("esbuild build", () => {
|
|
143
|
+
let output;
|
|
144
|
+
beforeAll(async () => {
|
|
145
|
+
const pluginPath = path.resolve(originalCwd, "dist/esbuild.js");
|
|
146
|
+
const { default: esbuildPlugin } = await import(pluginPath);
|
|
147
|
+
await esbuild.build({
|
|
148
|
+
entryPoints: [path.join(tmpDir, "src/index.ts")],
|
|
149
|
+
bundle: true,
|
|
150
|
+
format: "esm",
|
|
151
|
+
outfile: path.join(tmpDir, "dist-esbuild/index.js"),
|
|
152
|
+
plugins: [esbuildPlugin()],
|
|
153
|
+
logLevel: "silent",
|
|
154
|
+
});
|
|
155
|
+
output = await fs.readFile(path.join(tmpDir, "dist-esbuild/index.js"), "utf-8");
|
|
156
|
+
}, 60_000);
|
|
157
|
+
it("rewrites automerge dep to service-worker URL", () => {
|
|
158
|
+
expect(output).toContain(ENCODED_URL);
|
|
159
|
+
expect(output).toMatch(new RegExp(`from ["']/${ENCODED_URL}/`));
|
|
160
|
+
});
|
|
161
|
+
it("externalizes solid-js", () => {
|
|
162
|
+
expect(output).toMatch(/from ["']solid-js["']/);
|
|
163
|
+
// if solid-js were inlined, the output would be thousands of lines
|
|
164
|
+
const lines = output.split("\n").length;
|
|
165
|
+
expect(lines).toBeLessThan(20);
|
|
166
|
+
});
|
|
167
|
+
it("does not contain bundled automerge dep code", () => {
|
|
168
|
+
expect(output).not.toContain("my-sideboard-tool");
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
//# sourceMappingURL=integration.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integration.test.js","sourceRoot":"","sources":["../../src/test/integration.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AACnE,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,MAAM,aAAa,GAAG,uCAAuC,CAAC;AAC9D,MAAM,WAAW,GAAG,kBAAkB,CAAC,aAAa,CAAC,CAAC;AAEtD,QAAQ,CAAC,+BAA+B,EAAE,GAAG,EAAE;IAC7C,IAAI,MAAc,CAAC;IACnB,IAAI,WAAmB,CAAC;IAExB,SAAS,CAAC,KAAK,IAA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AAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;YACxC,MAAM,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;YACrD,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;QACpD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface PatchworkBundleOptions {
|
|
2
|
+
/** Path to the project's package.json. Default: "./package.json" relative to cwd */
|
|
3
|
+
packageJsonPath?: string;
|
|
4
|
+
rewrite?: {
|
|
5
|
+
/**
|
|
6
|
+
* How to handle dependencies with `automerge:` version specifiers.
|
|
7
|
+
* - `"patchwork"` (default): rewrite bare imports to *root-relative*
|
|
8
|
+
* service-worker URLs (`/{encodeURIComponent("automerge:docid")}/resolved/entry.js`),
|
|
9
|
+
* resolving through the dep's package.json exports with the `"patchwork"` condition.
|
|
10
|
+
* The leading `/` resolves against the *importing module's* origin, so this
|
|
11
|
+
* only works when the tool is served same-origin as the patchwork service
|
|
12
|
+
* worker. A tool served from a different origin (e.g. a separate static
|
|
13
|
+
* host) will 404 on these imports.
|
|
14
|
+
* - `"patchwork:cross-origin"`: rewrite bare imports to a virtual module
|
|
15
|
+
* that resolves the URL *at runtime* via `getImportableUrlFromAutomergeUrl`
|
|
16
|
+
* and dynamically imports it, re-exporting the dep's named exports
|
|
17
|
+
* (top-level await). This resolves against the running document's origin
|
|
18
|
+
* instead of the module's, so it works even when the tool is loaded
|
|
19
|
+
* cross-origin from where the service worker lives. Requires a build
|
|
20
|
+
* target that supports top-level await (e.g. `es2022`/`esnext`).
|
|
21
|
+
* - `"bundle"`: include the dependency code in the bundle normally.
|
|
22
|
+
*/
|
|
23
|
+
automerge?: "patchwork" | "patchwork:cross-origin" | "bundle";
|
|
24
|
+
/**
|
|
25
|
+
* How to handle npm dependencies (deps that aren't `automerge:` URLs
|
|
26
|
+
* and aren't in the bootloader externals list).
|
|
27
|
+
* - `"bundle"` (default): bundle them normally.
|
|
28
|
+
* - `"esm.sh"`: rewrite to `https://esm.sh/<pkg>@<version>` external URLs.
|
|
29
|
+
*/
|
|
30
|
+
npm?: "esm.sh" | "bundle";
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface ResolvedOptions {
|
|
34
|
+
packageJsonPath: string;
|
|
35
|
+
rewrite: {
|
|
36
|
+
automerge: "patchwork" | "patchwork:cross-origin" | "bundle";
|
|
37
|
+
npm: "esm.sh" | "bundle";
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export declare function resolveOptions(raw?: PatchworkBundleOptions): ResolvedOptions;
|
|
41
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACrC,oFAAoF;IACpF,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,OAAO,CAAC,EAAE;QACR;;;;;;;;;;;;;;;;;WAiBG;QACH,SAAS,CAAC,EAAE,WAAW,GAAG,wBAAwB,GAAG,QAAQ,CAAC;QAE9D;;;;;WAKG;QACH,GAAG,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;KAC3B,CAAC;CACH;AAED,MAAM,WAAW,eAAe;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE;QACP,SAAS,EAAE,WAAW,GAAG,wBAAwB,GAAG,QAAQ,CAAC;QAC7D,GAAG,EAAE,QAAQ,GAAG,QAAQ,CAAC;KAC1B,CAAC;CACH;AAED,wBAAgB,cAAc,CAAC,GAAG,CAAC,EAAE,sBAAsB,GAAG,eAAe,CAQ5E"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function resolveOptions(raw) {
|
|
2
|
+
return {
|
|
3
|
+
packageJsonPath: raw?.packageJsonPath ?? "./package.json",
|
|
4
|
+
rewrite: {
|
|
5
|
+
automerge: raw?.rewrite?.automerge ?? "patchwork",
|
|
6
|
+
npm: raw?.rewrite?.npm ?? "bundle",
|
|
7
|
+
},
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA2CA,MAAM,UAAU,cAAc,CAAC,GAA4B;IACzD,OAAO;QACL,eAAe,EAAE,GAAG,EAAE,eAAe,IAAI,gBAAgB;QACzD,OAAO,EAAE;YACP,SAAS,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,IAAI,WAAW;YACjD,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,IAAI,QAAQ;SACnC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read and parse a package.json file.
|
|
3
|
+
*/
|
|
4
|
+
export declare function readPackageJson(path: string): Record<string, any>;
|
|
5
|
+
/**
|
|
6
|
+
* Parse a bare import specifier into package name and subpath.
|
|
7
|
+
*
|
|
8
|
+
* - `"abc"` → `{ pkgName: "abc", subpath: "." }`
|
|
9
|
+
* - `"abc/utils"` → `{ pkgName: "abc", subpath: "./utils" }`
|
|
10
|
+
* - `"@scope/pkg"` → `{ pkgName: "@scope/pkg", subpath: "." }`
|
|
11
|
+
* - `"@scope/pkg/utils"` → `{ pkgName: "@scope/pkg", subpath: "./utils" }`
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseBareSpecifier(id: string): {
|
|
14
|
+
pkgName: string;
|
|
15
|
+
subpath: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Returns true if the specifier is a bare import (not relative, absolute,
|
|
19
|
+
* or a protocol URL).
|
|
20
|
+
*/
|
|
21
|
+
export declare function isBareSpecifier(id: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve a dependency's entry point by reading its package.json from
|
|
24
|
+
* `node_modules/` and resolving through its `exports` field.
|
|
25
|
+
*
|
|
26
|
+
* Returns the resolved path relative to the package root (e.g. `"./dist/index.js"`),
|
|
27
|
+
* or the subpath itself if resolution fails.
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveDepEntryPoint(pkgName: string, subpath?: string, conditions?: string[]): string;
|
|
30
|
+
/**
|
|
31
|
+
* Enumerate the *named* exports of an ES module file, following relative
|
|
32
|
+
* `export * from "./x"` re-exports one dependency at a time. Used to generate a
|
|
33
|
+
* runtime-resolved virtual module that re-exports the same names as the real
|
|
34
|
+
* module. `"default"` is included in the returned set if the module has a
|
|
35
|
+
* default export.
|
|
36
|
+
*
|
|
37
|
+
* es-module-lexer must be initialised first (see {@link ensureLexer}).
|
|
38
|
+
*/
|
|
39
|
+
export declare function collectModuleExports(entryPath: string, seen?: Set<string>): Set<string>;
|
|
40
|
+
/** Initialise es-module-lexer's WASM once; safe to await repeatedly. */
|
|
41
|
+
export declare function ensureLexer(): Promise<void>;
|
|
42
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAEjE;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,GAAG;IAC9C,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB,CAuBA;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAOnD;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,MAAY,EACrB,UAAU,GAAE,MAAM,EAAuC,GACxD,MAAM,CAwBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,IAAI,GAAE,GAAG,CAAC,MAAM,CAAa,GAC5B,GAAG,CAAC,MAAM,CAAC,CAiCb;AAmBD,wEAAwE;AACxE,wBAAgB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3C"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
2
|
+
import { resolve as resolvePath, dirname } from "node:path";
|
|
3
|
+
import { resolve as resolveExports } from "resolve.exports";
|
|
4
|
+
import { init as initLexer, parse as parseModule } from "es-module-lexer";
|
|
5
|
+
/**
|
|
6
|
+
* Read and parse a package.json file.
|
|
7
|
+
*/
|
|
8
|
+
export function readPackageJson(path) {
|
|
9
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Parse a bare import specifier into package name and subpath.
|
|
13
|
+
*
|
|
14
|
+
* - `"abc"` → `{ pkgName: "abc", subpath: "." }`
|
|
15
|
+
* - `"abc/utils"` → `{ pkgName: "abc", subpath: "./utils" }`
|
|
16
|
+
* - `"@scope/pkg"` → `{ pkgName: "@scope/pkg", subpath: "." }`
|
|
17
|
+
* - `"@scope/pkg/utils"` → `{ pkgName: "@scope/pkg", subpath: "./utils" }`
|
|
18
|
+
*/
|
|
19
|
+
export function parseBareSpecifier(id) {
|
|
20
|
+
const firstSlash = id.indexOf("/");
|
|
21
|
+
const isScoped = id.startsWith("@");
|
|
22
|
+
if (isScoped) {
|
|
23
|
+
const secondSlash = id.indexOf("/", firstSlash + 1);
|
|
24
|
+
if (secondSlash === -1) {
|
|
25
|
+
return { pkgName: id, subpath: "." };
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
pkgName: id.slice(0, secondSlash),
|
|
29
|
+
subpath: "./" + id.slice(secondSlash + 1),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (firstSlash === -1) {
|
|
33
|
+
return { pkgName: id, subpath: "." };
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
pkgName: id.slice(0, firstSlash),
|
|
37
|
+
subpath: "./" + id.slice(firstSlash + 1),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns true if the specifier is a bare import (not relative, absolute,
|
|
42
|
+
* or a protocol URL).
|
|
43
|
+
*/
|
|
44
|
+
export function isBareSpecifier(id) {
|
|
45
|
+
return (id.length > 0 &&
|
|
46
|
+
!id.startsWith(".") &&
|
|
47
|
+
!id.startsWith("/") &&
|
|
48
|
+
!id.includes(":"));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Resolve a dependency's entry point by reading its package.json from
|
|
52
|
+
* `node_modules/` and resolving through its `exports` field.
|
|
53
|
+
*
|
|
54
|
+
* Returns the resolved path relative to the package root (e.g. `"./dist/index.js"`),
|
|
55
|
+
* or the subpath itself if resolution fails.
|
|
56
|
+
*/
|
|
57
|
+
export function resolveDepEntryPoint(pkgName, subpath = ".", conditions = ["patchwork", "browser", "import"]) {
|
|
58
|
+
try {
|
|
59
|
+
const depPkgJsonPath = resolvePath(process.cwd(), "node_modules", pkgName, "package.json");
|
|
60
|
+
const depPkgJson = readPackageJson(depPkgJsonPath);
|
|
61
|
+
const resolved = resolveExports(depPkgJson, subpath, { conditions });
|
|
62
|
+
if (resolved && resolved[0]) {
|
|
63
|
+
return resolved[0];
|
|
64
|
+
}
|
|
65
|
+
// Fallback to "main" for root export
|
|
66
|
+
if (subpath === "." && typeof depPkgJson.main === "string") {
|
|
67
|
+
return depPkgJson.main;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// If we can't read the dep's package.json, just pass through the subpath
|
|
72
|
+
}
|
|
73
|
+
return subpath;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Enumerate the *named* exports of an ES module file, following relative
|
|
77
|
+
* `export * from "./x"` re-exports one dependency at a time. Used to generate a
|
|
78
|
+
* runtime-resolved virtual module that re-exports the same names as the real
|
|
79
|
+
* module. `"default"` is included in the returned set if the module has a
|
|
80
|
+
* default export.
|
|
81
|
+
*
|
|
82
|
+
* es-module-lexer must be initialised first (see {@link ensureLexer}).
|
|
83
|
+
*/
|
|
84
|
+
export function collectModuleExports(entryPath, seen = new Set()) {
|
|
85
|
+
const names = new Set();
|
|
86
|
+
if (seen.has(entryPath) || !existsSync(entryPath))
|
|
87
|
+
return names;
|
|
88
|
+
seen.add(entryPath);
|
|
89
|
+
let source;
|
|
90
|
+
try {
|
|
91
|
+
source = readFileSync(entryPath, "utf-8");
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return names;
|
|
95
|
+
}
|
|
96
|
+
const [imports, exports] = parseModule(source);
|
|
97
|
+
for (const e of exports) {
|
|
98
|
+
if (e.n)
|
|
99
|
+
names.add(e.n);
|
|
100
|
+
}
|
|
101
|
+
// `export * from "./rel"` contributes names that aren't listed inline. Those
|
|
102
|
+
// re-exports show up in `imports`; detect them by the statement text and
|
|
103
|
+
// recurse into relative targets.
|
|
104
|
+
for (const imp of imports) {
|
|
105
|
+
const spec = imp.n;
|
|
106
|
+
if (!spec || !spec.startsWith("."))
|
|
107
|
+
continue;
|
|
108
|
+
const stmt = source.slice(imp.ss, imp.se);
|
|
109
|
+
if (!/^export\s*\*/.test(stmt))
|
|
110
|
+
continue;
|
|
111
|
+
const target = resolveRelativeModule(dirname(entryPath), spec);
|
|
112
|
+
if (!target)
|
|
113
|
+
continue;
|
|
114
|
+
for (const n of collectModuleExports(target, seen)) {
|
|
115
|
+
if (n !== "default")
|
|
116
|
+
names.add(n); // `export *` never re-exports default
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return names;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Resolve a relative module specifier to an existing file, trying the literal
|
|
123
|
+
* path and common JS extensions / index files.
|
|
124
|
+
*/
|
|
125
|
+
function resolveRelativeModule(fromDir, spec) {
|
|
126
|
+
const base = resolvePath(fromDir, spec);
|
|
127
|
+
const candidates = [
|
|
128
|
+
base,
|
|
129
|
+
`${base}.js`,
|
|
130
|
+
`${base}.mjs`,
|
|
131
|
+
resolvePath(base, "index.js"),
|
|
132
|
+
resolvePath(base, "index.mjs"),
|
|
133
|
+
];
|
|
134
|
+
return candidates.find((c) => existsSync(c)) ?? null;
|
|
135
|
+
}
|
|
136
|
+
let lexerReady;
|
|
137
|
+
/** Initialise es-module-lexer's WASM once; safe to await repeatedly. */
|
|
138
|
+
export function ensureLexer() {
|
|
139
|
+
return (lexerReady ??= initLexer);
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=utils.js.map
|