Home

Awesome

requirejs/example-multipage

This project shows how to set up a multi-page requirejs-based project that has the following goals:

If you want to use shim config, for instance to load Backbone, see the requirejs/example-multipage-shim example instead. This project will not work well with shim config. This project works best when all the dependencies are AMD modules.

Getting this project template

If you are using volo:

volo create projectname requirejs/example-multipage

Otherwise, download latest zipball of master.

Project layout

This project has the following layout:

This www has the following layout:

To optimize, run:

node tools/r.js -o tools/build.js

That build command creates an optimized version of the project in a www-built directory. The js/common.js file will contain all the common modules. js/page1.js will contain the page1-specific modules, js/page2.js will contain the page2-specific modules.

Building up the common layer

As you do builds and see in the build output that each page is including the same module, add it to common's "include" array in tools/build.js.

It is better to add these common modules to the tools/build.js config instead of doing a requirejs([]) call for them in js/common.js. Modules that are not explicitly required at runtime are not executed when added to common.js via the include build option. So by using tools/build.js, you can include common modules that may be in 2-3 pages but not all pages. For pages that do not need a particular common module, it will not be executed. If you put in a requirejs() call for it in js/common.js, then it will always be executed.

More info

For more information on the optimizer: http://requirejs.org/docs/optimization.html

For more information on using requirejs: http://requirejs.org/docs/api.html