From 521a33510a6fd9b34a239341ca1a3bd9257e0da4 Mon Sep 17 00:00:00 2001 From: jrburke Date: Tue, 16 Aug 2011 15:01:17 -0700 Subject: [PATCH] update to latest require.js, document pragmasOnSave and has build options, flesh out http build example to send down just one script, have html test page. --- build/example.build.js | 40 ++++++++++++++++++++++++++++- build/tests/{ => http}/httpBuild.js | 27 +++++++++++++++---- build/tests/http/main.html | 28 ++++++++++++++++++++ build/tests/http/scripts/main.js | 18 +++++++++++++ build/tests/http/scripts/one.js | 3 +++ build/tests/http/scripts/three.js | 5 ++++ build/tests/http/scripts/two.js | 7 +++++ require.js | 10 +++++++- tests/node/embedded/main.js | 20 +++++++-------- 9 files changed, 141 insertions(+), 17 deletions(-) rename build/tests/{ => http}/httpBuild.js (55%) create mode 100644 build/tests/http/main.html create mode 100644 build/tests/http/scripts/main.js create mode 100644 build/tests/http/scripts/one.js create mode 100644 build/tests/http/scripts/three.js create mode 100644 build/tests/http/scripts/two.js diff --git a/build/example.build.js b/build/example.build.js index a8500a9b..47657fe2 100644 --- a/build/example.build.js +++ b/build/example.build.js @@ -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 diff --git a/build/tests/httpBuild.js b/build/tests/http/httpBuild.js similarity index 55% rename from build/tests/httpBuild.js rename to build/tests/http/httpBuild.js index 04c5f39e..16e74f54 100644 --- a/build/tests/httpBuild.js +++ b/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', @@ -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) { diff --git a/build/tests/http/main.html b/build/tests/http/main.html new file mode 100644 index 00000000..3186149c --- /dev/null +++ b/build/tests/http/main.html @@ -0,0 +1,28 @@ + + + + + + +

One script via RequireJS optimizer

+ +

Be sure to start up the server: node httpBuild.js

+ +

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.

+ +

See httpBuild.js in the same directory as this file for more information.

+ +
+ + + + + + + diff --git a/build/tests/http/scripts/main.js b/build/tests/http/scripts/main.js new file mode 100644 index 00000000..e038f4d0 --- /dev/null +++ b/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 = "Success! 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); +}); diff --git a/build/tests/http/scripts/one.js b/build/tests/http/scripts/one.js new file mode 100644 index 00000000..15483928 --- /dev/null +++ b/build/tests/http/scripts/one.js @@ -0,0 +1,3 @@ +define({ + name: 'one' +}); diff --git a/build/tests/http/scripts/three.js b/build/tests/http/scripts/three.js new file mode 100644 index 00000000..010b62f3 --- /dev/null +++ b/build/tests/http/scripts/three.js @@ -0,0 +1,5 @@ +define(function () { + return { + name: 'three' + }; +}); diff --git a/build/tests/http/scripts/two.js b/build/tests/http/scripts/two.js new file mode 100644 index 00000000..6da4c2aa --- /dev/null +++ b/build/tests/http/scripts/two.js @@ -0,0 +1,7 @@ +define(function (require) { + var three = require('three'); + return { + name: 'two', + threeName: three.name + }; +}); diff --git a/require.js b/require.js index a78986d8..0372c99e 100644 --- a/require.js +++ b/require.js @@ -1270,7 +1270,7 @@ var requirejs, require, define; resume(); } } - return undefined; + return context.require; }, /** @@ -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. */ diff --git a/tests/node/embedded/main.js b/tests/node/embedded/main.js index ead4f2c4..b351859a 100644 --- a/tests/node/embedded/main.js +++ b/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); +});