Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Default export behavior when module "default" key is undefined #13017

Description

@soyuka

TypeScript Version: 2.1.4 / tried with current nightly yesterday

Linked issues :

rollup/rollup#1156
infernojs/inferno#596

Code

Inferno exports something like:

typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('./inferno')) :
typeof define === 'function' && define.amd ? define(['exports', 'inferno'], factory) :(factory((global.Inferno = global.Inferno || {}),global.inferno));

In typescript, it is export default class Component{}.

When imported, you'll expect this to work:

import createElement from 'inferno-create-element'
import Component from 'inferno-component'

console.log(createElement)
console.log(Component)

But those will both be undefined because the typescript compiler exports something like:

console.log(inferno_create_element_1.default);
console.log(inferno_component_1.default);

I also tried to import as:

import * as createElement from 'inferno-create-element'
import * as Component from 'inferno-component'

Which does bring back the correct values, except their signature is now wrong and you can not use them as intended (for example class extend Component).

Reproduction
Here is a reproduction repository:

https://github.com/soyuka/repro-default-export

Clone and run bash init.sh.

Note that:

  • Inferno creator says typescript fixed this issue (I tried latest + next without success)
  • Rollup (which is used by inferno for umd bundle) says it might be a typescript issue
  • There is no difference with or without webpack, therefore it's definitely not a webpack issue

To me it's probably that typescript always expect a default key, which is why I made a proposal to add the following to rollup:

//add this and everything works:
if (typeof module !== 'undefined') {
    module.exports.default = module.exports
}

Would you be able to give me some details about the typescript expected behavior? Is this a rollup issue or a typescript one?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions