From 83a94493e9b30040f1e570104f70620847e51888 Mon Sep 17 00:00:00 2001 From: Julian Kern Date: Thu, 13 Dec 2018 21:02:52 +0100 Subject: [PATCH] - change negation to ^ #63 - fix issue with negation and es6 modules close #58 --- conditional.js | 16 ++++++++++++---- slim.js | 24 +++++++++++++++--------- test/boolean-conditionals.js | 30 ++++++++++++++++++++++++++++++ test/unit.html | 2 +- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/conditional.js b/conditional.js index 8c964c6..7eb4a04 100644 --- a/conditional.js +++ b/conditional.js @@ -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); } @@ -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); } @@ -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") { diff --git a/slim.js b/slim.js index 471618b..e28fcb7 100644 --- a/slim.js +++ b/slim.js @@ -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) * @@ -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); diff --git a/test/boolean-conditionals.js b/test/boolean-conditionals.js index 9969a1c..ca54415 100644 --- a/test/boolean-conditionals.js +++ b/test/boolean-conditionals.js @@ -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() diff --git a/test/unit.html b/test/unit.html index 15e8166..f7a42af 100644 --- a/test/unit.html +++ b/test/unit.html @@ -9,6 +9,6 @@
- +