ef4 / ember-browserify

ember-cli addon for easily loading CommonJS packages from npm via browserify.
MIT License
172 stars 28 forks source link

Can't handle try-catch for importing optional dependencies #86

Closed jonesetc closed 7 years ago

jonesetc commented 8 years ago

Some modules use a try-catch pattern as seen here to import an optional dependency or fallback. This does not seem to work with ember-browserify.

Example app: https://github.com/jonesetc/browserify-demo

Build output:

Build failed.
The Broccoli Plugin: [object Object] failed with:
Error: Cannot find module 'bufferutil' from '/home/kyle/code/js/browserify-demo/node_modules/ws/lib'
    at /home/kyle/code/js/browserify-demo/node_modules/ember-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:46:17
    at process (/home/kyle/code/js/browserify-demo/node_modules/ember-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:173:43)
    at ondir (/home/kyle/code/js/browserify-demo/node_modules/ember-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:188:17)
    at load (/home/kyle/code/js/browserify-demo/node_modules/ember-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:69:43)
    at onex (/home/kyle/code/js/browserify-demo/node_modules/ember-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:92:31)
    at /home/kyle/code/js/browserify-demo/node_modules/ember-browserify/node_modules/browserify/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:82:15)

The broccoli plugin was instantiated at: 
undefined

If you manually go and update the import at node_modules/ws/lib/BufferUtil.js to skip the optional dependency and just load the fallback, the build gets past this easily. There is another one you have to update here and then the whole build works just fine.

jonesetc commented 8 years ago

I'm trying to understand how this addon works, so I could be far off, but I think it might have to do with this snippet. It throws an error if the package isn't found locally, regardless of if that is tolerable or not.

ef4 commented 8 years ago

This is a limitation of browserify. I think there may be an option to tell it to ignore certain package names.

thecristen commented 7 years ago

Any way to get around this?

ef4 commented 7 years ago

You may want to experiment with browserify's ignore option. See also the discussion in https://github.com/substack/node-browserify/issues/901

shaunc commented 7 years ago

The following seems to work in config/environment (mutatis mutandis):

browserify: {
  ignoreMissing: ['WNdb', 'lapack'],
}

For an addon, you can put in tests/dummy/config/environment.js and/or add into config() in index.js (former to ignore during testing, the latter to ignore when installed as dependency to some other project.

I was using sentencer to test an addon, but didn't want dependent projects to have to include as it is very large. The issues were: 1) sentencer tries to include other optional dependencies I didn't want, and 2) To avoid sentencer in dependent packages, I put in devDependencies rather than dependencies, which resulted in failure in dependent package with a cryptic error message (which fortunately I was able to decipher because I knew I hadn't put sentencer in dependencies):

The Broccoli Plugin: [object Object] failed with:
Error: Cannot find module 'sentencer' from '/Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/tmp/stub_generator-output_path-psVQMCJc.tmp'
    at /Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/node_modules/browser-resolve/node_modules/resolve/lib/async.js:46:17
    at process (/Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/node_modules/browser-resolve/node_modules/resolve/lib/async.js:173:43)
    at ondir (/Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/node_modules/browser-resolve/node_modules/resolve/lib/async.js:188:17)
    at load (/Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43)
    at onex (/Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31)
    at /Volumes/Macintosh_HD/Users/shauncutts/src/crane-wisdom-service/submodules/crane-wisdom/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:111:15)

The broccoli plugin was instantiated at: 
undefined

For (1) I used the above snippet in test/dummy/config/environment.js, and in addon ./index.js, for (2) I now have:

  config(env, baseConfig) {
    var browserify = baseConfig.browserify = baseConfig.browserify || {};
    var ignoreMissing = browserify.ignoreMissing = browserify.ignoreMissing || [];
    if(ignoreMissing.indexOf('sentencer') === -1) {
      ignoreMissing.push('sentencer');
    }
  }

... seems to work! :)