Skip to content
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

import self issue bug #648

Closed
bluelovers opened this issue Jul 28, 2018 · 12 comments
Closed

import self issue bug #648

bluelovers opened this issue Jul 28, 2018 · 12 comments
Labels

Comments

@bluelovers
Copy link
Contributor

bluelovers commented Jul 28, 2018

file

./strtable.ts

import * as self from './strtable';
export default self;
console.log(__filename, require.extensions);

cli

ts-node strtable.ts

output

Expected behavior

should only once , even if .js exists

strtable.ts {
  '.ts': [Function],
  '.tsx': [Function],
  '.js': [Function],
  '.json': [Function],
  '.node': [Function] }

Actual behavior:

will load self x2 ( .js + .ts )

strtable.js { '.js': [Function],
  '.json': [Function],
  '.node': [Function],
  '.ts': [Function],
  '.tsx': [Function] }

strtable.ts { '.js': [Function],
  '.json': [Function],
  '.node': [Function],
  '.ts': [Function],
  '.tsx': [Function] }
@blakeembrey
Copy link
Member

Can you create a reproduction I can look at? There's no way, from this information you've given me, such behaviour could be replicated.

@bluelovers
Copy link
Contributor Author

2018-07-29-10-44-59-6

2018-07-29-10-45-18-7

@blakeembrey
Copy link
Member

blakeembrey commented Jul 29, 2018

Still can not be replicated. I can only assume you have both a .js file and a .ts file next to each other, so this is the expected behaviour. .js has priority of .ts, so these are two distinct files and you aren't importing self, you're importing a different file.

Reference:
image

@bluelovers
Copy link
Contributor Author

when we use ts-node and import , should use .ts > .js

@blakeembrey
Copy link
Member

I disagree. Messing with core node.js expected behaviour is not something I intend to support, and having a behaviour that's different in ts-node vs tsc + node.js goes against the mission of this module.

@bluelovers
Copy link
Contributor Author

@blakeembrey how about only if xxxx.ts already loaded, than use .ts > .js ?

@blakeembrey
Copy link
Member

That’s still altering the behaviour of node somewhat, I would need to override the module resolution logic. Plus it wouldn’t solve anything really for you - node always loads Javascript first, so you’re just pushing the issue to the second file that’s loaded. You can replicate this by putting a file before your “self import” and it’ll show it only loads the JavaScript and never TypeScript.

@bluelovers
Copy link
Contributor Author

how about make option?

#649

@blakeembrey
Copy link
Member

But why?

@bluelovers
Copy link
Contributor Author

make sure when we run .ts is new

sometime when .js, not update done or watcher fail for watch and make new .js

@blakeembrey
Copy link
Member

blakeembrey commented Jul 29, 2018

I still don’t understand why. Can’t you just output JS to a different directory?

I think this has negative side effects. For example, this will create an issue for anyone writing JS libraries in TypeScript and publishing them like you are (JS next to TS). The TypeScript will be resolved first and, as a result, won’t be able to compile. Anyone using this flag might run into this random error, creating more issues to triage.

@bjg2
Copy link

bjg2 commented Feb 21, 2019

I want to point out that having .js > .json > .ts > .tsx order actually messes up node expected execution, as mentioned in #786. I think correct ordering for ts-node should be: .ts > .tsx > .js > .json.

@blakeembrey I don't quite understand example above, if there are .js and .ts with the same name, which one you think should be imported? If tsc + node were executed (I guess ts-node aims to be simpler way to do tsc + node) .js would be overriden with transpiled .ts and that one would be executed. I don't think anyone expects to pick .js before .ts one, but for sure noone expects to pick .json before .ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants