Skip to content

Commit

Permalink
Multiple assignment case (#23)
Browse files Browse the repository at this point in the history
Fixes #22.
Tested out with huge bundle in [audio-formant](https://github.com/formant/audio-formant/blob/master/test.js), seems to work ok now.
All tests pass, not sure what other edge cases there can be.
  • Loading branch information
dy authored and goto-bus-stop committed Jan 15, 2019
1 parent bc309ba commit 4c21678
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ function createStream (opts) {
node.right.type === 'AssignmentExpression' ||
// Don't output a statement containing only `void () => {}`
node.right.type === 'ArrowFunctionExpression') {
prefix += 'void 0, '
// ignore alias assignment expression `exports.a = exports.b = exports.c`
if (!(
node.right.type === 'AssignmentExpression' &&
node.right.left.type === 'MemberExpression' &&
node.right.left.object.name === 'exports'
)) {
prefix += 'void 0, '
}
}
// Make sure we can't accidentally continue a previous statement.
// eg in `exports.a = [0]` the `[0]` could continue a previous statement if that
Expand Down
2 changes: 1 addition & 1 deletion test/funny-exports/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ console.log(/* common-shake removed: exports.a = */ 'hello world')
function a() {} /* common-shake removed: exports.b = */ void 0, function () { return a }, console.log(a)
var c = (0, /* common-shake removed: exports.c = */ 'beep boop')

/* common-shake removed: exports.d = */ void 0, exports.e = null
/* common-shake removed: exports.d = */ exports.e = null

},{}]},{},[1]);
2 changes: 1 addition & 1 deletion test/multiple-assign/a.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is roughly what Babel's ESM→CommonJS module transform does.
exports.a = exports.b = null
exports.a = exports.b = exports.c = exports.d = null

exports.a = 'a'
exports.b = 'b'
2 changes: 1 addition & 1 deletion test/multiple-assign/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
// This is roughly what Babel's ESM→CommonJS module transform does.
exports.a = /* common-shake removed: exports.b = */ null
exports.a = /* common-shake removed: exports.b = */ /* common-shake removed: exports.c = */ /* common-shake removed: exports.d = */ null

exports.a = 'a'
/* common-shake removed: exports.b = */ void 'b'
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function runTest (t, name) {

bundle.pipe(concat(function (result) {
t.is(
result.toString('utf8'),
fs.readFileSync(expected, 'utf8'),
result.toString('utf8').replace(/\r\n/g, '\n'),
fs.readFileSync(expected, 'utf8').replace(/\r\n/g, '\n'),
name
)
t.end()
Expand Down

0 comments on commit 4c21678

Please sign in to comment.