101arrowz / fflate

High performance (de)compression in an 8kB package
https://101arrowz.github.io/fflate
MIT License
2.24k stars 76 forks source link

Could not resolve import "worker_threads" when trying to use in web #12

Closed maxevilmind closed 3 years ago

maxevilmind commented 3 years ago

Hi everyone, I am trying to use the library as an ESM module like that: import * as fflate from 'fflate'; But I am getting Error: Could not resolve import "worker_threads" in "../../../node_modules/fflate/esm/node-worker.js". In esm/index.js I see the following line import wk from './node-worker'; and if I change it to import wk from './worker'; it works fine.

As I understand it tries to run node version worker in web for some reason. Does it miss some sort of condition to import proper worker?

I really need this so I am ready to work out a PR for that.

101arrowz commented 3 years ago

What bundler are you using? I rely on the "browser" key of package.json to replace the import in the browser.

maxevilmind commented 3 years ago

@101arrowz we don't have any bundler 🙈 We have a build-less open-wc setup (more on that https://modern-web.dev/guides/going-buildless/es-modules). That's pretty much what brought us to use fflate as it provides ESM exports and other similar libraries are common js. I am not sure if it is somehow possible to utilize this key while using no bundler.

101arrowz commented 3 years ago

If that's the case, I will change the build script to add a version that supports true ESM. Is there nothing in that guide that allows custom module resolution?

maxevilmind commented 3 years ago

@101arrowz wow that would be really amazing to get the true ESM version. Is there nothing in that guide that allows custom module resolution? I think unless I write my own tool for it - there is no way...

101arrowz commented 3 years ago

@maxevilmind I've pushed v0.3.6 with commit 214ec2f645b3195390f95abd0c0925609e28d9bd. Try the import again. Thanks for the feature request!

maxevilmind commented 3 years ago

@101arrowz amazing! thank you for pushing it quickly! I will test it out today

101arrowz commented 3 years ago

Did it end up working for you?

maxevilmind commented 3 years ago

yep, import works perfectly now thank you for adding the changes!

101arrowz commented 3 years ago

No idea how useful this is for you at this point @maxevilmind, but you probably would have been able to get the package working like this:

web-dev-server --config config.wds.mjs

In config.wds.mjs:

export default {
  nodeResolve: {
    browser: true
  }
}

Obviously I'm not going to revert the package to before this bugfix, just thought it may be useful to know.