You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.add or .require expose option hijacks entry module #1120
@jmm thanks for the clarification. I see it in the docs now, but I didn't realize that .add() adds another entry file.
Is there a way to add a module file with a custom module id to the bundle? In other words, is there a way to manually provide the file for one of the entry module's dependencies?
Note however that standalone only works with a single entry or directly-required file.
Is that for a standalone bundle there can only be one module that's exported, and browserify's API isn't expressive enough to allow what you want -- aliasing certain files under other ID's using b.require() -- and allow you to designate which entry file should be exported. Instead of a standalone option that takes a string ID there should probably be one of the following:
Here I have an app that requires React on the server and client. On the server, no problem. On the client, React weighs in at ~700kb, so I want to use React's minified lib, and alias any require calls to React to the minified version.
(Yes, I could run it through Uglify, but the minified build from the React team is smaller than what Uglify can produce without a grip of tinkering).
Anyway, after an hour of much profanity and trying to figure out why my App object is the React constructor, I stumbled across this issue.
@8bitDesigner Great, thanks. Both of you were confused by the documentation, and it's only even mentioned in the handbook, not the readme, so I think that can be improved. Depending how some other issues work out I may be looking to actually adjust the API or functionality though.
The browser field in package.json works but only for the require statements in your local sources. What happens if you want to do this for a dependency source? I realize using Browserify to remedy issues with upstream dependencies is not ideal but I ran into this recently as well: json-schema-faker/json-schema-faker#56 My hope was to make it so that require('faker') in json-schema-faker behaved as if it were require('faker/locale/en') so that my binary was not so big but it didn't work of course. I've done a lot of digging on the subject and this seems to be a pretty common pattern so is there an official way of handling this? I'm glad to help if I can.
@whitlockjc I don't know off the top of my head if it's the best way, but you should be able to do that with my pathmodify plugin. If you want to do it no matter where (what module) the require() call appears in, then bombs away:
Otherwise you can do it conditionally based on the requiring file pathname:
.plugin(pathmod(),{mods: [function(rec){varalias;if(rec.id==='faker'&&// Probably not something this naive, but you get the gist.rec.opts.filename.indexOf('json-schema-faker')>=0){alias={id: 'faker/locale/en'};}returnalias;}]});
Activity
jmm commentedon Feb 11, 2015
It's not in the readme, but the browserify handbook says:
asakusuma commentedon Feb 11, 2015
What does "directly-required" mean? Any use of
b.require? The behavior still happens when you useb.add(and are still using a single entry file).asakusuma commentedon Feb 11, 2015
And thanks for finding that note in the handbook @jmm
jmm commentedon Feb 11, 2015
I think so.
Can you elaborate? If you do
browserify('a').add('b'), that's 2 entry files.You're welcome. I knew I'd seen it somewhere.
asakusuma commentedon Feb 11, 2015
@jmm thanks for the clarification. I see it in the docs now, but I didn't realize that
.add()adds another entry file.Is there a way to add a module file with a custom module id to the bundle? In other words, is there a way to manually provide the file for one of the entry module's dependencies?
jmm commentedon Feb 11, 2015
I'm not sure exactly what's happening here without more extensive debugging, but try this (as a temporary workaround anyway):
Again, the current documentation makes it sound like this isn't even supposed to work.
In some cases the
package.jsonbrowserfield might help in a situation like this too.I wrote a plugin to do more extensive module ID / path aliasing, but not sure if that's called for here.
jmm commentedon Feb 12, 2015
I think the deal with this:
Is that for a standalone bundle there can only be one module that's exported, and browserify's API isn't expressive enough to allow what you want -- aliasing certain files under other ID's using
b.require()-- and allow you to designate which entry file should be exported. Instead of astandaloneoption that takes a string ID there should probably be one of the following:<EDIT 2015-03-30>or a
standaloneoption toadd()/require(), e.g.:</EDIT>Then you could do the kind of
require()calls you want and designate the module to export.8bitDesigner commentedon Mar 8, 2015
This just bit me hard, and I'm running against, what I think is a pretty common use case:
Here I have an app that requires React on the server and client. On the server, no problem. On the client, React weighs in at ~700kb, so I want to use React's minified lib, and alias any require calls to React to the minified version.
(Yes, I could run it through Uglify, but the minified build from the React team is smaller than what Uglify can produce without a grip of tinkering).
Anyway, after an hour of much profanity and trying to figure out why my
Appobject is the React constructor, I stumbled across this issue.8bitDesigner commentedon Mar 8, 2015
If this is unsupportable, that's totally cool, but I also read
And had no idea what that sentence meant. The fix here may just be some tweaking to the documentation.
jmm commentedon Mar 9, 2015
@8bitDesigner your situation sounds like a textbook case for the
browserfield inpackage.json-- have you tried it?8bitDesigner commentedon Mar 15, 2015
Ah! That totally worked for me; thanks for the pointer.
jmm commentedon Mar 16, 2015
@8bitDesigner Great, thanks. Both of you were confused by the documentation, and it's only even mentioned in the handbook, not the readme, so I think that can be improved. Depending how some other issues work out I may be looking to actually adjust the API or functionality though.
whitlockjc commentedon Aug 12, 2015
The
browserfield inpackage.jsonworks but only for therequirestatements in your local sources. What happens if you want to do this for a dependency source? I realize using Browserify to remedy issues with upstream dependencies is not ideal but I ran into this recently as well: json-schema-faker/json-schema-faker#56 My hope was to make it so thatrequire('faker')injson-schema-fakerbehaved as if it wererequire('faker/locale/en')so that my binary was not so big but it didn't work of course. I've done a lot of digging on the subject and this seems to be a pretty common pattern so is there an official way of handling this? I'm glad to help if I can.jmm commentedon Aug 13, 2015
@whitlockjc I don't know off the top of my head if it's the best way, but you should be able to do that with my
pathmodifyplugin. If you want to do it no matter where (what module) therequire()call appears in, then bombs away:Otherwise you can do it conditionally based on the requiring file pathname:
whitlockjc commentedon Aug 14, 2015
I'll give that a shot, thanks a lot @jmm.
whitlockjc commentedon Aug 17, 2015
@jmm That worked exactly like I needed it to. I really appreciate your help. I had literally tried all other things except this.
jmm commentedon Aug 17, 2015
@whitlockjc Great! Thanks for the feedback.
Only use entry files for standalone export