Got undefined when importing Skeletons
Cl00e9ment opened this issue · comments
When I try to import Skeletons I get undefined:
import Skeletons from 'skeletons';
console.log(Skeletons);
undefined
However, this is what I get when I import everything:
import * as Skeletons from 'skeletons';
console.log(Skeletons);
[Function: Skeletons] {
version: '0.1.0',
String: [Function],
Array: [Function],
Boolean: [Function],
Number: [Function],
Object: [Function],
MapObject: [Function],
Function: [Function],
Symbol: [Function],
Null: [Function],
Any: [Function],
Types: [Function],
Warnings: [Function],
typeof: [Function],
getKeyStr: [Function],
setDefault: [Function],
expectType: [Function]
}
I can't use this last syntax because this isn't an intended behaviour (it's inconsistent with index.d.ts
).
I tried to fix that myself, but I'm not familiar with the tools that you used.
Thanks.
I found where the bug come from. It come from the add-module-exports Babel plugin, see this issue.
Explanation
When Babel transpile Skeletons, this
export default Skeletons
become this:
var _default = Skeletons;
exports["default"] = _default;
module.exports = exports.default;
The last line cancels exports["default"] = _default
because you can't use both exports.default
and module.exports
(see this anwser). The last line also reset the __esModule
flag.
So, let Foo, an ES module that use Skeletons:
import Skeletons from 'skeletons';
If Foo is transpiled with Babel, the generated CommonJS code will check if Skeletons is an ES module and it's not because the __esModule
flag has been reset. Thus, it will work.
If Foo is transpiled with tsc (TypeScript Compile), the generated CommonJS code will infer that Skeletons is an ES module because we used the import
statement. tsc does not verify the __esModule
flag because it's a Babel flag. Thus it will not work because exports.default
equals undefined
.
Workaround
Simply add the addDefaultProperty
property for the add-module-exports
plugins:
{
"presets": ["@babel/preset-env"],
"plugins": [
[
"add-module-exports",
{
"addDefaultProperty": true
}
]
]
}
Since this bug has been fixed in the last commit, will you soon publish the new version on NPM?
Thanks.
Thanks @Cl00e9ment ,
The new version has published