Skip to content

Commit dbbeb23

Browse files
authored
Support Text and Data module types (#8387)
1 parent 62d5471 commit dbbeb23

29 files changed

+252
-144
lines changed

.changeset/silver-rooms-hear.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Support Text and Data module types.
6+
Text modules can be imported with a `.txt` or `.html` extension while Data modules can be imported with a `.bin` extension.
7+
This expands on the existing support for WebAssembly modules, which can now be imported with `.wasm` or `.wasm?module` extensions.
8+
Custom rules are not supported.
9+
More info on including non-JavaScript modules can be found [here](https://developers.cloudflare.com/workers/wrangler/bundling/#including-non-javascript-modules).
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Additional Modules
2+
3+
The modules directory contains the following example files:
4+
5+
- `bin-example.bin` - a TTF file for the Inter font with the extension changed to `.bin`
6+
- `html-example.html` - a Hello World HTML document
7+
- `text-example.txt` - a simple text file
8+
- `wasm-example.wasm` - a compiled WebAssembly file that exports an `add` function
9+
10+
The WebAssembly is generated from the following C code:
11+
12+
```c
13+
#include <stdint.h>
14+
15+
int32_t add(int32_t a, int32_t b) {
16+
return a + b;
17+
}
18+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect, test } from "vitest";
2+
import {
3+
getJsonResponse,
4+
getTextResponse,
5+
page,
6+
viteTestUrl,
7+
} from "../../__test-utils__";
8+
9+
test("supports Data modules with a '.bin' extension", async () => {
10+
const result = await getJsonResponse("/bin");
11+
expect(result).toEqual({ byteLength: 342936 });
12+
});
13+
14+
test("supports Text modules with a '.html' extension", async () => {
15+
await page.goto(`${viteTestUrl}/html`);
16+
const content = await page.textContent("h1");
17+
expect(content).toBe("Hello world");
18+
});
19+
20+
test("supports Text modules with a '.txt' extension", async () => {
21+
const result = await getTextResponse("/text");
22+
expect(result).toBe("Example text content.\n");
23+
});
24+
25+
test("supports CompiledWasm modules with a '.wasm' extension", async () => {
26+
const result = await getJsonResponse("/wasm");
27+
expect(result).toEqual({ result: 7 });
28+
});
29+
30+
test("supports CompiledWasm modules with a '.wasm?module' extension", async () => {
31+
const result = await getJsonResponse("/wasm-with-param");
32+
expect(result).toEqual({ result: 11 });
33+
});

packages/vite-plugin-cloudflare/playground/wasm/package.json renamed to packages/vite-plugin-cloudflare/playground/additional-modules/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@playground/wasm",
2+
"name": "@playground/additional-modules",
33
"private": true,
44
"type": "module",
55
"scripts": {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import bin from "./modules/bin-example.bin";
2+
import html from "./modules/html-example.html";
3+
import text from "./modules/text-example.txt";
4+
import wasm from "./modules/wasm-example.wasm";
5+
import wasmWithParam from "./modules/wasm-example.wasm?module";
6+
7+
export default {
8+
async fetch(request) {
9+
const url = new URL(request.url);
10+
11+
switch (url.pathname) {
12+
case "/bin": {
13+
return Response.json({ byteLength: bin.byteLength });
14+
}
15+
case "/html": {
16+
return new Response(html, { headers: { "Content-Type": "text/html" } });
17+
}
18+
case "/text": {
19+
return new Response(text, {
20+
headers: { "Content-Type": "text/plain" },
21+
});
22+
}
23+
case "/wasm": {
24+
const instance = await WebAssembly.instantiate(wasm);
25+
const result = instance.exports.add(3, 4);
26+
27+
return Response.json({ result });
28+
}
29+
case "/wasm-with-param": {
30+
const instance = await WebAssembly.instantiate(wasmWithParam);
31+
const result = instance.exports.add(5, 6);
32+
33+
return Response.json({ result });
34+
}
35+
default: {
36+
return new Response(null, { status: 404 });
37+
}
38+
}
39+
},
40+
} satisfies ExportedHandler;
335 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<h1>Hello world</h1>
5+
</body>
6+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example text content.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)