Skip to content

Commit

Permalink
Prevent the Babel plugin from breaking the output of babel-node (#374)
Browse files Browse the repository at this point in the history
* prevent the babel plugin from breaking the output of babel-node

the tagger code inserted by the babel plugin at the end of every file is an IIFE, which
happens to break babel-node when it's loaded. by changing it to a
variable declaration (that is still immediately invoked), it works the
same way and doesn't break the plugin.

* comment why we assigned the tagger function to an unused variable
  • Loading branch information
calesce committed Sep 22, 2016
1 parent bc263de commit 1058ad9
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 26 deletions.
8 changes: 6 additions & 2 deletions src/babel/index.js
Expand Up @@ -4,8 +4,11 @@ const buildRegistration = template(
'__REACT_HOT_LOADER__.register(ID, NAME, FILENAME);'
);
const buildSemi = template(';');

// We're making the IIFE we insert at the end of the file an unused variable
// because it otherwise breaks the output of the babel-node REPL (#359).
const buildTagger = template(`
(function () {
var UNUSED = (function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}
Expand Down Expand Up @@ -141,7 +144,7 @@ module.exports = function plugin(args) {
/* eslint-enable */
},

exit({ node }) {
exit({ node, scope }) {
const registrations = node[REGISTRATIONS];
node[REGISTRATIONS] = null; // eslint-disable-line no-param-reassign

Expand All @@ -150,6 +153,7 @@ module.exports = function plugin(args) {
node.body.push(buildSemi());
node.body.push(
buildTagger({
UNUSED: scope.generateUidIdentifier(),
REGISTRATIONS: registrations,
})
);
Expand Down
4 changes: 2 additions & 2 deletions test/babel/fixtures/bindings/expected.js
Expand Up @@ -22,7 +22,7 @@ const _default = React.createClass({});
export default _default;
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}
Expand All @@ -38,6 +38,6 @@ export default _default;
__REACT_HOT_LOADER__.register(E, "E", __FILENAME__);

__REACT_HOT_LOADER__.register(_default, "default", __FILENAME__);
})();
}();

;
4 changes: 2 additions & 2 deletions test/babel/fixtures/class-properties/arguments/expected.js
Expand Up @@ -8,12 +8,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
4 changes: 2 additions & 2 deletions test/babel/fixtures/class-properties/block-body/expected.js
Expand Up @@ -10,12 +10,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
Expand Up @@ -10,12 +10,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
Expand Up @@ -10,12 +10,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
Expand Up @@ -10,12 +10,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
Expand Up @@ -10,12 +10,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
Expand Up @@ -9,12 +9,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
4 changes: 2 additions & 2 deletions test/babel/fixtures/class-properties/new.target/expected.js
Expand Up @@ -7,12 +7,12 @@ class Foo {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Foo, "Foo", __FILENAME__);
})();
}();

;
4 changes: 2 additions & 2 deletions test/babel/fixtures/counter/expected.js
Expand Up @@ -21,14 +21,14 @@ var _default = function _default() {
exports.default = _default;
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(Counter, "Counter", __FILENAME__);

__REACT_HOT_LOADER__.register(_default, "default", __FILENAME__);
})();
}();

;
4 changes: 2 additions & 2 deletions test/babel/fixtures/issue-246/expected.js
Expand Up @@ -13,12 +13,12 @@ function spread() {
}
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(spread, "spread", __FILENAME__);
})();
}();

;
4 changes: 2 additions & 2 deletions test/babel/fixtures/name-clash/expected.js
Expand Up @@ -3,14 +3,14 @@ const _default2 = 42;
export default _default2;
;

(function () {
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}

__REACT_HOT_LOADER__.register(_default, "_default", __FILENAME__);

__REACT_HOT_LOADER__.register(_default2, "default", __FILENAME__);
})();
}();

;

0 comments on commit 1058ad9

Please sign in to comment.