Permalink
Browse files

Add amdefine/intercept and rev to 0.1.0

  • Loading branch information...
1 parent a15c421 commit d6f9819dd32289137a71ba646802a9943092072c @jrburke committed Oct 15, 2013
View
@@ -10,7 +10,7 @@ requiring those other programs to use AMD.
```javascript
"dependencies": {
- "amdefine": ">=0.0.8"
+ "amdefine": ">=0.1.0"
}
```
@@ -39,6 +39,55 @@ with npm, then just download the latest release and refer to it using a relative
[Latest Version](https://github.com/jrburke/amdefine/raw/latest/amdefine.js)
+### amdefine/intercept
+
+Instead of pasting the piece of text for the amdefine setup of a `define`
+variable in each module you create or cosunme, you can use `amdefine/intercept`
+instead. It will automatically insert the above snippet in each .js file loaded
+by Node.
+
+**Warning**: you should only use this if you are creating an application that
+is consuming AMD style defined()'d modules that are distributed via npm and want
+to run that code in Node.
+
+For library code where you are not sure if it will be used by others in Node or
+in the browser, then explicitly depending on amdefine and placing the code
+snippet above is suggested path, instead of using `amdefine/intercept`. The
+intercept module affects all .js files loaded in the Node app, and it is
+inconsiderate to modify global state like that unless you are also controlling
+the top level app.
+
+#### Why distribute AMD-style nodes via npm?
+
+npm has a lot of weaknesses for front-end use (installed layout is not great,
+should have better support for the `baseUrl + moduleID + '.js' style of loading,
+single file JS installs), but some people want a JS package manager and are
+willing to live with those constraints. If that is you, but still want to author
+in AMD style modules to get dynamic require([]), better direct source usage and
+powerful loader plugin support in the browser, then this tool can help.
+
+#### amdefine/intercept usage
+
+Just require it in your top level app module (for example index.js, server.js):
+
+```javascript
+require('amdefine/intercept');
+```
+
+The module does not return a value, so no need to assign the result to a local
+variable.
+
+Then just require() code as you normally would with Node's require(). Any .js
+loaded after the intercept require will have the amdefine check injected in
+the .js source as it is loaded. It does not modify the source on disk, just
+prepends some content to the text of the module as it is loaded by Node.
+
+#### How amdefine/intercept works
+
+It overrides the `Module._extensions['.js']` in Node to automatically prepend
+the amdefine snippet above. So, it will affect any .js file loaded by your
+app.
+
## define() usage
It is best if you use the anonymous forms of define() in your module:
@@ -112,6 +161,7 @@ To run the tests, cd to **tests** and run:
```
node all.js
+node all-intercept.js
```
## License
View
@@ -1,5 +1,5 @@
/** vim: et:ts=4:sw=4:sts=4
- * @license amdefine 0.0.8 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
+ * @license amdefine 0.1.0 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/amdefine for details
*/
View
@@ -0,0 +1,36 @@
+/*jshint node: true */
+var inserted,
+ Module = require('module'),
+ fs = require('fs'),
+ existingExtFn = Module._extensions['.js'],
+ amdefineRegExp = /amdefine\.js/;
+
+inserted = "if (typeof define !== 'function') {var define = require('amdefine')(module)}";
+
+//From the node/lib/module.js source:
+function stripBOM(content) {
+ // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
+ // because the buffer-to-string conversion in `fs.readFileSync()`
+ // translates it to FEFF, the UTF-16 BOM.
+ if (content.charCodeAt(0) === 0xFEFF) {
+ content = content.slice(1);
+ }
+ return content;
+}
+
+//Also adapted from the node/lib/module.js source:
+function intercept(module, filename) {
+ var content = stripBOM(fs.readFileSync(filename, 'utf8'));
+
+ if (!amdefineRegExp.test(module.id)) {
+ content = inserted + content;
+ }
+
+ module._compile(content, filename);
+}
+
+intercept._id = 'amdefine/intercept';
+
+if (!existingExtFn._id || existingExtFn._id !== intercept._id) {
+ Module._extensions['.js'] = intercept;
+}
View
@@ -1,7 +1,7 @@
{
"name": "amdefine",
"description": "Provide AMD's define() API for declaring modules in the AMD format",
- "version": "0.0.8",
+ "version": "0.1.0",
"homepage": "http://github.com/jrburke/amdefine",
"author": "James Burke <jrburke@gmail.com> (http://github.com/jrburke)",
"licenses": [
@@ -0,0 +1,33 @@
+/*jslint strict: false, evil: true */
+/*global Packages: false, process: false, require: true, define: true, doh: false */
+
+//A hack to doh to avoid dojo setup stuff in doh/runner.js
+var skipDohSetup = true,
+ fs, vm, load, env;
+
+require('../intercept');
+
+(function () {
+ if (typeof Packages !== 'undefined') {
+ env = 'rhino';
+ } else if (typeof process !== 'undefined') {
+ env = 'node';
+
+ fs = require('fs');
+ vm = require('vm');
+
+ load = function (path) {
+ return vm.runInThisContext(fs.readFileSync(path, 'utf8'), path);
+ };
+ }
+
+}());
+
+//Load the tests.
+load("doh/runner.js");
+load('doh/_' + env + 'Runner.js');
+
+require("./basic-intercept/basic-tests");
+
+//Print out the final report
+doh.run();
@@ -0,0 +1,7 @@
+define(['./b', './sub/nested/d'], function (b, d) {
+ return {
+ name: 'a',
+ b: b,
+ d: d
+ };
+});
@@ -0,0 +1,3 @@
+define({
+ name: 'b'
+});
@@ -0,0 +1,16 @@
+doh.register(
+ "basicIntercept",
+ [
+ function basicIntercept(t){
+ var a = require('./a');
+
+ t.is('a', a.name);
+ t.is('b', a.b.name);
+ t.is('d', a.d.name);
+ t.is('c', a.d.cName);
+ t.is('e', a.d.e.name);
+ }
+ ]
+);
+
+doh.run();
@@ -0,0 +1,10 @@
+define(function (require, exports, module) {
+
+ //A fake out, modify the exports, but still prefer the
+ //return value as the module value.
+ exports.name = 'badc';
+
+ return {
+ name: 'c'
+ };
+});
@@ -0,0 +1,10 @@
+define(function (require, exports, module) {
+ var c = require('../c'),
+ e = require('./e');
+
+ return {
+ name: 'd',
+ e: e,
+ cName: c.name
+ };
+});
@@ -0,0 +1,4 @@
+//Just testing a plain exports case.
+define(function (require, exports) {
+ exports.name = 'e';
+});
Oops, something went wrong.

0 comments on commit d6f9819

Please sign in to comment.