Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor-bundle and browserify-shim don't play well together #59

Open
DjebbZ opened this issue Feb 11, 2015 · 7 comments
Open

Factor-bundle and browserify-shim don't play well together #59

DjebbZ opened this issue Feb 11, 2015 · 7 comments

Comments

@DjebbZ
Copy link
Contributor

DjebbZ commented Feb 11, 2015

We were using browserify with browserify-shim to be able to use jQuery with a few plugins. So far so good. At this time we were creating another bundle for dependencies common across all pages, manually specifying them.

We then discovered and decided to use factor-bundle to factor common dependencies in a better way. The result was very good in terms of file size, but shims provided by browserify-shim disappeared. Since jQuery and its plugins are used in several pages, they're successfully factored out with factor-bundle, but they're produced without the shims. The problem is that jQuery plugins rely in window.jQuery that's no more available since there's no more shim.

We ended up requireing jQuery manually in the HTML and expose it, with <script>var jQuery = require("jquery")</script>. We would love factor-bundle to work with browserify-shim (or the opposite, whatever).

My understanding is that since factor-bundle is a plugin that act at the bundle level, it may not call it properly. But in our setup, it successfully apply the reactify transform (we use React.js with JSX syntax). So I suppose it's not a problem of factor-bundle not using transforms at all, but a specific incompatibility between factor-bundle and browserify-shim. However I couln't determine the problem precisely since I'm not familiar at all with the internals of those modules.

For reference in case it helps, here are relevant parts of our package.json and gulpfile (some parts are skipped to focus, it may look inconsistent/weird) :

package.json

  "browserify": {
    "transform": [
      "reactify",
      "browserify-shim"
    ]
  },
  "browser": {
    "jquery": "./node_modules/jquery/dist/jquery.js",
    "twitter-typeahead": "./node_modules/typeahead.js/dist/typeahead.bundle.js",
    "magnific-popup": "./node_modules/magnific-popup/dist/jquery.magnific-popup.js"
  },
  "browserify-shim": {
    "jquery": "$",
    "slick-carousel": {
      "depends": [
        "jquery"
      ]
    },
    "twitter-typeahead": {
      "depends": [
        "jquery"
      ]
    },
    "magnific-popup": {
      "depends": [
        "jquery"
      ]
    }
  }

gulpfile.js

function browserifyPages() {
    var pages = fs.readdirSync("./pages");

    var b = browserify(pages.map(function (p) {
        return "./pages/" + p;
    }), BROWSERIFY_ARGS);
    b.require("jquery");

    b = minifyBundle(b); // using minifyify
    return b.plugin("factor-bundle", {outputs: pages.map(function (p) {
            return JS_DST_DIR + "/" + p;
        })});
}

function browserifyCommonJs() {
    var b = browserify("./common/header.js", BROWSERIFY_ARGS);
    b.external("jquery");
    return minifyBundle(b);
}

Hope this helps identify the problem. Thanks in advance.

(Let me ping browserify-shim people : @thlorenz and @bendrucker. In case I shouldn't, sorry guys !)

@DjebbZ
Copy link
Contributor Author

DjebbZ commented Feb 12, 2015

I didn't tell, but I'm using : node 0.10.25/35, borwserify latest 8.1.3, browserify-shim latest 3.8.2, jquery latest 2.1.3.

@zetlen
Copy link

zetlen commented Feb 20, 2015

Got bit by this too. Same versions as @DjebbZ above. For now, since the only thing I'm shimming is jQuery, I'm gonna factor it out.

@idolizesc
Copy link

I strongly recommend literalify as an alternative to browserify-shim. If you are using large/well-known libraries like jQuery you probably want to include them from a separate script file/CDN anyway, and literalify makes that super easy.

@thlorenz
Copy link

thlorenz commented May 7, 2015

Actually b-shim uses exposify to do just what you decribe, see the global feature.

@zetlen
Copy link

zetlen commented May 8, 2015

@idolizesc Does it work with factor-bundle?

@idolizesc
Copy link

@zetlen Yes, it does.

I know you can do the same thing with b-shim, but it is just more work to configure in my opinion. Totally personal preference.

@bendrucker
Copy link

"literalify": {"some-dependency": "window.$"}
"browserify-shim": {"some-dependency": "global:$"}

The only difference is that b-shim doesn't support transform options for config but exposify does. And exposify is smarter for the global use case. Yes, it won't let you do arbitrary code. But it will check global in addition to window.

jcppman added a commit to jcppman/factor-bundle that referenced this issue Jun 19, 2015
fix browserify#68, browserify#59

- To avoid writing to ended writeStream, outputs will be constructed everytime
- In the scenario that user wanna use outpipe, the `outopt` will be
kept as string, so we can determine the user's attempt in later codes
and do the relavant opperations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants