Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boolean negation with es6 modules and negation syntax #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import 'es5-shim#?conditions.needs-es5shim'
These conditions can also be negated via:

```js
import 'es5-shim#?~conditions.supports-es5'
import 'es5-shim#?^conditions.supports-es5'
```

## License
Expand Down
25 changes: 17 additions & 8 deletions conditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ define(["module", "exports"], function(module, exports) {
conditionalMatch[0].substr(2);

var conditionExport = "default";

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

var conditionExportParts = conditionModule
.match(/^(?:\.\/|\.\.\/)+/); // split './' or '../' in relative names

Expand All @@ -96,11 +102,6 @@ define(["module", "exports"], function(module, exports) {
conditionModule = conditionModule.substr(0, conditionExportIndex);
}

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

var handleConditionalBuild = function() {};

/**
Expand Down Expand Up @@ -188,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 @@ -334,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
2 changes: 1 addition & 1 deletion docs/pages/boolean-conditional-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ The part before the `#?` is the polyfill package name we had before, the interes

> It is also possible to negate conditionals via:
> ```js
> import "document-register-element#?~supports-custom-elements";
> import "document-register-element#?^supports-custom-elements";
> ```
> See `steal-conditional`'s [README](https://github.com/stealjs/steal-conditional/blob/master/README.md) for more details.

Expand Down
2 changes: 1 addition & 1 deletion docs/steal-conditional.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import "es5-shim#?conditions.needs-es5shim"
These conditions can also be negated via:

```
import "es5-shim#?~is-es5-compatible"
import "es5-shim#?^is-es5-compatible"
```

### Bundling conditional modules
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>