Clone wiki

MinCat / Dependencies

JavaScript Dependency Management

MinCat's @require directive, gives scripts the ability to declare their own dependencies. When MinCat encounters this directive, it will begin building a dependency graph, collecting and sorting all of the scripts necessary to load its current target.

Required files are free to require other files. MinCat will follow the chain of required files all the way to the end.

The order in which MinCat concatenates these files is dictated by the source graph. For instance, consider these three files:

Dependencies

When MinCat operates on this collection of files, it will produce a single minimized file: a.min.js. The b.js and c.js files will not be minimized because they contains the /* skip minimize */ directive.

The resulting a.min.js would contain the contents of c.js, b.js, and a.js, in that order. Even though b.js and c.js are not minimized by themselves, they are still minimized as part of a.min.js.

The order here is specified because b.js contains a @require directive that includes c.js. If b.js lacked this directive, the ordering of b.js and c.js would be undefined. However, both would still be guaranteed to appear before a.js, since a.js requires both of them.

Note also that c.js is included only once in a.min.js, even though both a.js and b.js require it. MinCat always filters its dependency graph so that it deals with a distinct list of scripts.

Intellisense

Visual Studio has extensive intellisense support for JavaScript files. Like MinCat, Visual Studio scans the target JavaScript file for a specially-formed comment, the <references /> directive. Also like MinCat, Visual Studio uses this directive to build a graph of the current file's dependencies.

However, while these two directives serve a similar purpose, they are used in very different, incompatible ways. Specifically, the <references /> directive is allowed (and often desired) to build self-referential structures. That is, as far as intellisense is concerned, it is valid for a.js to reference b.js and for b.js to reference a.js, forming a reference loop.

This sort of structure is useful for Visual Studio to provide complete intellisense in both files. Unfortunately, this same structure makes it impossible for MinCat to determine which file should appear first: they both require one another. While this is not a problem for intellisense, MinCat requires a less ambiguous situation.

For this reason, MinCat's @require directive is intentionally separate from Visual Studio's similar <references /> directive.

Updated