Skip to content

Commit

Permalink
conditional package system updates
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 13, 2016
1 parent 565226e commit b4c0284
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
24 changes: 19 additions & 5 deletions lib/conditionals.js
Expand Up @@ -34,22 +34,36 @@
*
* These conditions can also be negated via:
*
* import 'es5-shim#?~./conditions.js|es6'
* import 'es5-shim#?./conditions.js|~es6'
*
*/

var sysConditions = ['browser', 'node', 'dev', 'production', 'default'];

function parseCondition(condition) {
var conditionExport, conditionModule, negation;

var negation = condition[0] == '~';
var conditionExportIndex = condition.lastIndexOf('|');
if (conditionExportIndex != -1) {
conditionExport = condition.substr(conditionExportIndex + 1);
conditionModule = condition.substr(negation, conditionExportIndex - negation) || '@system-env';
conditionModule = condition.substr(negation, conditionExportIndex - negation);

if (negation)
warn.call(this, 'Condition negation form "' + condition + '" is deprecated for "' + conditionModule + '|~' + conditionExport + '"');

if (conditionExport[0] == '~') {
negation = true;
conditionExport = conditionExport.substr(1);
}
}
else {
conditionExport = null;
conditionModule = condition.substr(negation);
if (sysConditions.indexOf(conditionModule) != -1) {
conditionExport = conditionModule;
conditionModule = '@system-env';
}
}

return {
Expand All @@ -60,7 +74,7 @@
}

function serializeCondition(conditionObj) {
return (conditionObj.negate ? '~' : '') + conditionObj.module + (conditionObj.prop ? '|' + conditionObj.prop : '');
return conditionObj.module + (conditionObj.prop ? '|' + conditionObj.prop : '');
}

function resolveCondition(conditionObj, parentName, bool) {
Expand All @@ -86,7 +100,7 @@
if (!conditionalMatch)
return Promise.resolve(name);

var conditionObj = parseCondition(conditionalMatch[0].substr(2, conditionalMatch[0].length - 3));
var conditionObj = parseCondition.call(this, conditionalMatch[0].substr(2, conditionalMatch[0].length - 3));

// in builds, return normalized conditional
if (this.builder)
Expand Down Expand Up @@ -115,7 +129,7 @@
if (booleanIndex == -1)
return Promise.resolve(name);

var conditionObj = parseCondition(name.substr(booleanIndex + 2));
var conditionObj = parseCondition.call(this, name.substr(booleanIndex + 2));

// in builds, return normalized conditional
if (this.builder)
Expand Down
6 changes: 3 additions & 3 deletions lib/core.js
Expand Up @@ -293,7 +293,7 @@ hook('instantiate', function(instantiate) {
For easy normalization canonicalization with latest URL support.
*/
SystemJSLoader.prototype.env = 'development';
SystemJSLoader.prototype.env = 'dev';

var curCurScript;
SystemJSLoader.prototype.config = function(cfg, isEnvConfig) {
Expand All @@ -314,8 +314,8 @@ SystemJSLoader.prototype.config = function(cfg, isEnvConfig) {
if (cfg.transpilerRuntime === false)
loader._loader.loadedTranspilerRuntime = true;

if (cfg.production)
setProduction.call(loader, true);
if ('production' in cfg)
setProduction.call(loader, cfg.production);

if (!isEnvConfig) {
// if using nodeConfig / browserConfig / productionConfig, take baseURL from there
Expand Down
31 changes: 21 additions & 10 deletions lib/package.js
Expand Up @@ -26,7 +26,8 @@
*
* // environment-specific map configurations
* './index.js': {
* '~browser': './index-node.js'
* '~browser': './index-node.js',
* './custom-condition.js|~export': './index-custom.js'
* }
* },
* // allows for setting package-prefixed depCache
Expand Down Expand Up @@ -284,17 +285,27 @@
if (loader.builder)
return Promise.resolve(pkgName + '/#:' + path);

// we load all conditions upfront
var conditionPromises = [];
var conditions = [];
for (var e in mapped) {
var c = parseCondition(e);
conditions.push({
condition: c,
map: mapped[e]
});
conditionPromises.push(loader['import'](c.module, pkgName));
}

// map object -> conditional map
return loader['import'](pkg.map['@env'] || '@system-env', pkgName)
.then(function(env) {
return Promise.all(conditionPromises)
.then(function(conditionValues) {
// first map condition to match is used
for (var e in mapped) {
var negate = e[0] == '~';

var value = readMemberExpression(negate ? e.substr(1) : e, env);

if (!negate && value || negate && !value)
return mapped[e];
for (var i = 0; i < conditions.length; i++) {
var c = conditions[i].condition;
var value = readMemberExpression(c.prop, conditionValues[i]);
if (!c.negate && value || c.negate && !value)
return conditions[i].map;
}
})
.then(function(mapped) {
Expand Down

0 comments on commit b4c0284

Please sign in to comment.