Skip to content

Commit ab65325

Browse files
fix(commonjs): conditional exports (#1952)
* add some failing fixture tests * fix conditional check bug * fix regression of (condition || true) case * format files with prettier * format files with pnpm lint --fix --------- Co-authored-by: Joe Moon <moon@stripe.com>
1 parent 16b7339 commit ab65325

File tree

11 files changed

+184
-2
lines changed

11 files changed

+184
-2
lines changed

packages/commonjs/src/ast-utils.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
export { default as isReference } from 'is-reference';
22

3+
function triStateAnd(a, b) {
4+
if (a === false) return false;
5+
if (b === false) return false;
6+
if (a === true && b === true) return true;
7+
return null;
8+
}
9+
10+
function triStateOr(a, b) {
11+
if (a === true) return true;
12+
if (b === true) return true;
13+
if (a === false && b === false) return false;
14+
return null;
15+
}
16+
317
const operators = {
418
'==': (x) => equals(x.left, x.right, false),
519

@@ -11,9 +25,9 @@ const operators = {
1125

1226
'!': (x) => isFalsy(x.argument),
1327

14-
'&&': (x) => isTruthy(x.left) && isTruthy(x.right),
28+
'&&': (x) => triStateAnd(isTruthy(x.left), isTruthy(x.right)),
1529

16-
'||': (x) => isTruthy(x.left) || isTruthy(x.right)
30+
'||': (x) => triStateOr(isTruthy(x.left), isTruthy(x.right))
1731
};
1832

1933
function not(value) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var condition1 = typeof global !== 'undefined' && global.env && global.env.USE_FEATURE_A;
2+
var condition2 = typeof global !== 'undefined' && global.env && global.env.USE_FEATURE_B;
3+
4+
function featureHandler() { return 'feature'; }
5+
function defaultHandler() { return 'default'; }
6+
7+
if (condition1 || condition2) {
8+
exports.handler = featureHandler;
9+
} else {
10+
exports.handler = defaultHandler;
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as commonjsHelpers from "_commonjsHelpers.js";
2+
import { __exports as input } from "\u0000fixtures/form/conditional-exports-or-conditional/input.js?commonjs-exports";
3+
4+
var hasRequiredInput;
5+
6+
function requireInput () {
7+
if (hasRequiredInput) return input;
8+
hasRequiredInput = 1;
9+
var condition1 = typeof commonjsHelpers.commonjsGlobal !== 'undefined' && commonjsHelpers.commonjsGlobal.env && commonjsHelpers.commonjsGlobal.env.USE_FEATURE_A;
10+
var condition2 = typeof commonjsHelpers.commonjsGlobal !== 'undefined' && commonjsHelpers.commonjsGlobal.env && commonjsHelpers.commonjsGlobal.env.USE_FEATURE_B;
11+
12+
function featureHandler() { return 'feature'; }
13+
function defaultHandler() { return 'default'; }
14+
15+
if (condition1 || condition2) {
16+
input.handler = featureHandler;
17+
} else {
18+
input.handler = defaultHandler;
19+
}
20+
return input;
21+
}
22+
23+
export { requireInput as __require };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var crypto = (typeof global !== 'undefined' && global.crypto) || null;
2+
3+
function randomFill() { return 'randomFill'; }
4+
function randomFillSync() { return 'randomFillSync'; }
5+
function oldBrowser() { throw new Error('not supported'); }
6+
7+
if ((crypto && crypto.getRandomValues) || false) {
8+
exports.randomFill = randomFill;
9+
exports.randomFillSync = randomFillSync;
10+
} else {
11+
exports.randomFill = oldBrowser;
12+
exports.randomFillSync = oldBrowser;
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as commonjsHelpers from "_commonjsHelpers.js";
2+
import { __exports as input } from "\u0000fixtures/form/conditional-exports-or-false/input.js?commonjs-exports";
3+
4+
var hasRequiredInput;
5+
6+
function requireInput () {
7+
if (hasRequiredInput) return input;
8+
hasRequiredInput = 1;
9+
var crypto = (typeof commonjsHelpers.commonjsGlobal !== 'undefined' && commonjsHelpers.commonjsGlobal.crypto) || null;
10+
11+
function randomFill() { return 'randomFill'; }
12+
function randomFillSync() { return 'randomFillSync'; }
13+
function oldBrowser() { throw new Error('not supported'); }
14+
15+
if ((crypto && crypto.getRandomValues) || false) {
16+
input.randomFill = randomFill;
17+
input.randomFillSync = randomFillSync;
18+
} else {
19+
input.randomFill = oldBrowser;
20+
input.randomFillSync = oldBrowser;
21+
}
22+
return input;
23+
}
24+
25+
export { requireInput as __require };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var crypto = (typeof global !== 'undefined' && global.crypto) || null;
2+
3+
function secureHandler() { return 'secure'; }
4+
function fallbackHandler() { return 'fallback'; }
5+
6+
if ((crypto && crypto.getRandomValues) || null) {
7+
exports.handler = secureHandler;
8+
} else {
9+
exports.handler = fallbackHandler;
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as commonjsHelpers from "_commonjsHelpers.js";
2+
import { __exports as input } from "\u0000fixtures/form/conditional-exports-or-null/input.js?commonjs-exports";
3+
4+
var hasRequiredInput;
5+
6+
function requireInput () {
7+
if (hasRequiredInput) return input;
8+
hasRequiredInput = 1;
9+
var crypto = (typeof commonjsHelpers.commonjsGlobal !== 'undefined' && commonjsHelpers.commonjsGlobal.crypto) || null;
10+
11+
function secureHandler() { return 'secure'; }
12+
function fallbackHandler() { return 'fallback'; }
13+
14+
if ((crypto && crypto.getRandomValues) || null) {
15+
input.handler = secureHandler;
16+
} else {
17+
input.handler = fallbackHandler;
18+
}
19+
return input;
20+
}
21+
22+
export { requireInput as __require };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var condition = typeof global !== 'undefined' && global.env && global.env.NODE_ENV;
2+
3+
function prodHandler() { return 'production'; }
4+
function defaultHandler() { return 'default'; }
5+
6+
if (condition || true) {
7+
exports.handler = prodHandler;
8+
} else {
9+
exports.handler = defaultHandler;
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as commonjsHelpers from "_commonjsHelpers.js";
2+
import { __exports as input } from "\u0000fixtures/form/conditional-exports-or-true/input.js?commonjs-exports";
3+
4+
var hasRequiredInput;
5+
6+
function requireInput () {
7+
if (hasRequiredInput) return input;
8+
hasRequiredInput = 1;
9+
var condition = typeof commonjsHelpers.commonjsGlobal !== 'undefined' && commonjsHelpers.commonjsGlobal.env && commonjsHelpers.commonjsGlobal.env.NODE_ENV;
10+
11+
function prodHandler() { return 'production'; }
12+
function defaultHandler() { return 'default'; }
13+
14+
if (condition || true) {
15+
input.handler = prodHandler;
16+
} else {
17+
exports.handler = defaultHandler;
18+
}
19+
return input;
20+
}
21+
22+
export { requireInput as __require };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var condition = typeof global !== 'undefined' && global.env && global.env.DEBUG;
2+
3+
function enabledHandler() { return 'enabled'; }
4+
function disabledHandler() { return 'disabled'; }
5+
6+
if (true || condition) {
7+
exports.handler = enabledHandler;
8+
} else {
9+
exports.handler = disabledHandler;
10+
}

0 commit comments

Comments
 (0)