Practices for organizing JavaScript AMD imports

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.

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:

  • Erich Gamma’s build 2014 presentation Microsoft: Surviving Application Scale JavaScript – TypeScript in the Trenches has few chapters devoted to code organization
  • (Yakov Fain, Victor Rasputnis, Viktor Gamov, and Anatole Tartakovsky)’s online book “Enterprise Web Development. From Desktop to Mobile” discusses module organization in Chapter 6 – Modularizing Large-Scale JavaScript Projects
  • Axel Rauschmayer in ECMAScript 6 modules: the final syntax shows how modules are supposed to be used in the upcoming JavaScript #6 (highlights: static module structure, support for cyclic dependencies)
  • Addy Osmani in Scalable JavaScript Design Patterns talks about modules, design patterns and large-scale JavaScript application architecture

1

(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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Practices for organizing JavaScript AMD imports

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.

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:

  • Erich Gamma’s build 2014 presentation Microsoft: Surviving Application Scale JavaScript – TypeScript in the Trenches has few chapters devoted to code organization
  • (Yakov Fain, Victor Rasputnis, Viktor Gamov, and Anatole Tartakovsky)’s online book “Enterprise Web Development. From Desktop to Mobile” discusses module organization in Chapter 6 – Modularizing Large-Scale JavaScript Projects
  • Axel Rauschmayer in ECMAScript 6 modules: the final syntax shows how modules are supposed to be used in the upcoming JavaScript #6 (highlights: static module structure, support for cyclic dependencies)
  • Addy Osmani in Scalable JavaScript Design Patterns talks about modules, design patterns and large-scale JavaScript application architecture

1

(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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật