kopia lustrzana https://github.com/c9/core
Revert "Revert "Revert "use amd-loader from npm"""
rodzic
ed2eaf86c2
commit
6ff908b5ff
|
@ -0,0 +1,3 @@
|
|||
.c9revisions/
|
||||
amd-loader-*.tgz
|
||||
package/
|
|
@ -0,0 +1,4 @@
|
|||
.git/
|
||||
.c9revisions/
|
||||
package/
|
||||
amd-loader-*.tgz
|
|
@ -0,0 +1,6 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- 0.4
|
||||
- 0.5
|
||||
- 0.6
|
||||
- 0.10
|
|
@ -0,0 +1,21 @@
|
|||
The MIT License
|
||||
|
||||
Copyright (c) 2010 ajax.org B.V (Fabian Jakobs)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
|
@ -0,0 +1,73 @@
|
|||
AMD loader for node.js
|
||||
======================
|
||||
|
||||
node-amd-loader adds the capability to load unmodified AMD (Asynchronous Module DefinitionAsynchronous Module Definition) from node.js applications.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
`node-amd-loader` can be easily installed using [npm](http://npmjs.org).
|
||||
|
||||
npm install amd-loader
|
||||
|
||||
Before being able to load AMD modules the `amd-loader` module has to be required.
|
||||
|
||||
require("amd-loader");
|
||||
|
||||
This needs to be done only once.
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
### load modules which use AMD define() ###
|
||||
|
||||
Load modules which are written using AMD [define](http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition#define.28.29_function) from node.js node.
|
||||
|
||||
amd.js
|
||||
|
||||
```javascript
|
||||
define(function(require, exports, module) {
|
||||
exports.B = "B";
|
||||
});
|
||||
```
|
||||
|
||||
main.js
|
||||
|
||||
```
|
||||
require("amd-loader");
|
||||
var amd = require("./amd");
|
||||
```
|
||||
|
||||
### support requireJS asyncronous loading syntax ###
|
||||
|
||||
From within an AMD modules the async require syntax introduced by [requireJS](http://requirejs.org) can be used.
|
||||
|
||||
```javascript
|
||||
require(["fs"], function(fs) {
|
||||
fs.readFile(...);
|
||||
})
|
||||
```
|
||||
|
||||
### support requireJS text plugin ###
|
||||
|
||||
From within an AMD module the requireJS text plugin is supported.
|
||||
|
||||
```javascript
|
||||
var readme = require("text!./readme.md");
|
||||
```
|
||||
|
||||
Continuous Integration status
|
||||
-----------------------------
|
||||
|
||||
This project is tested with [Travis CI](http://travis-ci.org)
|
||||
[![Build Status](https://secure.travis-ci.org/ajaxorg/node-amd-loader.png)](http://travis-ci.org/ajaxorg/node-amd-loader)
|
||||
|
||||
Credits
|
||||
-------
|
||||
|
||||
[Kris Zip](https://github.com/kriszyp) came up the the initial [idea](https://gist.github.com/650000) how to hijack the node module loading.
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
MIT license. See the LICENSE file for details.
|
|
@ -0,0 +1,85 @@
|
|||
var fs = require("fs");
|
||||
var Module = require("module");
|
||||
|
||||
var moduleStack = [];
|
||||
var defaultCompile = module.constructor.prototype._compile;
|
||||
|
||||
module.constructor.prototype._compile = function(content, filename){
|
||||
moduleStack.push(this);
|
||||
try {
|
||||
return defaultCompile.call(this, content, filename);
|
||||
}
|
||||
finally {
|
||||
moduleStack.pop();
|
||||
}
|
||||
};
|
||||
|
||||
global.define = function (id, injects, factory) {
|
||||
var DEFAULT_INJECTS = ["require", "exports", "module"];
|
||||
|
||||
// infer the module
|
||||
var currentModule = moduleStack[moduleStack.length-1];
|
||||
var mod = currentModule || module.parent || require.main;
|
||||
|
||||
// assign arguments
|
||||
if (arguments.length === 1) {
|
||||
factory = id;
|
||||
injects = DEFAULT_INJECTS;
|
||||
id = null;
|
||||
}
|
||||
else if (arguments.length === 2) {
|
||||
factory = injects;
|
||||
injects = id;
|
||||
id = null;
|
||||
}
|
||||
|
||||
if (typeof id === "string" && id !== mod.id) {
|
||||
throw new Error("Can not assign module to a different id than the current file");
|
||||
}
|
||||
|
||||
var req = function(module, relativeId, callback) {
|
||||
if (Array.isArray(relativeId)) {
|
||||
// async require
|
||||
return callback.apply(this, relativeId.map(req))
|
||||
}
|
||||
|
||||
var chunks = relativeId.split("!");
|
||||
var prefix;
|
||||
if (chunks.length >= 2) {
|
||||
prefix = chunks[0];
|
||||
relativeId = chunks.slice(1).join("!");
|
||||
}
|
||||
|
||||
var fileName = Module._resolveFilename(relativeId, module);
|
||||
if (Array.isArray(fileName))
|
||||
fileName = fileName[0];
|
||||
|
||||
if (prefix && prefix.indexOf("text") !== -1) {
|
||||
return fs.readFileSync(fileName, "utf8");
|
||||
} else
|
||||
return require(fileName);
|
||||
}.bind(this, mod);
|
||||
|
||||
id = mod.id;
|
||||
if (typeof factory !== "function") {
|
||||
// we can just provide a plain object
|
||||
return mod.exports = factory;
|
||||
}
|
||||
|
||||
var returned = factory.apply(mod.exports, injects.map(function (injection) {
|
||||
switch (injection) {
|
||||
// check for CommonJS injection variables
|
||||
case "require": return req;
|
||||
case "exports": return mod.exports;
|
||||
case "module": return mod;
|
||||
default:
|
||||
// a module dependency
|
||||
return req(injection);
|
||||
}
|
||||
}));
|
||||
|
||||
if (returned) {
|
||||
// since AMD encapsulates a function/callback, it can allow the factory to return the exports.
|
||||
mod.exports = returned;
|
||||
}
|
||||
};
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name" : "amd-loader",
|
||||
"version" : "0.0.5",
|
||||
"description" : "Add the capability to load AMD (Asynchronous Module Definition) modules to node.js",
|
||||
"author": "ajax.org B.V. <info@ajax.org>",
|
||||
"contributors": [
|
||||
{ "name": "Fabian Jakobs", "email": "fabian@ajax.org" }
|
||||
],
|
||||
"repository" : {
|
||||
"type" : "git",
|
||||
"url" : "http://github.com/ajaxorg/node-amd-loader.git"
|
||||
},
|
||||
"main": "./amd-loader.js",
|
||||
"scripts" : {
|
||||
"test" : "node test/test.js && node test/test2.js"
|
||||
},
|
||||
"engines" : {
|
||||
"node" : ">= 0.4.11"
|
||||
},
|
||||
"licenses" : [{
|
||||
"type" : "MIT",
|
||||
"url" : "http://github.com/ajaxorg/node-amd-loader/raw/master/LICENSE"
|
||||
}]
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
define(function(require, exports, module) {
|
||||
|
||||
exports.B = require("./b").B;
|
||||
exports.D = require("d").D;
|
||||
exports.A = "A";
|
||||
|
||||
exports.text = require("text!./c.txt");
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
define(function(require, exports, module) {
|
||||
exports.B = "B";
|
||||
});
|
|
@ -0,0 +1 @@
|
|||
hello world
|
|
@ -0,0 +1,3 @@
|
|||
define(function(require, exports, module) {
|
||||
exports.D = "D";
|
||||
});
|
|
@ -0,0 +1,6 @@
|
|||
require("../..");
|
||||
|
||||
define(function(require, exports, module) {
|
||||
exports.A = require("./a").A;
|
||||
exports.E = "E";
|
||||
});
|
|
@ -0,0 +1,34 @@
|
|||
require("..");
|
||||
var assert = require("assert");
|
||||
|
||||
console.log("Running amd-loader tests");
|
||||
console.log("========================\n");
|
||||
|
||||
console.log("resolve relative id");
|
||||
var a = require("./node_modules/a");
|
||||
var b = require("./node_modules/b");
|
||||
|
||||
assert.equal(a.A, "A");
|
||||
assert.equal(a.B, "B");
|
||||
assert.equal(a.text, "hello world");
|
||||
assert.equal(a.D, "D");
|
||||
|
||||
assert.equal(b.B, "B");
|
||||
|
||||
console.log("resolve fully qualified id");
|
||||
var a = require("a");
|
||||
var b = require("b");
|
||||
|
||||
assert.equal(a.A, "A");
|
||||
assert.equal(a.B, "B");
|
||||
assert.equal(a.text, "hello world");
|
||||
assert.equal(b.B, "B");
|
||||
|
||||
console.log("resolve from node_modules");
|
||||
var d = require("d");
|
||||
|
||||
assert.equal(d.D, "D");
|
||||
|
||||
// TODO
|
||||
// node_modules + package
|
||||
// async require
|
|
@ -0,0 +1,4 @@
|
|||
// this tests the case where the AMD-loader is not loaded in the main module and
|
||||
// the module using amd-loader uses define itself
|
||||
console.log("require file with amd-loader and define")
|
||||
require("e");
|
|
@ -4,7 +4,7 @@
|
|||
require("amd-loader");
|
||||
try {
|
||||
require("heapdump");
|
||||
} catch (e) {}
|
||||
} catch(e) {}
|
||||
|
||||
var path = require("path");
|
||||
var architect = require("architect");
|
||||
|
|
Ładowanie…
Reference in New Issue