@distilled.cloud/cloudflare-rolldown-plugin 0.1.2 → 0.3.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/README.md +1 -1
- package/dist/plugins/nodejs-compat.d.ts +2 -2
- package/dist/plugins/nodejs-compat.d.ts.map +1 -1
- package/dist/plugins/nodejs-compat.js +36 -15
- package/dist/plugins/nodejs-compat.js.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -0
- package/dist/utils.js.map +1 -1
- package/package.json +6 -5
- package/src/plugins/nodejs-compat.ts +42 -21
- package/src/utils.ts +4 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Plugin } from "rolldown";
|
|
2
2
|
import type { CloudflarePluginOptions } from "../plugin.js";
|
|
3
|
-
export declare function makeNodejsCompatPlugin(options: CloudflarePluginOptions):
|
|
3
|
+
export declare function makeNodejsCompatPlugin(options: CloudflarePluginOptions): Plugin | Array<Plugin>;
|
|
4
4
|
//# sourceMappingURL=nodejs-compat.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodejs-compat.d.ts","sourceRoot":"","sources":["../../src/plugins/nodejs-compat.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"nodejs-compat.d.ts","sourceRoot":"","sources":["../../src/plugins/nodejs-compat.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGvC,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAM5D,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAQ/F"}
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { getCloudflarePreset, nonPrefixedNodeModules } from "@cloudflare/unenv-preset";
|
|
2
2
|
import assert from "node:assert";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
|
+
import path from "node:path";
|
|
4
5
|
import { esmExternalRequirePlugin } from "rolldown/plugins";
|
|
5
6
|
import { defineEnv } from "unenv";
|
|
6
|
-
import { hasNodejsCompat } from "../utils.js";
|
|
7
|
+
import { hasNodejsAls, hasNodejsCompat } from "../utils.js";
|
|
7
8
|
const NODE_BUILTIN_MODULES_REGEXP = new RegExp(`^(${nonPrefixedNodeModules.join("|")}|node:.+)$`);
|
|
8
9
|
const VIRTUAL_MODULE_ID_REGEXP = /^virtual:nodejs-global-inject\/.+$/;
|
|
9
10
|
export function makeNodejsCompatPlugin(options) {
|
|
10
11
|
if (hasNodejsCompat(options.compatibilityFlags)) {
|
|
11
12
|
return makeUnenvPlugin(options);
|
|
12
13
|
}
|
|
14
|
+
if (hasNodejsAls(options.compatibilityFlags)) {
|
|
15
|
+
return makeNodeJsAlsPlugin();
|
|
16
|
+
}
|
|
13
17
|
return makeNodeJsImportWarningPlugin();
|
|
14
18
|
}
|
|
15
19
|
function makeUnenvPlugin(options) {
|
|
@@ -89,23 +93,37 @@ function makeUnenvPlugin(options) {
|
|
|
89
93
|
},
|
|
90
94
|
];
|
|
91
95
|
}
|
|
96
|
+
function makeNodeJsAlsPlugin() {
|
|
97
|
+
return {
|
|
98
|
+
name: "rolldown-plugin-cloudflare:nodejs-als",
|
|
99
|
+
resolveId: {
|
|
100
|
+
filter: { id: /^(node:)?async_hooks$/ },
|
|
101
|
+
handler(id) {
|
|
102
|
+
return { id, external: true };
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
92
107
|
function makeNodeJsImportWarningPlugin() {
|
|
93
108
|
const imports = new Map();
|
|
109
|
+
let root = process.cwd();
|
|
94
110
|
return {
|
|
95
|
-
name: "rolldown-plugin-cloudflare:nodejs-
|
|
111
|
+
name: "rolldown-plugin-cloudflare:nodejs-import-warnings",
|
|
112
|
+
options(options) {
|
|
113
|
+
if (options.cwd) {
|
|
114
|
+
root = options.cwd;
|
|
115
|
+
}
|
|
116
|
+
},
|
|
96
117
|
resolveId: {
|
|
97
118
|
filter: { id: NODE_BUILTIN_MODULES_REGEXP },
|
|
98
|
-
async handler(id, importer
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (!imports.has(id)) {
|
|
103
|
-
imports.set(id, new Set());
|
|
104
|
-
}
|
|
105
|
-
imports.get(id)?.add(importer);
|
|
119
|
+
async handler(id, importer) {
|
|
120
|
+
if (importer) {
|
|
121
|
+
if (!imports.has(id)) {
|
|
122
|
+
imports.set(id, new Set());
|
|
106
123
|
}
|
|
107
|
-
|
|
124
|
+
imports.get(id)?.add(importer);
|
|
108
125
|
}
|
|
126
|
+
return { id, external: true };
|
|
109
127
|
},
|
|
110
128
|
},
|
|
111
129
|
buildStart() {
|
|
@@ -113,12 +131,15 @@ function makeNodeJsImportWarningPlugin() {
|
|
|
113
131
|
},
|
|
114
132
|
buildEnd() {
|
|
115
133
|
if (imports.size > 0) {
|
|
134
|
+
let message = `Unexpected Node.js imports. ` +
|
|
135
|
+
`Do you need to enable the "nodejs_compat" compatibility flag? ` +
|
|
136
|
+
"Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details.\n";
|
|
116
137
|
for (const [id, importers] of imports.entries()) {
|
|
117
|
-
|
|
118
|
-
`
|
|
119
|
-
|
|
120
|
-
].join("\n"));
|
|
138
|
+
for (const importer of importers) {
|
|
139
|
+
message += ` - "${id}" imported from "${path.relative(root, importer)}"\n`;
|
|
140
|
+
}
|
|
121
141
|
}
|
|
142
|
+
this.warn(message);
|
|
122
143
|
}
|
|
123
144
|
},
|
|
124
145
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodejs-compat.js","sourceRoot":"","sources":["../../src/plugins/nodejs-compat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM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C,OAAO,CAAC,CAAC;YACrB,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,wBAAwB,CAC/B,MAAgE;IAEhE,MAAM,cAAc,GAAG,IAAI,GAAG,EAAkB,CAAC;IACjD,KAAK,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACrE,wFAAwF;QACxF,6DAA6D;QAC7D,MAAM,CAAC,OAAO,eAAe,KAAK,QAAQ,EAAE,yCAAyC,CAAC,CAAC;QACvF,cAAc,CAAC,GAAG,CAChB,qBAAqB,CAAC,YAAY,CAAC,EACnC,8BAA8B,eAAe,iBAAiB,YAAY,mBAAmB,CAC9F,CAAC;IACJ,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,SAAS,qBAAqB,CAAC,MAAc;IAC3C,OAAO,gCAAgC,MAAM,EAAW,CAAC;AAC3D,CAAC"}
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,OAAO,CAEtE"}
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,OAAO,CAEtE;AAED,wBAAgB,YAAY,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,OAAO,CAEnE"}
|
package/dist/utils.js
CHANGED
|
@@ -5,4 +5,7 @@ export function sanitizePath(path) {
|
|
|
5
5
|
export function hasNodejsCompat(flags) {
|
|
6
6
|
return flags?.some((flag) => flag === "nodejs_compat" || flag === "nodejs_compat_v2") ?? false;
|
|
7
7
|
}
|
|
8
|
+
export function hasNodejsAls(flags) {
|
|
9
|
+
return flags?.some((flag) => flag === "nodejs_als") ?? false;
|
|
10
|
+
}
|
|
8
11
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,aAAa,GAAG,SAAS,CAAC;AAEhC,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAA6B;IAC3D,OAAO,KAAK,EAAE,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,eAAe,IAAI,IAAI,KAAK,kBAAkB,CAAC,IAAI,KAAK,CAAC;AACjG,CAAC"}
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,aAAa,GAAG,SAAS,CAAC;AAEhC,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,OAAO,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAA6B;IAC3D,OAAO,KAAK,EAAE,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,eAAe,IAAI,IAAI,KAAK,kBAAkB,CAAC,IAAI,KAAK,CAAC;AACjG,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,KAA6B;IACxD,OAAO,KAAK,EAAE,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,YAAY,CAAC,IAAI,KAAK,CAAC;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@distilled.cloud/cloudflare-rolldown-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Rolldown plugin for Cloudflare Workers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
7
7
|
"rolldown",
|
|
8
8
|
"workers"
|
|
9
9
|
],
|
|
10
|
-
"homepage": "https://github.com/alchemy-run/cloudflare-tools/tree/main/packages/rolldown-plugin
|
|
10
|
+
"homepage": "https://github.com/alchemy-run/cloudflare-tools/tree/main/packages/cloudflare-rolldown-plugin#readme",
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/alchemy-run/cloudflare-tools/issues"
|
|
13
13
|
},
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsc",
|
|
45
45
|
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
46
|
+
"dev": "tsc --watch",
|
|
46
47
|
"test": "vitest run",
|
|
47
48
|
"test:watch": "vitest",
|
|
48
49
|
"typecheck": "tsc --noEmit",
|
|
@@ -53,9 +54,9 @@
|
|
|
53
54
|
"unenv": "^2.0.0-rc.24"
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|
|
56
|
-
"@cloudflare/workers-types": "
|
|
57
|
-
"
|
|
58
|
-
"vitest": "
|
|
57
|
+
"@cloudflare/workers-types": "^4.20260310.1",
|
|
58
|
+
"@distilled.cloud/test-utils": "0.0.0",
|
|
59
|
+
"vitest": "^4.1.1"
|
|
59
60
|
},
|
|
60
61
|
"peerDependencies": {
|
|
61
62
|
"rolldown": "^1.0.0-rc.9"
|
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import { getCloudflarePreset, nonPrefixedNodeModules } from "@cloudflare/unenv-preset";
|
|
2
2
|
import assert from "node:assert";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
|
-
import
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import type { Plugin } from "rolldown";
|
|
5
6
|
import { esmExternalRequirePlugin } from "rolldown/plugins";
|
|
6
7
|
import { defineEnv } from "unenv";
|
|
7
8
|
import type { CloudflarePluginOptions } from "../plugin.js";
|
|
8
|
-
import { hasNodejsCompat } from "../utils.js";
|
|
9
|
+
import { hasNodejsAls, hasNodejsCompat } from "../utils.js";
|
|
9
10
|
|
|
10
11
|
const NODE_BUILTIN_MODULES_REGEXP = new RegExp(`^(${nonPrefixedNodeModules.join("|")}|node:.+)$`);
|
|
11
12
|
const VIRTUAL_MODULE_ID_REGEXP = /^virtual:nodejs-global-inject\/.+$/;
|
|
12
13
|
|
|
13
|
-
export function makeNodejsCompatPlugin(options: CloudflarePluginOptions):
|
|
14
|
+
export function makeNodejsCompatPlugin(options: CloudflarePluginOptions): Plugin | Array<Plugin> {
|
|
14
15
|
if (hasNodejsCompat(options.compatibilityFlags)) {
|
|
15
16
|
return makeUnenvPlugin(options);
|
|
16
17
|
}
|
|
18
|
+
if (hasNodejsAls(options.compatibilityFlags)) {
|
|
19
|
+
return makeNodeJsAlsPlugin();
|
|
20
|
+
}
|
|
17
21
|
return makeNodeJsImportWarningPlugin();
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
function makeUnenvPlugin(options: CloudflarePluginOptions):
|
|
24
|
+
function makeUnenvPlugin(options: CloudflarePluginOptions): Array<Plugin> {
|
|
21
25
|
const { alias, inject, external, polyfill } = defineEnv({
|
|
22
26
|
presets: [
|
|
23
27
|
getCloudflarePreset({
|
|
@@ -96,23 +100,38 @@ function makeUnenvPlugin(options: CloudflarePluginOptions): RolldownPluginOption
|
|
|
96
100
|
];
|
|
97
101
|
}
|
|
98
102
|
|
|
99
|
-
function
|
|
103
|
+
function makeNodeJsAlsPlugin(): Plugin {
|
|
104
|
+
return {
|
|
105
|
+
name: "rolldown-plugin-cloudflare:nodejs-als",
|
|
106
|
+
resolveId: {
|
|
107
|
+
filter: { id: /^(node:)?async_hooks$/ },
|
|
108
|
+
handler(id) {
|
|
109
|
+
return { id, external: true };
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function makeNodeJsImportWarningPlugin(): Plugin {
|
|
100
116
|
const imports = new Map<string, Set<string>>();
|
|
117
|
+
let root = process.cwd();
|
|
101
118
|
return {
|
|
102
|
-
name: "rolldown-plugin-cloudflare:nodejs-
|
|
119
|
+
name: "rolldown-plugin-cloudflare:nodejs-import-warnings",
|
|
120
|
+
options(options) {
|
|
121
|
+
if (options.cwd) {
|
|
122
|
+
root = options.cwd;
|
|
123
|
+
}
|
|
124
|
+
},
|
|
103
125
|
resolveId: {
|
|
104
126
|
filter: { id: NODE_BUILTIN_MODULES_REGEXP },
|
|
105
|
-
async handler(id, importer
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (!imports.has(id)) {
|
|
110
|
-
imports.set(id, new Set());
|
|
111
|
-
}
|
|
112
|
-
imports.get(id)?.add(importer);
|
|
127
|
+
async handler(id, importer) {
|
|
128
|
+
if (importer) {
|
|
129
|
+
if (!imports.has(id)) {
|
|
130
|
+
imports.set(id, new Set());
|
|
113
131
|
}
|
|
114
|
-
|
|
132
|
+
imports.get(id)?.add(importer);
|
|
115
133
|
}
|
|
134
|
+
return { id, external: true };
|
|
116
135
|
},
|
|
117
136
|
},
|
|
118
137
|
buildStart() {
|
|
@@ -120,14 +139,16 @@ function makeNodeJsImportWarningPlugin(): RolldownPluginOption {
|
|
|
120
139
|
},
|
|
121
140
|
buildEnd() {
|
|
122
141
|
if (imports.size > 0) {
|
|
142
|
+
let message =
|
|
143
|
+
`Unexpected Node.js imports. ` +
|
|
144
|
+
`Do you need to enable the "nodejs_compat" compatibility flag? ` +
|
|
145
|
+
"Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details.\n";
|
|
123
146
|
for (const [id, importers] of imports.entries()) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
...Array.from(importers).map((importer) => `- ${importer}`),
|
|
128
|
-
].join("\n"),
|
|
129
|
-
);
|
|
147
|
+
for (const importer of importers) {
|
|
148
|
+
message += ` - "${id}" imported from "${path.relative(root, importer)}"\n`;
|
|
149
|
+
}
|
|
130
150
|
}
|
|
151
|
+
this.warn(message);
|
|
131
152
|
}
|
|
132
153
|
},
|
|
133
154
|
};
|
package/src/utils.ts
CHANGED
|
@@ -7,3 +7,7 @@ export function sanitizePath(path: string): string {
|
|
|
7
7
|
export function hasNodejsCompat(flags?: ReadonlyArray<string>): boolean {
|
|
8
8
|
return flags?.some((flag) => flag === "nodejs_compat" || flag === "nodejs_compat_v2") ?? false;
|
|
9
9
|
}
|
|
10
|
+
|
|
11
|
+
export function hasNodejsAls(flags?: ReadonlyArray<string>): boolean {
|
|
12
|
+
return flags?.some((flag) => flag === "nodejs_als") ?? false;
|
|
13
|
+
}
|