Skip to content

Commit

Permalink
- change negation to ^ #63
Browse files Browse the repository at this point in the history
- fix issue with negation and es6 modules close #58
  • Loading branch information
Julian Kern committed Dec 13, 2018
1 parent 81e7531 commit 83a9449
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
16 changes: 12 additions & 4 deletions conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ define(["module", "exports"], function(module, exports) {

var conditionExport = "default";

var booleanNegation = !substitution && conditionModule[0] === "~";
var booleanNegation = !substitution && conditionModule[0] === "^";
if (booleanNegation) {
conditionModule = conditionModule.substr(1);
}
Expand Down Expand Up @@ -189,7 +189,7 @@ define(["module", "exports"], function(module, exports) {
if (substitution) {
res = id.replace(PLACEHOLDER, "#{" + cond + "}");
} else {
var prefix = booleanNegation ? "#?~" : "#?";
var prefix = booleanNegation ? "#?^" : "#?";
res = id.replace(PLACEHOLDER, prefix + cond);
}

Expand Down Expand Up @@ -335,8 +335,16 @@ define(["module", "exports"], function(module, exports) {
//!steal-remove-end

var handleConditionalEval = function(m) {
var conditionValue = (typeof m === "object") ?
readMemberExpression(conditionExport, m) : m;
var conditionValue;
if(typeof m === "object"){
if(m.__esModule){ // es6-module
conditionValue = m[conditionExport];
}else{ // cjs-module
conditionValue = readMemberExpression(conditionExport, m);
}
}else{
conditionValue = m;
}

if (substitution) {
if (typeof conditionValue !== "string") {
Expand Down
24 changes: 15 additions & 9 deletions slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ module.exports = function(stealRequire) {

var conditionExport = "default";

var booleanNegation = !substitution && conditionModule[0] === "^";
if (booleanNegation) {
conditionModule = conditionModule.substr(1);
}

/**
* Get the index where the member expression is located (if any)
*
Expand All @@ -87,18 +92,19 @@ module.exports = function(stealRequire) {
conditionModule = conditionModule.substr(0, conditionExportIndex);
}

var booleanNegation = !substitution && conditionModule[0] === "~";
if (booleanNegation) {
conditionModule = conditionModule.substr(1);
}

// need to turn conditionModule into a numeric id somehow.
var actualConditionModule = stealRequire(config.map[conditionModule]);

var conditionValue =
typeof actualConditionModule === "object"
? readMemberExpression(conditionExport, actualConditionModule)
: actualConditionModule;
var conditionValue;
if(typeof actualConditionModule === "object"){
if(actualConditionModule.__esModule){ // es6-module
conditionValue = actualConditionModule[conditionExport];
}else{ // cjs-module
conditionValue = readMemberExpression(conditionExport, actualConditionModule);
}
}else{
conditionValue = actualConditionModule;
}

if (substitution) {
moduleId = moduleId.replace(conditionalRegEx, conditionValue);
Expand Down
30 changes: 30 additions & 0 deletions test/boolean-conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ QUnit.module("es6 - true condition with `.` modifier", function(hooks) {
testTrueConditionWithModifier);
});

QUnit.module("es6 - default boolean", function(hooks) {
hooks.beforeEach(function() {
loader.delete("browser");
var oldFetch = this.oldFetch = loader.fetch;

loader.fetch = function(load) {
return load.name === "is-true" ?
Promise.resolve("export default true;") :
oldFetch.call(loader, load);
};
});

hooks.afterEach(function() {
loader.fetch = this.oldFetch;
});

QUnit.test("normalizes name to special @empty module", function (assert) {
var done = assert.async();
loader.normalize("jquery#?^is-true")
.then(function(name) {
assert.equal(name, "@empty");
done();
})
.catch(function(err) {
assert.notOk(err, "should not fail");
done();
});
});
});

QUnit.module("with boolean conditional in a npm package name", function(hooks) {
hooks.beforeEach(function() {
this.loader = helpers.clone()
Expand Down
2 changes: 1 addition & 1 deletion test/unit.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<div id="qunit"></div>
<div id="qunit-fixture"></div>

<script src="../node_modules/steal/steal.js" data-main="test/test"></script>
<script src="../node_modules/steal/steal-sans-promises.js" data-main="test/test"></script>
</body>
</html>

0 comments on commit 83a9449

Please sign in to comment.