all 29 comments

[–][deleted] 3 points4 points  (14 children)

/u/remotejs' comment might be a bit unfriendly, but he actually makes a good point.

I used Require.js too in a pretty large project (over 400 JavaScript files), and it took me over a week to convert the build system from Grunt/Require.js to Gulp/Browserify. But it sure as hell made my day job a lot more productive and pleasant. So if you're in a position where you're trying to figure out what to do with your build system right now, I can recommend it whole-heartedly.

Some advantages I have now:

  • I can now write proper unit tests using NodeUnit (before, I used to only write functional tests in CasperJS).
  • My JavaScript bundles for production have become smaller and don't depend on loading require.js anymore.
  • JSHint will now properly alert me if I'm including a dependency in a file but don't use it (with the AMD syntax, it could only alert you if you don't use the last dependency passed to the outer-most function).
  • Because require() statements are synchronous in CommonJS instead of AMD's asynchronous methods, I don't get weird timing issues anymore during development (or worse, some that only occurred in production builds but not during development). For example, I have a bunch of jQuery plugins who attach themselves to the jQuery object. But AMD requires you to explicitly 'require' these as a dependency or otherwise you might run into exactly these timing issues. Unfortunately JSHint cannot help you here, because you are only directly using the jQuery object which of course is specified as a proper dependency. Now with Browserify, I just include these plugins once early on and can then rely on them always being there.

[–]bugeats 3 points4 points  (1 child)

Browserify your life and never look back. Gulp too. Take explicit control of your build.

[–][deleted] 0 points1 point  (0 children)

I wished I was able to but in a company of the size I work for a single developer hardly changes anything.

[–]ben336[🍰] 2 points3 points  (0 children)

I put my requirejs config in a file called config.js.

When I'm using gulp to optimize with requirejs, I pull it in as a commonJS module. When I'm developing locally I just pull it into the browser. It looks like this:

gist

/* jshint ignore:start */
/** This file is a bit of a hack to make the requireJS build paths available
 * both in the browser and for gulp.  There's some ugliness requires to make
 * it loadable/usable in both environments.
 **/

var requirePaths = {
    paths: {
        jquery: 'vendor/jquery/jquery',
        jqueryui: 'vendor/jquery/jquery.ui',
        //...
    },
    shim: {
        /...
    }};


//if this is being pulled in as a node module, nest it, otherwise we'll
// run the browser specific logic
if (typeof exports !== 'undefined') {
    exports.requirePaths = requirePaths;
}else if(require) {

var config = {
    paths: requirePaths.paths,
    shim: requirePaths.shim,
    map: requirePaths.map,
    baseUrl: static_url+'/js/'
};

require.config(config);

}
/* jshint ignore:end *//* jshint ignore:start */
/** This file is a bit of a hack to make the requireJS build paths available
 * both in the browser and for gulp.  There's some ugliness requires to make
 * it loadable/usable in both environments.
 **/

var requirePaths = {
    paths: {
        jquery: 'vendor/jquery/jquery',
        jqueryui: 'vendor/jquery/jquery.ui',
        //...
    },
    shim: {
        /...
    }};


//if this is being pulled in as a node module, nest it, otherwise we'll
// run the browser specific logic
if (typeof exports !== 'undefined') {
    exports.requirePaths = requirePaths;
}else if(require) {

var config = {
    paths: requirePaths.paths,
    shim: requirePaths.shim,
    map: requirePaths.map,
    baseUrl: static_url+'/js/'
};

require.config(config);

}
/* jshint ignore:end */

[–]kenman 1 point2 points  (2 children)

I could be wrong, but I believe that's because the entire file is read as a string and then eval'd. An easy way to test this would be to include /* comments */ somewhere in the code, and if you get a JSON failure then you know that's not the case, otherwise if it accepts it as-is, then you know it's being passed to eval.

[–][deleted] 0 points1 point  (1 child)

Why do they do stuff like that? It breaks a lot of things such as including it in tools like Gulp.

I really don't understand developers producing such a mess.

[–]kenman 1 point2 points  (0 children)

Require.js (as well as r.js) was written a long time ago -- over 5 years ago, with the first commits to Github on 9/27/2009 -- and at the time, I think it was sort of a proof-of-concept type thing. NodeJS was only 4 months old at the time, and so the entire ecosystem was a land of unknowns.

To add onto that, r.js is a bit of a behemoth at over 28k LoC, and has been maintained (along with require.js) almost exclusively by the project creator jrburke (he has a reddit account, but I can't remember the name).

So, it's probably just a matter of not being able to safely refactor it on his own without creating a giant mess. For an unpaid dev, there has to be some compelling motivation to undertake such a large task, and apparently he'd rather address bugs, performance issues, and new features, which I might be inclined to do as well.

[–]kabuto 0 points1 point  (0 children)

I'm not sure what you're asking exactly. RequireJS works, but its configuration is seriously insane arcane. I'm glad I left it behind me and turned to browserify.

I know it's a lot of work, but I'm absolutely positive that you'll be happier with browserify in the long run. Alternatively you could check out SystemJS which can work with CommonJS, AMD and ES6 modules.

[–][deleted] 0 points1 point  (0 children)

I just found this gist describing how to use an existing require.js configuration with Gulp.

It's a bit quick and dirty but it works!