101arrowz / fflate

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

Facing an issue when fflate.min.js is being bundled #51

Closed jscastro76 closed 3 years ago

jscastro76 commented 3 years ago

Background I'm maintaining Threebox, and I updated the repo to Three.js r127 which now uses fflate in many different loaders.

The problem The problem is that I'm finding issues to bundle fflate.min.js through different bundling tools, including browserify, webpack and vue-cli-service.

How to reproduce Using the file you provided to Three.js fflate.min.js, referenced from [FBXLoader] that is also bundled in my plugin, and then bundlef with broserify or webpack (I have faced issues also with vue, but there could be ther more dependencies...) I get the following error every time I execute the bundle commands:

browseryfy:

> browserify -p tinyify exports.js > dist/threebox.min.js 
Error: Can't walk dependency graph: ENOENT: no such file or directory, lstat 'C:\.....\threebox-new\worker_threads' required by C:\.....\threebox-new\src\objects\fflate.min.js[threebox-new] run build successfully completed

Webpack:

WARNING in ./node_modules/threebox-plugin/src/objects/fflate.min.js 6:538-570
Module not found: Error: Can't resolve 'worker_threads' in 'C:\....\node_modules\threebox-plugin\src\objects'
resolve 'worker_threads' in 'C:\....\node_modules\threebox-plugin\src\objects'
  Parsed request is a module
  using description file: C:\....\node_modules\threebox-plugin\package.json (relative path: ./src/objects)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in C:/..../node_modules
        single file module
          using description file: C:\....\package.json (relative path: ./node_modules/worker_threads)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              C:\....\node_modules\worker_threads doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              C:\....\node_modules\worker_threads.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              C:\....\node_modules\worker_threads.json doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              C:\....\node_modules\worker_threads.wasm doesn't exist
        C:\....\node_modules\worker_threads doesn't exist
ModuleNotFoundError: Module not found: Error: Can't resolve 'worker_threads' in 'C:\....\node_modules\threebox-plugin\src\objects'
    at C:\....\node_modules\webpack\lib\Compilation.js:1668:28
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:712:13
    at eval (eval at create (C:\....\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:10:1)
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:273:22
    at eval (eval at create (C:\....\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:9:1)
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:402:22
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:117:11
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:628:24
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:782:8
    at C:\....\node_modules\webpack\lib\NormalModuleFactory.js:902:5
 @ ./node_modules/threebox-plugin/src/objects/loaders/FBXLoader.js 2:15-42

Alternatives Some bundlers accept the option to ignore a reference to a module, but not all of them. I was guessing if you find an easy way to avoid this reference or make it conditional in the code. Any hint will be also greatly appreciated.

jscastro76 commented 3 years ago

I have found a workaround based on some threads facing similar issues with other modules using explicit require in the code of core modules...

/// instead of var e = {}, assign e to eval
function(_f){"use strict";var e=eval;...

///then... use the e to eval the require first 
  try{e("require('worker_threads')").Worker}

It's not a big deal, but it will solve the issue with a minimum code change keeping compatibility with browser and bundle use. This thread gave me the clue, but specially this tweet

101arrowz commented 3 years ago

Actually, I foresaw this issue and provided an unminified ESM build and an unminified CJS build to avoid the problem. I was hoping that nobody would ever use Webpack on the UMD build; it was meant specifically for direct users. Could you try replacing this line with a require call to this CJS build?

101arrowz commented 3 years ago

@jscastro76 Let me know if you have any issues after using the CJS build; I'll close this issue for now. If you find any further problems, I'll reopen it.

jscastro76 commented 3 years ago

I solved it in the way commented above, but surely more people bundling the fflate version of three.js will face that issue. I was thinking on moving to CJS so then I can come back to the original monitors version. Thanks for your quick reply!