@distilled.cloud/cloudflare-rolldown-plugin 0.3.0 → 0.4.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/factory.d.ts +19 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +30 -0
- package/dist/factory.js.map +1 -0
- package/dist/options.d.ts +26 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +1 -0
- package/dist/options.js.map +1 -0
- package/dist/plugin.d.ts +3 -6
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +10 -11
- package/dist/plugin.js.map +1 -1
- package/dist/plugins/additional-modules.d.ts +1 -2
- package/dist/plugins/additional-modules.d.ts.map +1 -1
- package/dist/plugins/additional-modules.js +67 -53
- package/dist/plugins/additional-modules.js.map +1 -1
- package/dist/plugins/cloudflare-externals.d.ts +1 -2
- package/dist/plugins/cloudflare-externals.d.ts.map +1 -1
- package/dist/plugins/cloudflare-externals.js +36 -15
- package/dist/plugins/cloudflare-externals.js.map +1 -1
- package/dist/plugins/index.d.ts +7 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +7 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/nodejs-compat.d.ts +9 -3
- package/dist/plugins/nodejs-compat.d.ts.map +1 -1
- package/dist/plugins/nodejs-compat.js +178 -115
- package/dist/plugins/nodejs-compat.js.map +1 -1
- package/dist/plugins/options.d.ts +1 -5
- package/dist/plugins/options.d.ts.map +1 -1
- package/dist/plugins/options.js +128 -23
- package/dist/plugins/options.js.map +1 -1
- package/dist/plugins/virtual-modules.d.ts +3 -0
- package/dist/plugins/virtual-modules.d.ts.map +1 -0
- package/dist/plugins/virtual-modules.js +125 -0
- package/dist/plugins/virtual-modules.js.map +1 -0
- package/dist/plugins/wasm-init.d.ts +1 -2
- package/dist/plugins/wasm-init.d.ts.map +1 -1
- package/dist/plugins/wasm-init.js +15 -13
- package/dist/plugins/wasm-init.js.map +1 -1
- package/package.json +16 -1
- package/src/factory.ts +60 -0
- package/src/options.ts +25 -0
- package/src/plugin.ts +22 -18
- package/src/plugins/additional-modules.ts +89 -55
- package/src/plugins/cloudflare-externals.ts +36 -16
- package/src/plugins/index.ts +6 -0
- package/src/plugins/nodejs-compat.ts +197 -140
- package/src/plugins/options.ts +145 -26
- package/src/plugins/virtual-modules.ts +135 -0
- package/src/plugins/wasm-init.ts +15 -14
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Plugin } from "rolldown";
|
|
2
|
+
import { createPlugin } from "../factory.js";
|
|
3
|
+
import type { UnenvApi } from "./nodejs-compat.js";
|
|
4
|
+
|
|
5
|
+
// oxlint-disable-next-line no-control-regex
|
|
6
|
+
const VIRTUAL_MODULE_REGEXP = /^\0distilled:.*$/;
|
|
7
|
+
|
|
8
|
+
export const WORKER_ENTRY_PREFIX = "\0distilled:worker-entry:" as const;
|
|
9
|
+
const USER_ENTRY_PREFIX = "\0distilled:user-entry:" as const;
|
|
10
|
+
const PEAR_ENTRY_PREFIX = "\0distilled:pear-entry:" as const;
|
|
11
|
+
const INJECT_PREFIX = "\0distilled:inject:" as const;
|
|
12
|
+
const EXPORT_TYPES_ID = "\0distilled:export-types" as const;
|
|
13
|
+
|
|
14
|
+
export const virtualModulesPlugin = createPlugin("virtual-modules", (options) => {
|
|
15
|
+
let unenvApi: UnenvApi | undefined;
|
|
16
|
+
const inject = () => {
|
|
17
|
+
if (!unenvApi) return [];
|
|
18
|
+
return [
|
|
19
|
+
...unenvApi.polyfill.map((module) => `import "${module}";`),
|
|
20
|
+
...Object.keys(unenvApi.inject).map(
|
|
21
|
+
(injectedName) => `import "${INJECT_PREFIX}${injectedName}";`,
|
|
22
|
+
),
|
|
23
|
+
];
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
shared: {
|
|
27
|
+
buildStart({ plugins }) {
|
|
28
|
+
unenvApi = plugins.find(
|
|
29
|
+
(plugin): plugin is Plugin<UnenvApi> =>
|
|
30
|
+
"name" in plugin && plugin.name === "distilled-cloudflare:nodejs-unenv",
|
|
31
|
+
)?.api;
|
|
32
|
+
},
|
|
33
|
+
resolveId: {
|
|
34
|
+
filter: { id: VIRTUAL_MODULE_REGEXP },
|
|
35
|
+
handler(id) {
|
|
36
|
+
if (
|
|
37
|
+
id.startsWith(WORKER_ENTRY_PREFIX) ||
|
|
38
|
+
id.startsWith(PEAR_ENTRY_PREFIX) ||
|
|
39
|
+
id.startsWith(INJECT_PREFIX) ||
|
|
40
|
+
id === EXPORT_TYPES_ID
|
|
41
|
+
) {
|
|
42
|
+
return { id };
|
|
43
|
+
}
|
|
44
|
+
if (id.startsWith(USER_ENTRY_PREFIX)) {
|
|
45
|
+
return this.resolve(id.slice(USER_ENTRY_PREFIX.length), undefined, {
|
|
46
|
+
isEntry: true,
|
|
47
|
+
kind: "import-statement",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
load: {
|
|
53
|
+
filter: { id: VIRTUAL_MODULE_REGEXP },
|
|
54
|
+
handler(id) {
|
|
55
|
+
if (id.startsWith(WORKER_ENTRY_PREFIX)) {
|
|
56
|
+
const userEntryId = id.replace(WORKER_ENTRY_PREFIX, USER_ENTRY_PREFIX);
|
|
57
|
+
return [
|
|
58
|
+
...inject(),
|
|
59
|
+
`import { getExportTypes } from "${EXPORT_TYPES_ID}";`,
|
|
60
|
+
...(options.exports
|
|
61
|
+
? [`export { ${options.exports.join(", ")} } from "${userEntryId}";`]
|
|
62
|
+
: [
|
|
63
|
+
`import * as userEntry from "${userEntryId}";`,
|
|
64
|
+
`export * from "${userEntryId}";`,
|
|
65
|
+
`export default userEntry.default ?? {};`,
|
|
66
|
+
]),
|
|
67
|
+
"if (import.meta.hot) {",
|
|
68
|
+
" import.meta.hot.accept((module) => {",
|
|
69
|
+
" const exportTypes = getExportTypes(module);",
|
|
70
|
+
' import.meta.hot.send("distilled-cloudflare:worker-export-types", exportTypes);',
|
|
71
|
+
" });",
|
|
72
|
+
"}",
|
|
73
|
+
].join("\n");
|
|
74
|
+
}
|
|
75
|
+
if (id === EXPORT_TYPES_ID) {
|
|
76
|
+
return `
|
|
77
|
+
import {
|
|
78
|
+
WorkerEntrypoint,
|
|
79
|
+
DurableObject,
|
|
80
|
+
WorkflowEntrypoint,
|
|
81
|
+
} from "cloudflare:workers";
|
|
82
|
+
|
|
83
|
+
const baseClasses = new Map([
|
|
84
|
+
["WorkerEntrypoint", WorkerEntrypoint],
|
|
85
|
+
["DurableObject", DurableObject],
|
|
86
|
+
["WorkflowEntrypoint", WorkflowEntrypoint],
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
export function getExportTypes(module) {
|
|
90
|
+
const exportTypes = {};
|
|
91
|
+
|
|
92
|
+
for (const [key, value] of Object.entries(module)) {
|
|
93
|
+
if (key === "default") {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let exportType;
|
|
98
|
+
|
|
99
|
+
if (typeof value === "function") {
|
|
100
|
+
for (const [type, baseClass] of baseClasses) {
|
|
101
|
+
if (baseClass.prototype.isPrototypeOf(value.prototype)) {
|
|
102
|
+
exportType = type;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!exportType) {
|
|
108
|
+
exportType = "DurableObject";
|
|
109
|
+
}
|
|
110
|
+
} else if (typeof value === "object" && value !== null) {
|
|
111
|
+
exportType = "WorkerEntrypoint";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
exportTypes[key] = exportType;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return exportTypes;
|
|
118
|
+
}`;
|
|
119
|
+
}
|
|
120
|
+
if (id.startsWith(INJECT_PREFIX)) {
|
|
121
|
+
const injectedName = id.slice(INJECT_PREFIX.length);
|
|
122
|
+
const moduleSpecifier = unenvApi?.inject[injectedName];
|
|
123
|
+
if (!moduleSpecifier) {
|
|
124
|
+
throw new Error(`Expected module specifier for "${injectedName}" to be defined`);
|
|
125
|
+
}
|
|
126
|
+
return [
|
|
127
|
+
`import ${injectedName} from "${moduleSpecifier}";`,
|
|
128
|
+
`globalThis.${injectedName} = ${injectedName};`,
|
|
129
|
+
].join("\n");
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
});
|
package/src/plugins/wasm-init.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createPlugin } from "../factory.js";
|
|
2
2
|
import { sanitizePath } from "../utils.js";
|
|
3
3
|
|
|
4
4
|
const WASM_INIT_QUERY = /\.wasm\?init$/;
|
|
5
5
|
|
|
6
|
-
export const wasmInitPlugin
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
6
|
+
export const wasmInitPlugin = createPlugin("wasm-init", () => ({
|
|
7
|
+
shared: {
|
|
8
|
+
load: {
|
|
9
|
+
filter: { id: WASM_INIT_QUERY },
|
|
10
|
+
handler(id) {
|
|
11
|
+
return [
|
|
12
|
+
`import wasmModule from "${sanitizePath(id)}";`,
|
|
13
|
+
`export default async (imports) => {`,
|
|
14
|
+
` const result = await WebAssembly.instantiate(wasmModule, imports);`,
|
|
15
|
+
` return "instance" in result ? result.instance : result;`,
|
|
16
|
+
`};`,
|
|
17
|
+
].join("\n");
|
|
18
|
+
},
|
|
18
19
|
},
|
|
19
20
|
},
|
|
20
|
-
};
|
|
21
|
+
}));
|