Can you help me understand the benefit of require.js?
gist.github.com
gist.github.com
But if you don't buy into compilation as a step in your frontend development cycle, I'm not sure there's much point in using require.js, goog.provide/require, or any other module system.
For example, in the last large js project I was on, we found that the closure compiler by itself - even without ADVANCED_OPTIMIZATIONS - did well enough at optimizing the code that a separate module system wasn't worth it in terms of additional time to refactor.
You had a set of modules and compiled them into one file and did not use a module system - I am still naive on modern JavaScript work so any guidance gratefully etc.
While we were looking at that, I found that we could make our performance target by concatenating all the source together, running them through closure, and just serving up one monster js file a little under 1MB in size.
This was an enterprise system, so we could assume certain levels of client performance that cat picture industry applications probably can't, as well as having a lower bar for performance in the first place - a five second load time on a low performing system was firmly ok, so YMMV.
Compared to require.js browserify has the following benefits:
* CommonJS module format which is less verbose than AMD
* it uses node_modules/ dir to load dependencies from so
* ... you can use npm to install dependencies
* ... and if you use Node.js then that facilitates code sharing between your client and server
If this is an open source project, you just went from:
python -m SimpleHTTPServer || open index.html
To:
npm install && grunt build && grunt run
... And now as a person wanting to work on your code I have a whole slew of new tools to learn!
It's not always as cut and dry of a decision...
I don't think grunt is as pervasive as npm in the world of node.js.
Generally I have something like the following...
init.js - in the head, modernizr, css-browser shims etc
jquery.js - before /body
site.js - site-wide functionality
section.js - functionality for a section of the site
page.js - functionality for that specific page
This allows me to cache/reuse common functionality and still have a limited number of http requests for js... how would I accomplish this with requirejs, browserify and the rest?From what I've seen you can't. Ideally, I'd like to be able to package harmony-style modules.. there's some transpilers for that structure already. It would just be nice to be able to reliably package sections for downlevel reuse without having one huge script per page in a site that is expressly not single page.
1. https://github.com/substack/node-browserify#multiple-bundles
Is it no longer good enough that the open source community builds great free things for us to use? Do they now have to upload the know how of these tools to the users brains in a Matrix like fashion?
In terms of developer time, 2 days is rather expensive.
If this is your own pet project, then by all means spend the time necessary to learn it. Or if you intend to "Move Fast and Break Things", then spending time to learn things is also good. But if you're running a business with "x" which has worked efficiently for years and isn't broken, "y" is not necessary.
You are using words like efficiently and phrases like "isn't broken" without quantification. Having only done things one way, and it doing it's job does not mean it's efficient or not broken. Only that you don't know a more efficient way or it hasn't been proven to break yet.
While getting things out the door is important, it's also just as important to not base assumptions without actually quantifying. And you can't do that if you assume the mantra of "if it ain't broke, don't fix it."
It's a balance that, as a professional, you have to manage.
This isn't a "if it ain't broke, don't fix it" either. You push things to the breaking point and see if that's projected in your growth (maybe a bit beyond what's projected), but maintainability and efficiency don't hinge on what's new. It hinges on what works and if what works is what you have, you don't waste time with new things.
But back to the point... You learn "new" things when you figure existing things aren't up to the task or you try to see if amending x will get you further. y Is still not necessary if it will.
Contrary to what? I said as much. The problem is that you need to quantify that efficiency. You can't just say "x is efficient."
> But if you're running a business with "x" which has worked efficiently for years and isn't broken, "y" is not necessary.
So, x has worked efficiently for years? Based on what? How are you quantifying that? And how can you say it's efficient without having some yardstick to measure against? By not looking at other technologies, you can't really say whether it is still efficient or not.
Efficiency is a moving target.
Absolutely, but each developer will make that judgement and require.js shouldn't be victims of those who give up at the first hurdle.
>> There are require.js examples apps on github , Q and As on Stackoverflow, and open source code that's available for digging deep
That's good to hear. I will at some point give it another shot and it's good to know there are examples and support out there. It was a couple of years ago when I did look into it, I remember getting confused and the on site documentation didn't really steer me in the right direction. The documentation doesn't appear to have changed much in that time. It can be hard work and time consuming having to dig deep to solve a problem.
>> Do they now have to upload the know how of these tools to the users brains in a Matrix like fashion?
The OSC are not obliged to provide a better user experience in getting new users to adopt their software. Of course not. But if they want to have as many developers appreciating their hard work, then you know, it doesn't hurt.
>> It only took me ~2 Days to get the gist of require.
2 days to learn how to use it is something I cannot afford for a workflow improvement over a show-stopper problem. If it was something I could do in a hour to get an understanding and start porting my application with the knowledge that I'm not going to be stuck for 2 days, then that would be a different situation.
The real trigger for introducing RequireJS was our need to optimize our JS by concatenating and minifying it. And to do that in the right order, you need to know your dependencies very well. That's when I decided I didn't want to do that by hand. Not now, and not in the future. So I took https://github.com/tnajdek/angular-requirejs-seed as an example and started defining modules and dependencies. Now I can easily run the r.js optimizer as a build step before releasing and now I'll get a correct and concatenated/minified JS for production.
The nice thing is that RequireJS not only manages our Angular modules (defined in separate files), but also their dependencies on vendor scripts (JQuery, Hammer.js etc.) I really have the feeling I'm in control again.
I haven't looked into alternatives (mainly Browserify) and don't know if I will use JS modules in my future development. The idea seems neat, but it really adds a lot of pain in my opinion. Perhaps there is a threshold of webapp complexity, past which the pain is worth it.
it's an astounding amount of complexity to introduce into a project, just to be able to get the bare minimum of functionality.
if something breaks, and something always breaks, I simply don't want to be responsible for debugging it.
in development:
<script data-main="your-module.js" src="require.js"></script>
in production: <script src="your-module.min.js"></script>
One problem I haven't solved is when html renderer writes inputs to your javascript module. I can either print my module's main body in html: <script>
require(['yourModule'], function(yourModule) {
var config = {% server template vars %};
yourModule.start(config);
});
</script>
Or use global config variable and shim.view-source:https://www.wakati.me/
https://www.wakati.me/static/js/router.js
This also allows changing 1 version number to invalidate the user's cached static files. I don't know if the OP's build script supports that or not.
Above site was built using conventions found here:
My current project uses require and after several months of working with the legacy code, I have not been convinced that it has improved maintainability. I would not use it again if given the choice.
The work-around is to use require.js in commonjs style or to add hacks into your architecture. Not fun.
This whole thread reminds me of the PHP days before namespaces. If you have circular dependencies within your own codebase, you have an architecture problem. If they are introduced by third-party libraries, maybe the usage of the library should be questioned.
I have a Modal class which depends on some utilities in a Utility class, but the Utility class depends on the Modal class because there are some utilities that show Modals.
This is not a broken architecture and is handled just fine by other programming languages/dependency handlers.
What are the utilities contained in this module?
Hence, careful selection of projects to work on is key.