Skip to content

Commit

Permalink
Possibility to update the packagesMap config
Browse files Browse the repository at this point in the history
Introduction of an API (`noder.updatePackagesMap`) that allows to update the packagesMap after noder has been already initialized.

Close #21
  • Loading branch information
flongo committed Jul 17, 2014
1 parent 23af778 commit 8bb89a9
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 54 deletions.
14 changes: 11 additions & 3 deletions build/visitors/NoderMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ var NoderMap = function(cfg) {
this.starStarCompress = cfg.hasOwnProperty('starStarCompress') ? cfg.starStarCompress : ['**/*'];
this.noderConfig = cfg.noderConfig;
this.noderContext = cfg.noderContext;
this.varName = cfg.varName || "noder";
this.toFile = cfg.toFile;
};

NoderMap.prototype.onBeforeBuild = function(packaging) {
configUtils.planBootstrapFileRebuild(packaging, this.noderConfig);
if (!this.toFile) {
configUtils.planBootstrapFileRebuild(packaging, this.noderConfig);
}
};

NoderMap.prototype._starCompress = function(path, map) {
Expand Down Expand Up @@ -147,7 +151,7 @@ NoderMap.prototype._starStarCompress = function(path, map) {
};

NoderMap.prototype.onAfterBuild = function(packaging) {
var map = configUtils.getPackagesMap(packaging, this.noderConfig, this.noderContext);
var map = this.toFile ? {} : configUtils.getPackagesMap(packaging, this.noderConfig, this.noderContext);
var sourceFilesPatterns = this.sourceFiles;
var outputFilesPatterns = this.outputFiles;
var sourceFiles = packaging.sourceFiles;
Expand All @@ -172,7 +176,11 @@ NoderMap.prototype.onAfterBuild = function(packaging) {
this._starStarCompress('', map);
}
}
configUtils.rebuildBootstrapFile(packaging, this.noderConfig);
if (this.toFile) {
grunt.file.write(path.join(packaging.outputDirectory, this.toFile), this.varName + ".updatePackagesMap(" + JSON.stringify(map) + ");");
} else {
configUtils.rebuildBootstrapFile(packaging, this.noderConfig);
}
};

module.exports = NoderMap;
3 changes: 2 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ noderJS creates a global variable called `noder` (its name is [configurable](con
This variable corresponds to the root module that is automatically created by noderJS.
All the properties usually available on the `module` variable in a module (and listed in the previous section)
are also available on the `noder` global variable. Moreover, the following shortcuts are also available for
convenience (they are documented later in this page):
convenience (most of them are documented later in this page):

* `noder.asyncRequire = noder.require("noder-js/asyncRequire")`
* `noder.define = noder.require("noder-js/currentContext").define`
* `noder.execute = noder.require("noder-js/currentContext").jsModuleExecute`
* `noder.createContext = noder.require("noder-js/context").createContext`
* `noder.updatePackagesMap`: the map provided as a parameter will be recursively merged with the existing one (it is normally provided in the [configuration](configuration.html)). This method can be useful when you want noder to be able to load modules from packages. For more information on packaging, see [here](packaging.html).

The `noder` global variable is especially intended to be used from any JavaScript code in the page that is not
loaded through noderJS (and thus does not have access to its own `module` and `require` variables). Unless there
Expand Down
Binary file added doc/images/file_structure_after_first_build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
206 changes: 156 additions & 50 deletions doc/packaging.md

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions spec/browser/context-tests/packaged-modules-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
define("paintings/catalogue.js", ["./painting"], function(module) {
var require = module.require;
var painting = require("./painting");
module.exports = function() {
return painting();
};
});

define("paintings/image.js", [], function(module) {
module.exports = function() {
return "testString";
};
});

define("paintings/painting.js", ["./image"], function(module) {
var require = module.require;
var image = require("./image");
module.exports = function() {
return image();
};
});
15 changes: 15 additions & 0 deletions spec/browser/context-tests/packaged-modules-three.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
define("paintings/catalogue.js", ["./painting"], function(module) {
var require = module.require;
var painting = require("./painting");
module.exports = function() {
return painting();
};
});

define("paintings/painting.js", ["./image"], function(module) {
var require = module.require;
var image = require("./image");
module.exports = function() {
return image();
};
});
11 changes: 11 additions & 0 deletions spec/browser/context-tests/packaged-modules-two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
define("sculptures/catalogue.js", [], function(module) {
module.exports = function() {
return "anotherTestString";
};
});

define("paintings/image.js", [], function(module) {
module.exports = function() {
return "testString";
};
});
75 changes: 75 additions & 0 deletions spec/browser/context.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var global = (function() {
return this;
})();

describe("Context", function() {
var noder = global.noder || require('../../dist/node/noder.js');
var directory = global.window ? "/base/spec/browser" : __dirname;
var expect = global.expect ? global.expect : require("expect.js");

var fail = function(done) {
return function(error) {
done(error || 'Unknown error');
};
};

it("updatePackagesMap new", function(done) {
var newRootModule = noder.createContext({
packaging: {
baseUrl: directory + '/context-tests/'
}
});

newRootModule.updatePackagesMap({
paintings: {
"*": "packaged-modules-one.js"
}
});
newRootModule.asyncRequire('paintings/catalogue').spread(function(catalogue) {
expect(catalogue()).to.equal('testString');
}).then(done, fail(done));
});

it("updatePackagesMap merge", function(done) {
var newRootModule = noder.createContext({
packaging: {
baseUrl: directory + '/context-tests/',
packagesMap: {
paintings: {
"catalogue.js": "packaged-modules-three.js",
"painting.js": "packaged-modules-three.js"
}
}
}
});

newRootModule.updatePackagesMap({
sculptures: {
"*": "packaged-modules-two.js"
},
paintings: {
"image.js": "packaged-modules-two.js"
}
});
newRootModule.asyncRequire('paintings/catalogue', 'sculptures/catalogue').spread(function(catalogueOne, catalogueTwo) {
expect(catalogueOne()).to.equal('testString');
expect(catalogueTwo()).to.equal('anotherTestString');
}).then(done, fail(done));
});

});
2 changes: 2 additions & 0 deletions src/modules/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ var Context = function(config) {
this.resolver = createInstance(config.Resolver, Resolver, this);
this.loader = createInstance(config.Loader, Loader, this);

rootModule.updatePackagesMap = bind(this.loader.updatePackagesMap, this.loader);

var globalVarName = config.varName;
if (globalVarName) {
global[globalVarName] = rootModule;
Expand Down
10 changes: 10 additions & 0 deletions src/modules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var emptyObject = {};
var scriptBaseUrl = require('../node-modules/scriptBaseUrl');
var filters = require('./filters');
var bind1 = require('./bind1');
var merge = require('./merge');

var xhrContent = function(xhr) {
return xhr.responseText;
Expand Down Expand Up @@ -93,4 +94,13 @@ loaderProto.jsPackageEval = function(jsCode, url) {
return jsEval(jsCode, url, "(function(define){\n", "\n})");
};

loaderProto.updatePackagesMap = function(newMap) {
var config = this.config;
if (config.packagesMap) {
merge(config.packagesMap, newMap, true);
} else {
config.packagesMap = newMap;
}
};

module.exports = Loader;

0 comments on commit 8bb89a9

Please sign in to comment.