-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Importing commonJS modules with es6 imports #11179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Try SystemJS, it handles this elegantly with both TypeScript and Babel. |
@aluanhaddad it seams like SystemJS requires that I rewrite all my imports. But I would like to avoid that, change as less code as possible and convert the project progressively. |
Here's my suggestion to make it super easy. You just first do like you say: |import * as myModule from 'module';| Now forget about all that default stuff and use myModule directly like so: |console.log('myModule', myModule); I setup my first TypeScript recently and had to go through some of these Have a look at the file cowarray-test.ts in the test folder in this cowarray: Implementation of copy-on-write for JavaScript arrays using Project on GitHub: https://github.com/johanssj/cowarray NPMJS: https://www.npmjs.com/package/cowarray Be interested if this does indeed solve your problem. Cheers, Justin Johansson. On 27/09/16 23:54, Tom Esterez wrote:
|
@Johanssj I would agree with you for a new project. But when it comes to converting an existing Babel project, It means that I have to review all the imports and modify the ones that are broken. Which is painful and error prone... |
Why not a |
@testerez I believe the behaviour you point to is an older Babel 5 behaviour anyway. Babel 6 has been doing the same thing as TypeScript as far as I know, but there is a plugin for Babel that emits the interop still. See https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&code=export%20default%20function%20test%20()%20%7B%7D. I didn't quite understand the situation though - are you converting a full project or does your dependency still use the old Babel output? If it's the former, then everything should just work with updated emits. If it's the later, are you updating the dependencies? If not, you can use the CommonJS import style of TypeScript which is |
@blakeembrey it seams that babel is still doing this magic: https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015&code=import%20myModule%20from%20'module'%3B%0Aconsole.log(myModule)%3B |
@testerez Thanks. I missed that. Babel 5 used |
So there is no solution that does not imply that I rewrite all my imports? |
@testerez SystemJS allows |
I finaly did it the hard way:
I found that this is still very difficult to convert an existing project to typescript gradually. Regarding the current issue, a solution could be to make the same emit as Babel when compiling js files. Or offer another module strategy in tsc config. |
I would recommend changing one file at a time instead of changing the whole project in one shoot. by default you should get no error for importing an existing .js file, it just converts to |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I'm trying to convert a Babel project with typescript and I realize that I need to change some of the imports from
import module from 'module'
toimport * as module from 'module'
to make it work.I understand that the modules I try to import has no "default" export but Babel deals with it by wrapping the import with
_interopRequireDefault
.For example
Is converted like this by Babel
and like this by Typescript
Which logs
undefined
when importing a classic commonJS module.Maybe Typescript is right here but the glue Babel provides appends to be pretty convenient. And overall, the difference in the typescript behaviour makes the conversion of an existing Babel project very painful.
Do you have any suggestion to make it easier?
The text was updated successfully, but these errors were encountered: