choojs / bankai

:station: - friendly web compiler
Apache License 2.0
1.09k stars 102 forks source link

problem bundling hypercore: 'require is not defined' #522

Closed rhodey closed 5 years ago

rhodey commented 5 years ago

Bug report w/ example app and steps to reproduce

Expected Behavior

bankai build index.js should bundle the npm module hypercore@6.25.0 correctly without problem. I am using bankai 9.15.0.

Current Behavior

something about require('hypercore') is breaking the bankai output bundle, in the console I get ReferenceError: require is not defined.

strangely, bankai start index.js works as expected and doesn't break require, hypercore and everything works just fine. this only appears to be a problem with bankai build index.js.

Context

I'm using choo and bankai for a P2P app that gossips hypercores over WebRTC. http://radiowitness.hashbase.io/

Code Sample

https://github.com/rhodey/bankai-hypercore-poc

Your Environment

$ node -v v10.4.0 $ npm -v 6.1.0

yoshuawuyts commented 5 years ago

Hmm, odd. We're enabling some optimizations for build that we don't for start. I wonder what the culprit might be.

goto-bus-stop commented 5 years ago

it seems to be using sodium-native instead of sodium-javascript in prod for some reason.

goto-bus-stop commented 5 years ago

hypercore has a conditional call to require('fd-lock'). browser-pack-flat is overeager about inlining it. fd-lock doesn't work in the browser, so hypercore should probably explicitly exclude it using the browser field in package.json:

"browser": {
  "fd-lock": false
}

but it's also something that should be addressed in browser-pack-flat. it doesn't currently try to check if a require call may be conditional.

rhodey commented 5 years ago

wow thanks for the quick research @goto-bus-stop! I tried debugging this myself yesterday but I'm very unfamiliar with bankai and js bundling in general. If you have any tips to share on your debug process I'm very curious to know. either way thanks! sounds like i should close this out and re-open a similar report on browser-pack-flat