Sunday, February 16, 2014

Javascript Module Loaders Considered Harmful

Introduction

I’m coming to an opinion of Javascript module loaders that is profoundly negative and I’d like to express why I think they are, generally, a bad idea. However, I do think they have a place, which I’ll get to at the end.

Now, I understand I might be in the minority here. Between the competing specifications of CommonJS and AMD modules, loader systems like RequireJS or the (honestly really awesome) Google Module Server, and the huge cultural influence of Node on the Javascript world, you’d be hard pressed to argue against Javascript modules these days. Scripts are old hat, too stupid, too inflexible. Everyone knows that and no one would make an argument in their favor, right?

I’m going to step out on a limb and say “Javascript Module Loaders Considered Harmful” and I know the baggage involved with declaring something “Considered Harmful”. I mean every ounce of context that phrase carries with it, and I hope I can persuade you.

Harm #1: Confused Debuggers

Module loaders often make debugging difficult in a number of obvious and subtle ways.

Compression, transpiling, and other pre-processing in the pipelines that typically support module loaders (which are not always necessary, but are in use in tandem consistently) rewrite scripts from the input module to the source that actually gets parsed by the browser. There is a frequent mismatch when debugging in the browser between your actual source and the code you see in the debugger. Source Maps are helping this, but support for them in both browsers and tool chains is still underwhelming.

Often times, even when the source is unchanged or the source maps work properly, the simple nature of the dynamic tags used by module loaders causes issue. Debuggers might not recognize the same script between page loads, without an inline <script> node, causing breakpoints to be lost, for example.

It is very hard to justify module loaders on their benefit when they can make debugging your code, the very thing they aim to help you organize and simplify, so much more difficult.

Harm #2: Module Load Order

Perhaps the single most common and frustrating thing to deal with in module loaders is the order of load or script execution. Compared to the expected load order of a series of <script> tags in your page, understanding when each module will be executed can be an exercise in frustration quickly.

Execution order from module loaders is usually a factor of the race to which modules are downloaded first and the inter-module dependencies defined which will keep actual execution of a downloaded script pending until its dependencies have all been loaded.

There is obvious merit here and helping us to define and understand dependencies, and to provide clear references between them in the form of things like require() calls, is certainly admirable.

However, the fact is that the vast majority of our projects don’t have such complicated intra-module dependencies to really justify this.

Harm #3: Workflow Complication

Simply put, using a module loader instead of simple scripts is simply harder to work with.

There are well known difficulties integrating other libraries that don’t use the same module system. That there are multiple such systems makes this worse. Library authors are forced to provide extra wrappers for all the different loaders or pick a side when their users have or care not to.

When you bring in new developers to a project or when a developer finds a library she wants to use which has decided to distribute itself as a module in a specific system, like RequireJS, there is an added barrier to use with little apparent benefit to a developer who just expected another <script> tag to their existing project. Is it worth integrating someone else’s preferred loader to use their simple library? I fear this may lead to fragmentation of our already disperse ecosystem.

Conclusion

The harms are not justified compared to the benefit when the majority of web applications utilize such a small and constant set of Javascript.

There is a case for module loaders, which can only really be made when the costs are outweighed by the benefits, and this is only really verified when the costs of not using a module loader are greater than the costs when you do use a module loader. Costs of not using a module loader are incurred when you have a large enough body of code that loading all of it at the start of your app is detrimental to performance, user experience, or both.

> There is obvious merit here and helping us to define and understand > dependencies, and to provide clear references between them in the form of things > like require() calls, is certainly admirable.

The right solution is to use an AMD shim (AMD-Lite) module system to declare and resolve dependencies - but to use server-side bundling/minification of JavaScript.

When you do that, and when you use the AMD shim to only load your own script (not 3rd-party libraries) then the benefits are great.What's more, you can then use the browser's built-in asynchronous script loading if you so choose. Simply set the async attribute on the script tag. Document ready happens sooner and the factory functions will only get executed when all dependencies are satisfied.

And for some AMD shims, you can ask it if there are any unresolved dependencies. If you are lucky, you will get a hierarchical tree of dependencies printed to the console.

When you say, "the fact is the vast majority of our projects don't have such complicated intramodule dependencies to really justify this", I wish you put detail sentences. This sentence should be the introduction to a paragraph or section.

For example, what happens if a client makes a request that requires validation between two modules? ALSO, what are your modules? For us, a module may be a filter, which in turn is a part of a filter list renderer module for searching.

Without these tinier modules you limit faceted compositionof your UI. Also, you run into ppotential for other kinds of race conditions related to two librarians using the same names for things and the order of your script execution.

That all being said, there is one gross thing about (current) AMD module LOADERS and it is that they can't, to my knowledge, statically detect infinitely recursive module references.

Anyway, thanks for trying. You at least made me think a bit, even though I think you are a bit naive and trying to enforce your naive worldview on others, I still greatly appreciate the effort and courage to speak your mind.

Please try to fill out your ideas with real world examples and explanations of how you divide your code into modules.

You don't have to debug using compressed/preprocessed sources. I have never done so. Source maps do work very well (I have never had a problem with them so far), but I have yet to see a bug that cannot be reproduced using the original sources.

You wouldn't debug a C program without debugging symbols, would you ?

#2 Load order

Relying on load order is just bad practice, period. It is even worse when working with loader(s) that rely on module dependencies.

Even when working with plain script tags, relying on load order is just opening the doors to maintenance problems.

#3 Workflow complication

This is a non-problem IMO. Each module standard has its ecosystem with corresponding libraries. Most loaders provide a way to shim around other module standards, that you only have to setup once for your first project.

But they also solve a bunch of problems for you. And so do module loaders. I have never been so happy with maintaining my JS projects since they all use a module loader. I don't have to worry about manual dependency resolving (load order anyone ?...), about minification...

As a conclusion, working with loaders is a different workflow, that is for sure. But saying it is beneficial or harmful is nonsense to me. It is very beneficial in a lot of situations and projects. It MAY be harmful in some specific situations, but that's about it.

In the end, a tool is made to do what it has been made to do. You'll always have problems if you try to nail down staples with a screwdriver. But screwdrivers are awesome at driving screws.