Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

require("glamorous").default undefined? #89

Closed
kokjinsam opened this issue Apr 27, 2017 · 10 comments
Closed

require("glamorous").default undefined? #89

kokjinsam opened this issue Apr 27, 2017 · 10 comments

Comments

@kokjinsam
Copy link
Collaborator

  • glamorous version: 3.11.1
  • glamor version: 2.20.4
  • react version: 15.5.4

What you did:

I'm creating an UI component package written in Typescript.

What happened:

The UI component is written in ES6. Then Typescript compiles ES6 code to ES5. But it throws error when using the package.

Here's the compiled ES5 code:

image

Problem description:

The error says that glamorous.default is undefined

@kentcdodds
Copy link
Contributor

Ah, that's interesting that TypeScript does that... 🤔

So here's the deal with our various dist files. The main in the package.json points to dist/glamorous.cjs.js which exports itself as a normal CJS module (so it doesn't have a default). With babel this isn't a problem because of the interopRequireDefault thing that it does. With webpack this isn't a problem, because it picks up the module config in our package.json which points to dist/glamorous.es.js and it can consume the ESModules. What do we need to do to make things work well with TypeScript?

@kokjinsam
Copy link
Collaborator Author

kokjinsam commented Apr 27, 2017

I'm not sure what can be done. Either there's something wrong with the Typescript definition or Typescript itself. Looking through SO, I found this, which is very similar to this case.

Edited: But the proposed solution on SO doesn't seem like a valid ES6 import statement

@kentcdodds
Copy link
Contributor

Feel free to open a PR to update whatever we need to so we can support TypeScript well!

@dragosbulugean
Copy link

having the same issue with typescript. solution is to use require instead of import, but you lose autocomplete and it looks ugly next to all your imports.

@kentcdodds
Copy link
Contributor

Are you all using webpack?

@ErikCupal
Copy link
Collaborator

I know I have been having problems with this some time ago (because of some different library). I solved it by compiling typescript to ES6 (with ES modules - no CJS), running through Babel and finally through Webpack (of course using ts-loader and babel-loader). I'm using it since them and got no problems 😃. In my opinion it is better solution for now than importing using require.

If anyone needs help with this, I can share my webpack.config.js, .babelrc and tsconfig.json 😉.

@kokjinsam
Copy link
Collaborator Author

@dragosbulugean you're using import glamorous = require("glamorous");? I'm hesitant to do that because that does not seem like a valid ES6 import statement.

@kentcdodds you mean to bundle the package? or consuming the package?

@ErikCupal that's what I'm doing right now. How are you dealing with type definitions? BTW, do you think that there could be a mistake on how we do exports in the type definitions that is causing this problem?

@dragosbulugean
Copy link

@kentcdodds I am using fuse-box
@sammkj const glamorous = require("glamorous") (it works but doesn't look good and autocompletion doesn't work)

@kokjinsam
Copy link
Collaborator Author

I just wanna make a note here for any future reference.

For those who encounter this problem, please make sure that babel-plugin-module-exports is not used when compiling the library or project. babel-plugin-module-exports adds module.exports = exports['default']; which is not compatible with Typescript.

LouisWayne referenced this issue in DefinitelyTyped/DefinitelyTyped Aug 13, 2017
* Add root quill editor

* Improvements

- Typescript syntax
- DefinitelyTyped 2.0 syntax
- Add new Quill experimental API

* Fix namespace error
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants