Skip to content

Commit

Permalink
update to latest require.js, document pragmasOnSave and has build opt…
Browse files Browse the repository at this point in the history
…ions, flesh out http build example to send down just one script, have html test page.
  • Loading branch information
jrburke committed Aug 16, 2011
1 parent ad3ba61 commit 521a335
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 17 deletions.
40 changes: 39 additions & 1 deletion build/example.build.js
Expand Up @@ -113,11 +113,49 @@
//excludeStart/excludeEnd and includeStart/includeEnd work, and the
//the pragmas value to the includeStart or excludeStart lines
//is evaluated to see if the code between the Start and End pragma
//lines should be included or excluded.
//lines should be included or excluded. If you have a choice to use
//"has" code or pragmas, use "has" code instead. Pragmas are harder
//to read, but they can be a bit more flexible on code removal vs.
//has-based code, which must follow JavaScript language rules.
//Pragmas also remove code in non-minified source, where has branch
//trimming is only done if the code is minified via UglifyJS or
//Closure Compiler.
pragmas: {
fooExclude: true
},

//Same as "pragmas", but only applied once during the file save phase
//of an optimization. "pragmas" are applied both during the dependency
//mapping and file saving phases on an optimization. Some pragmas
//should not be processed during the dependency mapping phase of an
//operation, such as the pragma in the CoffeeScript loader plugin,
//which wants the CoffeeScript compiler during the dependency mapping
//phase, but once files are saved as plain JavaScript, the CoffeeScript
//compiler is no longer needed. In that case, pragmasOnSave would be used
//to exclude the compiler code during the save phase.
pragmasOnSave: {
//Just an example
excludeCoffeeScript: true
},

//Allows trimming of code branches that use has.js-based feature detection:
//https://github.com/phiggins42/has.js
//The code branch trimming only happens if minification with UglifyJS or
//Closure Compiler is done. For more information, see:
//http://requirejs.org/docs/optimization.html#hasjs
has: {
'function-bind': true,
'string-trim': false
},

//Similar to pragmasOnSave, but for has tests -- only applied during the
//file save phase of optimization, where "has" is applied to both
//dependency mapping and file save phases.
hasOnSave: {
'function-bind': true,
'string-trim': false
},

//Allows namespacing requirejs, require and define calls to a new name.
//This allows stronger assurances of getting a module space that will
//not interfere with others using a define/require AMD-based module
Expand Down
27 changes: 22 additions & 5 deletions build/tests/httpBuild.js → build/tests/http/httpBuild.js
@@ -1,7 +1,8 @@
/*jslint strict: false*/
/*global require: false, console: false */

var requirejs = require('../../r.js'),
//If you install requirejs via npm, replace this line with require('requirejs')
var requirejs = require('../../../r.js'),
http = require('http'),
fs = require('fs'),
host = '127.0.0.1',
Expand All @@ -10,11 +11,27 @@ var requirejs = require('../../r.js'),

//Set up the config passed to the optimizer
config = {
baseUrl: '../../../requirejs/tests',
baseUrl: 'scripts',
paths: {
//Put path to require.js in here, leaving off .js
//since it is a module ID path mapping. For final deployment,
//if a smaller AMD loader is desired, no dynamic
//loading needs to be done, and loader plugins are not
//in use, change this path to that file. One possibility
//could be the one at:
//https://github.com/ajaxorg/ace/blob/master/build_support/mini_require.js
requireLib: '../../../../../requirejs/require'
},
//Uncomment this line if uglify minification is not wanted.
//optimize: 'none',
name: 'one',
include: 'dimple',
out: 'builds/outSingleOpt.js'
//Specify the optimization target. Choose the requireLib,
//so that it is first in the output, then include the main.js
//for this project.
name: 'requireLib',
include: ['main'],
//Uncomment this if you want to debug three.js by itself
//excludeShallow: ['three'],
out: 'scripts/main-built.js'
};

function respond(res, code, contents) {
Expand Down
28 changes: 28 additions & 0 deletions build/tests/http/main.html
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>One script via RequireJS optimizer</h1>

<p><strong>Be sure to start up the server: node httpBuild.js</strong></p>

<p>Demonstrates how to use the RequireJS optimizer to only load
one script in the page using Node as the server to serve the optimized
script. Using the "excludeShallow" build config option inside httpBuild.js
allows debugging one script separate from the rest of the optimized file.</p>

<p>See httpBuild.js in the same directory as this file for more information.</p>

<hr>

<!-- Successful load will print something after the hr tag -->

<!-- httpBuild.js will return optimized content no matter what the URL
is below. For deployment, replace this script tag to one that
loads main-built.js, or whatever the config.out name value is
inside httpBuild.js -->
<script src="http://127.0.0.1:4304/main.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions build/tests/http/scripts/main.js
@@ -0,0 +1,18 @@

//Set the baseUrl for scripts, for use
//if individually debuggin files via
//excludeShallow in httpBuild.js
require.config({
baseUrl: 'scripts'
});

require(['one', 'two'], function (one, two) {
var html = "<b>Success!</b> One's name is: " + one.name +
", two's name is: " + two.name +
", three's name is: " + two.threeName,
node = document.createElement('div');

node.innerHTML = html;

document.getElementsByTagName('body')[0].appendChild(node);
});
3 changes: 3 additions & 0 deletions build/tests/http/scripts/one.js
@@ -0,0 +1,3 @@
define({
name: 'one'
});
5 changes: 5 additions & 0 deletions build/tests/http/scripts/three.js
@@ -0,0 +1,5 @@
define(function () {
return {
name: 'three'
};
});
7 changes: 7 additions & 0 deletions build/tests/http/scripts/two.js
@@ -0,0 +1,7 @@
define(function (require) {
var three = require('three');
return {
name: 'two',
threeName: three.name
};
});
10 changes: 9 additions & 1 deletion require.js
Expand Up @@ -1270,7 +1270,7 @@ var requirejs, require, define;
resume();
}
}
return undefined;
return context.require;
},

/**
Expand Down Expand Up @@ -1482,6 +1482,14 @@ var requirejs, require, define;
return context.require(deps, callback);
};

/**
* Support require.config() to make it easier to cooperate with other
* AMD loaders on globally agreed names.
*/
req.config = function (config) {
return req(config);
};

/**
* Export require as a global, but only if it does not already exist.
*/
Expand Down
20 changes: 10 additions & 10 deletions tests/node/embedded/main.js
@@ -1,13 +1,13 @@
var cs = require('coffee-script'),
requirejs = require('requirejs');

requirejs({
baseUrl: 'scripts',
nodeRequire: require
},
['./coffee/foo', 'bar'],
function (foo, bar) {
console.log('bar.data: ' + bar.data);
console.log('foo.name: ' + foo.name);
}
);
requirejs.config({
baseUrl: 'scripts',
nodeRequire: require
});

requirejs(['./coffee/foo', 'bar'],
function ( foo, bar) {
console.log('bar.data: ' + bar.data);
console.log('foo.name: ' + foo.name);
});

0 comments on commit 521a335

Please sign in to comment.