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

Add js extension to import/export #18971

Closed
TheColorRed opened this Issue Oct 5, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@TheColorRed
Copy link

TheColorRed commented Oct 5, 2017

TypeScript Version: 2.5.2

Code

export { MyClass } from './a/b' // This is a file

In Chrome 61 Modules are supported out of the box. Node knows that this is a file, but when run in Chrome it thinks it is a directory and then it fails when it is loaded because it is trying to load index.js instead of b.js.

When changed to:

export { MyClass } from './a/b.js'

Chrome loads the file correctly. Is there a way to have the compiler add the .js extension on import/export without having to hard code it into all the files?

@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Oct 5, 2017

The compiler does not rewrite module names for you. a module name is a resource identifier, and will be emitted verbatim in most cases.

You should write the module name as you expect it to work at runtime; the compiler should also be able to understand that ./a/b.js points to ./a/b.ts at design time.

@mhegazy mhegazy added the Duplicate label Oct 5, 2017

@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Oct 5, 2017

Please see similar discussions in #18951 and #16640

@whitecolor

This comment has been minimized.

Copy link

whitecolor commented Oct 14, 2017

Such thing really should be done by loaders, until TS approach doesn't break any standards.

@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Oct 30, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@amatiasq

This comment has been minimized.

Copy link

amatiasq commented Jun 7, 2018

I'm having this issue in two directions:

  • I started the development without any extension.
  • I could write test and run them with jest perfectly.
  • When I tried to compile the modules and load them as ES6 modules in Chrome it didn't work because they had no extension.
  • I added .js extension manually to all imports.
  • Now it compiles and runs in Chrome but jest complains:

Cannot find module './math.js' from 'vector.ts'

The only solution I see is for the compiler to add .js to import statements, maybe can this be under a flag?

I've seen the related issues but this is different since it's only related to the extension Typescript is changing. We're not talking here about paths, but if tsc is creating a .js file for each .ts file it makes sense to change/add the extension on the import statements from no extension or from .ts to .js, right?

@jens-t jens-t referenced this issue Nov 26, 2018

Merged

build changes #49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment