Skip to content

.add or .require expose option hijacks entry module #1120

Open

Description

@asakusuma

If you b.add or b.require a module and re-map it's module id using the expose option, it will replace the entry module as the root module for the bundle. I reproduced this in a demonstration repo: https://github.com/asakusuma/browserify-expose-bug

I'm assuming this is not expected behavior, but if it is, is there another way to include a file as a module and re-map the module id?

Activity

  1. jmm commented on Feb 11, 2015

    @jmm
    Collaborator

    It's not in the readme, but the browserify handbook says:

    Note however that standalone only works with a single entry or directly-required file.

  2. asakusuma commented on Feb 11, 2015

    @asakusuma
    Author

    What does "directly-required" mean? Any use of b.require? The behavior still happens when you use b.add (and are still using a single entry file).

  3. asakusuma commented on Feb 11, 2015

    @asakusuma
    Author

    And thanks for finding that note in the handbook @jmm

  4. jmm commented on Feb 11, 2015

    @jmm
    Collaborator

    What does "directly-required" mean? Any use of b.require?

    I think so.

    The behavior still happens when you use b.add (and are still using a single entry file).

    Can you elaborate? If you do browserify('a').add('b'), that's 2 entry files.

    And thanks for finding that note in the handbook @jmm

    You're welcome. I knew I'd seen it somewhere.

  5. asakusuma commented on Feb 11, 2015

    @asakusuma
    Author

    @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?

  6. jmm commented on Feb 11, 2015

    @jmm
    Collaborator

    I'm not sure exactly what's happening here without more extensive debugging, but try this (as a temporary workaround anyway):

    var b = browserify({
      standalone: 'MyModule'
    });
    
    b.require('./modules/root', {expose: 'z'});

    Again, the current documentation makes it sound like this isn't even supposed to work.

    In some cases the package.json browser field 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.

  7. jmm commented on Feb 12, 2015

    @jmm
    Collaborator

    I think the deal with this:

    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:

    browserify({standalone: {file: './path/to/file', id: 'module-name'})
    // or
    browserify()
      .standalone('./path/to/file', 'module-name')

    <EDIT 2015-03-30>
    or a standalone option to add() / require(), e.g.:

    browserify()
      .add('./path/to/file', {standalone: 'module-name'})

    </EDIT>

    Then you could do the kind of require() calls you want and designate the module to export.

  8. 8bitDesigner commented on Mar 8, 2015

    @8bitDesigner

    This just bit me hard, and I'm running against, what I think is a pretty common use case:

    $ browserify \
      -r ./node_modules/react/dist/react.min.js:react \
      -e some/app/.js \
      --standalone App
      -o public/bundle.js
    

    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.

  9. 8bitDesigner commented on Mar 8, 2015

    @8bitDesigner

    If this is unsupportable, that's totally cool, but I also read

    Note however that standalone only works with a single entry or directly-required file.

    And had no idea what that sentence meant. The fix here may just be some tweaking to the documentation.

  10. jmm commented on Mar 9, 2015

    @jmm
    Collaborator

    @8bitDesigner your situation sounds like a textbook case for the browser field in package.json -- have you tried it?

  11. 8bitDesigner commented on Mar 15, 2015

    @8bitDesigner

    Ah! That totally worked for me; thanks for the pointer.

  12. jmm commented on Mar 16, 2015

    @jmm
    Collaborator

    @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.

  13. whitlockjc commented on Aug 12, 2015

    @whitlockjc

    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.

  14. jmm commented on Aug 13, 2015

    @jmm
    Collaborator

    @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:

    var pathmod = require('pathmodify');
    browserify()
      .plugin(pathmod(), {mods: [
        pathmod.id('faker', 'faker/locale/en'),
      ]});

    Otherwise you can do it conditionally based on the requiring file pathname:

      .plugin(pathmod(), {mods: [
        function (rec) {
          var alias;
    
          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'};
          }
          return alias;
        }
      ]});
  15. whitlockjc commented on Aug 14, 2015

    @whitlockjc

    I'll give that a shot, thanks a lot @jmm.

  16. whitlockjc commented on Aug 17, 2015

    @whitlockjc

    @jmm That worked exactly like I needed it to. I really appreciate your help. I had literally tried all other things except this.

  17. jmm commented on Aug 17, 2015

    @jmm
    Collaborator

    @whitlockjc Great! Thanks for the feedback.

  18. added a commit that references this issue on Sep 23, 2015
    cd2d8ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions