Skip to content

Standalone and External #670

Open
Open

Description

@fdecampredon

I'm creating a little extension for React.
I would like to add react as external, and still be able to compile my library as an umd module.
However when using standalone + external I would need that the obtained umd module would be able to fetch 'react' as a global dependencies or as an amd module dependencies, is there a way to do that properly ?

Activity

  1. mulderp commented on Mar 16, 2014

    @mulderp

    I am having a somewhat similar problem, and solve it like this.
    In index.js I define:

    var $             = require('jquery-untouched');
    var _             = require('underscore');
    var Backbone      = require('backbone');
    Backbone.aPlugin = require('./aPlugin.js');
    Backbone.$        = $;
    
    module.exports = {
        $: $,
        _: _,
        Backbone: Backbone
    };
    

    Then, I do:

    browserify -r ./index > browser/bundle.js
    

    And in index.html, I add:

      <script src="bundle.js"></script>
      <script>
        var app = require("./index");
        var $ = app.$;
        var _ = app._;
        var Backbone = app.Backbone;
      </script>
    

    Would be interesting to hear other's thoughts, if this approach makes sense, or if there are others.

  2. cmaher commented on Jul 24, 2014

    @cmaher

    I have submitted a PR to the project that provides the 'standalone' option. If that PR is accepted, then I will submit a PR to node-browserify to support that behavior. ForbesLindesay/umd#19

  3. cmaher commented on Jul 25, 2014

    @cmaher

    I am currently handling this with my fork at cmaher/node-browserify like

      var bundleStream = browserify('./index.js', {
          standalone: {
            name: 'my-module',
            amd: { deps: ['underscore'] }
          }
        })
        .exclude('underscore')
        .bundle();
  4. QuentinRoy commented on Mar 10, 2015

    @QuentinRoy

    So bad this issues seems to receive so few support :-(. I would love to see such behaviour.
    In the mistime, I found two workaround (none is perfect though).

    The first one is using pure-cjs instead of browserify. It handles UMD style export perfectly and if you already know browserify, you almost have nothing to do. It is perfectly compatible with most (all?) browserify transforms.
    Unfortunately pure-cis has 2 major disadvantages (in my opinion):

    1. It always reformats your code (actually the results is the output of your source's tree parsed with esprima)
    2. All your dependencies must be accessible by node during the build (event the one you are excluding).
      That's the solution I am currently using.

    The second one is detailed in this post. The main idea is to generate your umd wrapper yourself (or modifying the template of a UMD generator). It is a little bit harder to do though and looks a little bit hacky...

    Once again, @cmaher, I think it is very sad your pull request didn't get accepted.. IMO dependency management is a must have so that you can build good libraries. Because of this missing behaviour, I have seen libs embedding their dependencies in their build, meaning that users must load the library twice if he also needs it..

  5. cmaher commented on Mar 10, 2015

    @cmaher
  6. terinjokes commented on Apr 24, 2015

    @terinjokes
    Contributor

    @cmaher Bummer your contribution to umd was never accepted. I've seen a couple requests for this feature, and agree it would have been great to add.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions