Skip to content

Conversation

@garyb
Copy link
Member

@garyb garyb commented Aug 6, 2015

This is a slightly sneaky trick to prevent webpack, etc. attempting to bundle node's url and xmlhttprequest modules in the JS when bundling for the browser.

jdegoes added a commit that referenced this pull request Aug 6, 2015
Use module.require to prevent bundling for browser
@jdegoes jdegoes merged commit db63471 into master Aug 6, 2015
@jdegoes jdegoes deleted the module-require branch August 6, 2015 20:39
@ozanmakes
Copy link

@garyb Looks like this and #39 breaks browserify's node compatible output:

$ echo "console.log(typeof module.require)" > test.js
$ node test.js
function
$ browserify --node --outfile browserified.js test.js
$ node browserified.js
undefined

Would you consider using another method of detection or going back to if (typeof module !== "undefined" && module.exports) and require("xhr2")?

@garyb
Copy link
Member Author

garyb commented Feb 9, 2016

I'm not really sure how to do that - the purpose of this was to explicitly ensure browserify and webpack would not bundle the require'd module (so we don't include a massive blob of useless JS for the browser), and I don't think there's a way to direct webpack or browserify to behave differently depending on the command line arguments they're given purely by modifying the JS.

On the other hand, I don't want to have to include some instructions about how to use a custom setup to build this with browserify/webpack, to explicitly ignore the require'd module. I'm not sure it even would be possible to have pulp build it anymore, which is the most common PS build tool, so that's pretty bad too.

I'll try some stuff out. I think when I tried it before browserify and webpack saw through attempts to use something like var req = require; and then using req("xhr2"), but maybe there's something along those lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants