Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Jul 31, 2012
0 parents commit 1ae4524
Show file tree
Hide file tree
Showing 21 changed files with 27,159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
www-built
www-ghpages
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.

## Project layout

This is accomplished by using the following project layout. This repo has
the following structure:

* 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:

* www/
* 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 where to combine the common modules.
* page1.js - loads the common module, then loads 'app/main1', the
main module for page 1. This module is used for the data-main for
page1.html.
* page2.js - loads the common module, then loads 'app/main2', the
main module for page 2. This module is used for the data-main for
page2.html.

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

Common modules are added to the built common.js by modifying the "include"
array in tools/build.js for the common layer. So, as you do builds and see
in the build output that each page is including the same module, add it
to the x.

It is better to add these common modules to the tools/build.js config insted of
doing a require([]) call for them in js/common.js. Modules that are not
explicitly required are not executed, so by usint tools/build.js, you can
include common modules that may be in 2-3 pages but not all pages, and for
pages that do not need the module, while it will be downloaded in the built
common.js file, it will not be executed. If you put in a require() call for it
in common.js, then it would 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
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"amd": {},
"volo": {
"baseUrl": "www/js/lib",
"dependencies": {
"jquery": "github:jquery/jquery/1.7.2"
}
}
}
46 changes: 46 additions & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
appDir: '../www',
baseUrl: 'js/lib',
paths: {
app: '../app'
},
dir: '../www-built',
modules: [
//First set up the common build layer.
{
//module names are relative to baseUrl
name: '../common',
//List common dependencies here. Only need to list
//top level dependencies, "include" will find
//nested dependencies.
include: ['jquery',
'app/lib',
'app/controller/Base',
'app/model/Base'
]
},

//Now set up a build layer for each page, but exclude
//the common one. "exclude" will exclude nested
//the nested, built dependencies from "common". Any
//"exclude" that includes built modules should be
//listed before the build layer that wants to exclude it.
//"include" the appropriate "app/main*" module since by default
//it will not get added to the build since it is loaded by a nested
//require in the page*.js files.
{
//module names are relative to baseUrl/paths config
name: '../page1',
include: ['app/main1'],
exclude: ['../common']
},

{
//module names are relative to baseUrl
name: '../page2',
include: ['app/main2'],
exclude: ['../common']
}

]
}
Loading

0 comments on commit 1ae4524

Please sign in to comment.