• Stars
    star
    840
  • Rank 54,265 (Top 2 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example RequireJS-based project that has multiple pages that share a common set of modules.

requirejs/example-multipage

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

  • Each page uses a mix of common and page-specific modules.
  • All pages share the same requirejs config.
  • After an optimization build, the common items should be in a shared common layer, and the page-specific modules should be in a page-specific layer.
  • The HTML page should not have to be changed after doing the build.

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:

  • tools: The requirejs optimizer, r.js, and the optimizer config, build.js.
  • www: The code that runs in the browser while in development mode.
  • www-built: Generated after an optimizer build. Contains the built code that can be deployed to the live site.

This www has the following layout:

  • page1.html: page 1 of the app.
  • page2.html: page 2 of the app.
  • js
    • app: the directory to store app-specific modules.
    • lib: the directory to hold third party modules, like jQuery.
    • common.js: contains the requirejs config, and it will be the build target for the set of common modules.
    • page1.js: used for the data-main for page1.html. Loads the common module, then loads app/main1, the main module for page 1.
    • page2.js: used for the data-main for page2.html. Loads the common module, then loads app/main2, the main module for page 2.

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

More Repositories

1

requirejs

A file and module loader for JavaScript
JavaScript
12,930
star
2

r.js

Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
JavaScript
2,568
star
3

almond

A minimal AMD API implementation for use after optimized builds
JavaScript
2,414
star
4

text

An AMD loader plugin for loading text resources
JavaScript
932
star
5

example-multipage-shim

Example RequireJS-based project that has multiple pages that share a common set of modules with shim config
JavaScript
382
star
6

require-cs

An AMD loader plugin for CoffeeScript
JavaScript
335
star
7

require-jquery

A RequireJS+jQuery sample project
JavaScript
261
star
8

alameda

AMD loader, like requirejs, but with promises and for modern browsers
JavaScript
195
star
9

example-jquery-shim

Example project that uses jQuery and jQuery plugins via shim config
JavaScript
190
star
10

domReady

An AMD loader plugin for detecting DOM ready
JavaScript
182
star
11

example-jquery-cdn

Example project that uses jQuery and jQuery plugins wrapped as modules
JavaScript
151
star
12

cajon

JavaScript module loader for the browser that can load CommonJS/node and AMD modules
JavaScript
117
star
13

i18n

An AMD loader plugin for loading internationalization/localization string resources
JavaScript
98
star
14

xrayquire

An auxillary script that gives extra info on the use of requirejs in a web page.
JavaScript
74
star
15

example-libglobal

Use AMD modules to develop a JS library that builds to a lib that can be used by "globals only" scripts or as an AMD module
JavaScript
61
star
16

element

An AMD loader plugin for custom elements
JavaScript
56
star
17

requirejs-bower

Repo hold assets for publishing to the bower package manager.
JavaScript
34
star
18

requirejs-npm

Wrappers to allow using RequireJS-related scripts
JavaScript
22
star
19

step

AMD loader plugin that takes a step config to know how to sequential load some files
JavaScript
20
star
20

prim

Promise lib for use inside requirejs-related projects
JavaScript
7
star
21

requirejs-branding

Branding/images/design for RequireJS web site
CSS
6
star
22

browsertests

Standalone browser capability tests to help inform what is possible in a loader
PHP
3
star
23

alameda-prim

The alameda AMD loader that includes private promise shim
JavaScript
3
star
24

requirejs.github.io

requirejs.org site
HTML
2
star
25

requirejs-nuget

Artifact used for pushing to NuGet
JavaScript
2
star