requirejs-memfiles
Advanced tools
Comparing version 2.1.15-1 to 2.1.15-2
73
index.js
@@ -1,2 +0,2 @@ | ||
var fileApi, files, prim, | ||
var fileApi, files, mutexLock, prim, queue, | ||
path = require( "path" ), | ||
@@ -119,4 +119,46 @@ requirejs = require( "requirejs" ); | ||
queue = []; | ||
function enqueue() { | ||
queue.push( arguments ); | ||
if ( queue.length === 1 ) { | ||
run(); | ||
} | ||
} | ||
function dequeue() { | ||
queue.shift(); | ||
run(); | ||
} | ||
function run() { | ||
var args = queue[ 0 ]; | ||
if ( args !== undefined ) { | ||
setFiles.apply( {}, args ); | ||
} | ||
} | ||
function setFiles( _files, callback ) { | ||
if ( mutexLock ) { | ||
return callback( new Error( "Internal bug: concurrent calls not supported" ) ); | ||
} | ||
mutexLock = true; | ||
requirejs.define( "node/file", [ "prim" ], function( _prim ) { | ||
prim = _prim; | ||
return fileApi; | ||
}); | ||
files = _files; | ||
callback(function() { | ||
files = {}; | ||
mutexLock = false; | ||
dequeue(); | ||
}); | ||
} | ||
/** | ||
* requirejs.setFiles( files ) | ||
* requirejs.setFiles( files, callback ) | ||
* | ||
@@ -130,13 +172,24 @@ * @files [Object] containing (path, data) key-value pairs, e.g.: | ||
* | ||
* @callback [Function] called with one argument: a callback function that must be called when | ||
* use is complete. | ||
* | ||
* Example: | ||
* ```javascript | ||
* requirejs.setFiles( files, function( done ) { | ||
* requirejs.optmize( config, function() { | ||
* doSomething(); | ||
* done(); | ||
* }, function( error ) { | ||
* doSomething( error ); | ||
* done(); | ||
* }); | ||
* }); | ||
* | ||
* ``` | ||
* | ||
*/ | ||
requirejs.setFiles = function( _files ) { | ||
requirejs.define( "node/file", [ "prim" ], function( _prim ) { | ||
prim = _prim; | ||
return fileApi; | ||
}); | ||
files = _files; | ||
requirejs.setFiles = function( files, callback ) { | ||
enqueue( files, callback ); | ||
}; | ||
module.exports = requirejs; |
{ | ||
"name": "requirejs-memfiles", | ||
"version": "2.1.15-1", | ||
"version": "2.1.15-2", | ||
"main": "index.js", | ||
@@ -25,2 +25,3 @@ "repository": { | ||
"devDependencies": { | ||
"async": "0.9.0", | ||
"chai": "1.10.x", | ||
@@ -27,0 +28,0 @@ "jshint": "2.6.x", |
122
test/rjs.js
@@ -0,1 +1,2 @@ | ||
var async = require( "async" ); | ||
var expect = require( "chai" ).expect; | ||
@@ -5,27 +6,106 @@ var requirejs = require( "../index.js" ); | ||
describe( "requirejs.optimize", function() { | ||
var files = { | ||
"a.js": "define([\"./b\"], function( b ) { return b; });", | ||
"b.js": "define(function() { return \"B\"; });" | ||
}; | ||
it( "should build dist/output.js", function( done ) { | ||
var files = { | ||
"a.js": "define([\"./b\"], function( b ) { return b; });", | ||
"b.js": "define(function() { return \"B\"; });" | ||
}; | ||
requirejs.setFiles( files ); | ||
requirejs.optimize({ | ||
appDir: ".", | ||
baseUrl: ".", | ||
dir: "dist", | ||
modules: [{ | ||
name: "output", | ||
include: "a", | ||
create: true | ||
}] | ||
}, function() { | ||
expect( files ).to.include.keys( "dist/output.js" ); | ||
expect( files[ "dist/output.js" ] ).to.be.equal( "define(\"b\",[],function(){return\"B\"}),define(\"a\",[\"./b\"],function(e){return e}),define(\"output\",function(){});" ); | ||
done(); | ||
}, function( error ) { | ||
expect( error ).to.be.null; | ||
before(function( doneSetup ) { | ||
requirejs.setFiles( files, function( doneRequirejsMemfiles ) { | ||
requirejs.optimize({ | ||
appDir: ".", | ||
baseUrl: ".", | ||
dir: "dist", | ||
modules: [{ | ||
name: "output", | ||
include: "a", | ||
create: true | ||
}] | ||
}, function() { | ||
doneSetup(); | ||
doneRequirejsMemfiles(); | ||
}, function( error ) { | ||
doneSetup( error ); | ||
doneRequirejsMemfiles(); | ||
}); | ||
}); | ||
}); | ||
it( "should build dist/output.js", function() { | ||
expect( files ).to.include.keys( "dist/output.js" ); | ||
expect( files[ "dist/output.js" ] ).to.be.equal( "define(\"b\",[],function(){return\"B\"}),define(\"a\",[\"./b\"],function(e){return e}),define(\"output\",function(){});" ); | ||
}); | ||
}); | ||
describe( "concurrent requirejs.optimize calls", function() { | ||
var filesA, filesB; | ||
filesA = { | ||
"a.js": "define([\"./b\"], function( b ) { return b; });", | ||
"b.js": "define(function() { return \"B\"; });" | ||
}; | ||
filesB = { | ||
"a.js": "define([\"./b\"], function( b ) { return b; });", | ||
"b.js": "define(function() { return \"B\"; });" | ||
}; | ||
before(function( doneSetup ) { | ||
async.parallel([ | ||
function( callback ) { | ||
requirejs.setFiles( filesA, function( doneRequirejsMemfiles ) { | ||
requirejs.optimize({ | ||
appDir: ".", | ||
baseUrl: ".", | ||
dir: "dist", | ||
modules: [{ | ||
name: "outputA", | ||
include: "a", | ||
create: true | ||
}] | ||
}, function() { | ||
callback(); | ||
doneRequirejsMemfiles(); | ||
}, function( error ) { | ||
callback( error ); | ||
doneRequirejsMemfiles(); | ||
}); | ||
}); | ||
}, | ||
function( callback ) { | ||
requirejs.setFiles( filesB, function( doneRequirejsMemfiles ) { | ||
requirejs.optimize({ | ||
appDir: ".", | ||
baseUrl: ".", | ||
dir: "dist", | ||
modules: [{ | ||
name: "outputB", | ||
include: "a", | ||
create: true | ||
}] | ||
}, function() { | ||
callback(); | ||
doneRequirejsMemfiles(); | ||
}, function( error ) { | ||
callback( error ); | ||
doneRequirejsMemfiles(); | ||
}); | ||
}); | ||
} | ||
], function( error ) { | ||
doneSetup( error ); | ||
}); | ||
}); | ||
it( "should build dist/outputA.js", function() { | ||
expect( filesA ).to.include.keys( "dist/outputA.js" ); | ||
expect( filesA ).to.not.include.keys( "dist/outputB.js" ); | ||
expect( filesA[ "dist/outputA.js" ] ).to.be.equal( "define(\"b\",[],function(){return\"B\"}),define(\"a\",[\"./b\"],function(e){return e}),define(\"outputA\",function(){});" ); | ||
}); | ||
it( "should build dist/outputB.js", function() { | ||
expect( filesB ).to.include.keys( "dist/outputB.js" ); | ||
expect( filesB ).to.not.include.keys( "dist/outputA.js" ); | ||
expect( filesB[ "dist/outputB.js" ] ).to.be.equal( "define(\"b\",[],function(){return\"B\"}),define(\"a\",[\"./b\"],function(e){return e}),define(\"outputB\",function(){});" ); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12570
258
4