Skip to content

Commit

Permalink
text_blobs/Text module support for service worker format in local mode
Browse files Browse the repository at this point in the history
This adds support for `text_blobs`/Text module support in local mode. Now that cloudflare/miniflare#228 has landed in miniflare (thanks @caass!), we can use that in wrangler as well.

Fixes #416
  • Loading branch information
threepointone committed Mar 31, 2022
1 parent 942e9f1 commit b7e3352
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasWarnings()).toBe(false);
});

it("should error on invalid `wasm_module` paths", () => {
it("should error on invalid `wasm_modules` paths", () => {
const expectedConfig = {
wasm_modules: {
MODULE_1: 111,
Expand Down
10 changes: 10 additions & 0 deletions packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function useLocalWorker({
const scriptPath = realpathSync(bundle.path);

const wasmBindings = { ...bindings.wasm_modules };
const textBlobBindings = { ...bindings.text_blobs };
if (format === "service-worker") {
for (const { type, name } of bundle.modules) {
if (type === "compiled-wasm") {
Expand All @@ -91,6 +92,13 @@ function useLocalWorker({
// characters with an underscore.
const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
wasmBindings[identifier] = name;
} else if (type === "text") {
// In service-worker format, text modules are referenced by global identifiers,
// so we convert it here.
// This identifier has to be a valid JS identifier, so we replace all non alphanumeric
// characters with an underscore.
const identifier = name.replace(/[^a-zA-Z0-9_$]/g, "_");
textBlobBindings[identifier] = name;
}
}
}
Expand Down Expand Up @@ -130,6 +138,7 @@ function useLocalWorker({
: undefined,
bindings: bindings.vars,
wasmBindings,
textBlobBindings,
sourceMap: true,
logUnhandledRejections: true,
};
Expand Down Expand Up @@ -227,6 +236,7 @@ function useLocalWorker({
publicDirectory,
rules,
bindings.wasm_modules,
bindings.text_blobs,
]);
return { inspectorUrl };
}
2 changes: 1 addition & 1 deletion packages/wrangler/src/module-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default function createModuleCollector(props: {
modules.splice(0);
});

// ~ start legacy module specifier support ~
// ~ start legacy module specifier support ~

// This section detects usage of "legacy" 1.x style module specifiers
// and modifies them so they "work" in wrangler v2, but with a warning
Expand Down

0 comments on commit b7e3352

Please sign in to comment.