Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use with wrangler by cloudflare #156

Closed
damianslayer opened this issue Sep 12, 2023 · 3 comments · May be fixed by #187
Closed

How to use with wrangler by cloudflare #156

damianslayer opened this issue Sep 12, 2023 · 3 comments · May be fixed by #187
Labels
question Further information is requested

Comments

@damianslayer
Copy link

How to use with wrangler ?

@wilsonzlin
Copy link
Owner

AFAIK Wrangler is a Cloudflare CLI for Workers. Can you clarify what you are looking to do? If you want to use this library from a Worker, I believe you'll need the WASM variant, as I don't think the Workers runtime supports native Node.js modules.

@wilsonzlin wilsonzlin added the question Further information is requested label Dec 22, 2023
@rmarscher
Copy link

rmarscher commented Apr 29, 2024

I was able to get @minify-html/wasm working on Cloudflare's workerd runtime (using wrangler to run it locally).

The index.js created by wasm-bindgen needs to be patched. I edited node_modules/@minify-html/wasm/index.js to be this:

import * as imports from "./index_bg.js"

// switch between both syntax for node and for workerd
import wkmod from "./index_bg.wasm"
import * as wasm from "./index_bg.wasm"
if (typeof process !== "undefined" && process.release.name === "node") {
  imports.__wbg_set_wasm(wasm)
} else {
  const instance = new WebAssembly.Instance(wkmod, { "./index_bg.js": imports })
  imports.__wbg_set_wasm(instance.exports)
}

export * from "./index_bg.js"

And then I could import and use the minify function normally from my project.

import { minify } from "@minify-html/wasm"

const html = "<p>\n    Some HTML\n</p>\n\n\n"
const encoder = new TextEncoder()
const decoder = new TextDecoder()

decoder.decode(
  minify(encoder.encode(html), {
    keep_spaces_between_attributes: true,
    keep_comments: true,
  }),
)

This section of the cloudflare docs mentions that workaround. https://developers.cloudflare.com/workers/languages/rust/#javascript-plumbing-wasm-bindgen

@damianslayer
Copy link
Author

I was able to get @minify-html/wasm working on Cloudflare's workerd runtime (using wrangler to run it locally).

The index.js created by wasm-bindgen needs to be patched. I edited node_modules/@minify-html/wasm/index.js to be this:

import * as imports from "./index_bg.js"

// switch between both syntax for node and for workerd
import wkmod from "./index_bg.wasm"
import * as wasm from "./index_bg.wasm"
if (typeof process !== "undefined" && process.release.name === "node") {
  imports.__wbg_set_wasm(wasm)
} else {
  const instance = new WebAssembly.Instance(wkmod, { "./index_bg.js": imports })
  imports.__wbg_set_wasm(instance.exports)
}

export * from "./index_bg.js"

And then I could import and use the minify function normally from my project.

import { minify } from "@minify-html/wasm"

const html = "<p>\n    Some HTML\n</p>\n\n\n"
const encoder = new TextEncoder()
const decoder = new TextDecoder()

decoder.decode(
  minify(encoder.encode(html), {
    keep_spaces_between_attributes: true,
    keep_comments: true,
  }),
)

This section of the cloudflare docs mentions that workaround. https://developers.cloudflare.com/workers/languages/rust/#javascript-plumbing-wasm-bindgen

You may create PR for that, so we can use the wasm variant directly for workers without patch first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants