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

Make wasm plugin work with service workers #334

Merged
merged 3 commits into from
Apr 26, 2020
Merged

Make wasm plugin work with service workers #334

merged 3 commits into from
Apr 26, 2020

Conversation

vedantroy
Copy link
Contributor

@vedantroy vedantroy commented Apr 25, 2020

The code generated by @rollup/plugin-wasm breaks when executed in a
service worker because window is undefined.

Rollup Plugin Name: wasm

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • [] no

This is a draft PR because I'm not sure how to write a test that runs code inside a service worker. Once I figure that out, I will add a test and update the above checkbox.

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers: #333

Description

This PR adds a check for whether window is undefined. This PR makes the loader function use globalThis instead of window. Prior to this, @rollup/plugin-wasm would break when the resulting generated code was run in a service worker, because it assumed window was defined.

The code generated by @rollup/plugin-wasm breaks when executed in a
service worker because window is undefined.
@shellscape
Copy link
Collaborator

Thanks for taking this on. We'll leave this open and pending until you give the go-ahead to review.

Instead of checking whether window is defined, we can just use
globalThis.
@vedantroy
Copy link
Contributor Author

vedantroy commented Apr 25, 2020

@shellscape I added a test and refactored test.js because I needed to pass the code generated by rollup into a Worker instead of testBundle.

Note: The test is a bit fragile because I have to fake the browser environment (atob does not exist on globalThis in node, so I polyfilled it, and I undefined process to force the wasm loader to go into the browser section). However, I think it's the most reasonable test that is possible.

The test can only run on node versions where worker_threads is available (node > 11.7.0), so I skip the test on earlier versions of node.

Edit: I see the CI is failing for Node version 10 because worker_threads is not a module. I can fix this by installing the threads library (https://www.npmjs.com/package/threads) as a dev dependency, so the tests can pass on Node 10. But confirming this ok, before I do it because I don't know if adding a dependency (even if it is a dev dependency) is a good idea.

An alternative is to just merge the PR without a test? because the change is very trivial (replacing window with globalThis).

Edit 2: Seems like the threads package is not a drop in replacement for worker_threads in Node, so this will be more complicated than I thought.

@shellscape
Copy link
Collaborator

Thanks for the PR!

@ColinEberhardt and @jamen would you two be willing to take a look at this one?

Copy link

@jamen jamen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The worker test does seem a bit hacky, but I don't have a better idea, so its probably fine. The change affecting the other tests is functionally the same, so that's fine.

It should probably be added in a major version because there is a gap of support between WebAssembly and globalThis according to caniuse (1, 2).

Looks okay to me. 👍

@vedantroy
Copy link
Contributor Author

@jamen The gap of support seems to show that WebAssembly is less supported than globalThis in all cases (except for Opera Mobile, which supports wasm but not globalThis). However, if this is an issue, I can add a check so it tries using window before globalThis.

@jamen
Copy link

jamen commented Apr 26, 2020

What I mean is the browser versions. WebAssembly came first and globalThis came later, so there are versions where WebAssembly exists but globalThis doesn't. I believe that is a breaking change, because the build would be supported in a more limited range of versions than WebAssembly itself.

I don't think it needs changed, but it should be in a major version so it wouldn't surprise existing builds. I haven't been very active in web lately though, so someone else can make that call. But also, a build concerned about this should be using something like @babel/preset-env anyways.

@shellscape
Copy link
Collaborator

Thanks everyone!

@shellscape shellscape merged commit 59f984d into rollup:master Apr 26, 2020
LarsDenBakker pushed a commit to LarsDenBakker/plugins that referenced this pull request Sep 12, 2020
BREAKING CHANGES:

This change leverages `globalThis` and will fail in older environments where `globalThis` is not implemented. See https://caniuse.com/#search=WebAssembly and https://caniuse.com/#search=globalThis. If your environment doesn't support `globalThis`, please use an earlier version of this plugin.

* Make wasm plugin work with service workers

The code generated by @rollup/plugin-wasm breaks when executed in a
service worker because window is undefined.

* Add test for worker scenario and simplify the fix

Instead of checking whether window is defined, we can just use
globalThis.

* Only run worker test if worker_threads supported
nickbabcock added a commit to nickbabcock/plugins that referenced this pull request Apr 29, 2022
The "target environment browser" test invoked the bundled output, which
leads to an undefined error on node 14 as [`atob` was introduced in node
16][0].

This commit uses the same atob polyfill that was introduced in rollup#334

[0]: https://developer.mozilla.org/en-US/docs/Web/API/atob#browser_compatibility
shellscape pushed a commit that referenced this pull request Apr 29, 2022
The "target environment browser" test invoked the bundled output, which
leads to an undefined error on node 14 as [`atob` was introduced in node
16][0].

This commit uses the same atob polyfill that was introduced in #334

[0]: https://developer.mozilla.org/en-US/docs/Web/API/atob#browser_compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants