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

Multiple assignment case #23

Merged
merged 4 commits into from
Jan 15, 2019
Merged
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
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' &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naive guess, possibly can be more general.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus side effect

/* 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'),
goto-bus-stop marked this conversation as resolved.
Show resolved Hide resolved
fs.readFileSync(expected, 'utf8').replace(/\r\n/g, '\n'),
name
)
t.end()
Expand Down