I read here that I don't need to put a semicolon after default exports. So this program has an unnecessary semicolon:
export default function() {};
But if my module continues like this:
export default function() {};
(() => {
// creating a new function scope
})();
then I can't leave the semicolon.
So what is going on here? The grammar says I don't need the semicolon but if I leave it the code means something else?
UPDATE:
If I leave the semicolon:
export default function() {}
(() => {
// creating a new function scope
})();
then the exported function gets called instead of being exported. babeljs.io compiles the latter into:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = (function () {})(function () {
// creating a new function scope
})();
;
module.exports = exports["default"];
More precisely after it gets called an error is thrown, because the return value of the first function also gets called (but that is not a function). The error I get in chrome is this:
Uncaught TypeError: (intermediate value)(...) is not a function(…)
You don't need to add a semicolon after a export default
when it's followed by a function declaration, that's what the grammar says.
Babel is wrong, I've filed a bug against it. That code should be interpreted as exporting the function and then running the IIFE as an IIFE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With