Domanda

Our company has been using newer versions of the Dojo framework, which have progressed to an AMD-based loader format. I'm currently trying to find logical ways to separate layer files, taking a module and all its dependencies, and wrapping them all into single minified files. My goals are generally as follows:

  • Minimize the number of separate requests made per-page
  • When possible, make use of cache so that large modules that have already been used are re-retrieved.

Our app consists of many hundreds of pages and dialogs, much of which have their own custom logic and related scripts.

I'm starting to run into more and more complex issues in terms of finding the right way to construct layer files for the best performance. Just recently, I ran into the following type of scenario:

A depends on B, depends on C, depends on D ...etc... depends on Z.

A1 depends on D (which depends on ... Z)

A2 depends on L (which depends on ... Z)

The scenario is mostly just an example - I don't have 24 dependencies in a straight tree. But under our previous method of simply declaring all objects globally under a namespace, this would be a simple matter of minifying everything to one layer file that's included on relevant pages. As it stands now, I'm not sure what I'd gain by making D its own layer, or A, or L. Just to avoid loading the same code twice, we'd be separating it into about 3 or 4 requests. One troublesome fact is the way that the page's script will always immediately put out new script requests for anything in its "require()" block that it hasn't cached just yet. Moreover, it's become very complicated just to comprehend and discuss.

I wanted to know how other people tend to approach this problem, hopefully in a simpler way; the previous version of our product had some significant javascript loading-time issues, so this certainly isn't premature optimization (it could be "past-mature"). We are starting to switch our pages to using asynchronously-loaded script tags now that most of our scripts support it, in case that's any help.

È stato utile?

Soluzione

This is not really an answer, it is just a longer comment

For inspiration I'd suggest you to look at (1) the https://github.com/componentjs/component project which aimed to simplify management of dependencies between JavaScript packages (and their packaging and minification). Since being born 2 years ago it somehow settled down and can offer some solution.

Another module management best practices I'd suggest you to look at (2) are those used and recommended for use together with TypeScript. Some discussion of pros/cons of using internal/external modules is available in https://typescript.codeplex.com/discussions/507413

Compile-time creation of 1 minified script per page gets my vote, lazy loading big scripts on-demand possible but rather an exception to the rule.

This is how I tend to approach the problem

Some interesting further reading:

Altri suggerimenti

(Self-answer) One thing I've been trying lately, and with moderate success, is making separate boot layers. Since we can at least be certain that any particular block of JavaScript will either all execute, or not execute at all, and it needs to be available for any define/requires to work, it's a pretty good place to put all of the main "library modules" for any particular user type.

From there, I've been making minified layers for every "page script", excluding any modules already written in one of the user type boot scripts. There's still a lot of manual work, and in fact some restructuring. For performance, I need to have the login page for each user type request their "layer" before they get to their homepage (each script totals over a megabyte - not a small app). It seems to be fitting my needs so far, but hasn't gone through any actual performance test.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top