-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
ts loader should resolve relative path's which not starts with './' #29
Comments
I'm surprised that it works with Might be worthwhile to try installing |
@jbrantly, i already use latest ntypescript |
The loader does no module resolving by itself - it delegates all of that to TypeScript. So if it works in TypeScript, it should work in the loader. I wrote a quick example app and was unable to duplicate. Code like The only thing that I can think of is that ts-loader is using an outdated version of TypeScript. If you're using ntypescript, you will need to configure the loader to use that using the compiler option, like:
I'd also make sure you're using the latest version of webpack and of ts-loader. If you continue to see issues, feel free to paste your tsconfig.json and webpack.config.js here so I can try to duplicate further. |
Were you able to make any progress on this? |
In your example you are using To put it another way, look at this code:
When you say 'react' there, you really mean a file in node_modules/react. But when you say 'components/login', you really mean a file relative to this file: './components/login'. TypeScript and webpack have no way to divine what you really mean, so you must explicitly tell them that the second module is relative. In your original post you mentioned that module identifiers like '../../myModule' weren't working. There is no reason that shouldn't work though because '../' indicates that it is a relative module. Do you have an example of that not working? |
Thank you for your answer! |
You are right. ES6 modules, as near as I can tell, are "implementation-specific" when it comes to resolving module identifiers (meaning they don't necessarily have to follow CommonJS rules). However, webpack (and node) work off those rules and in this case webpack is actually throwing that error. In order words, using "./" is a requirement of webpack (for now at least, they are working on a ES6 compatibility for webpack2). I'm actually a little surprised that TypeScript resolves it though. Learn something new... |
I think that you should write about './' path in ts-loader requirements. |
I've created issue microsoft/TypeScript#4214 for it. It seems to me that TypeScript should throw an error if configured to output CJS (which it is by default when using ts-loader). |
I want to add that one can configure 'global' module locations using modulesDirectories in webpack config, like this: resolve: {
modulesDirectories: [ 'node_modules', 'bower_components', 'src', 'style']
} We found it pretty convenient (adding src and style folders) as it allows us to make 'local' module loading independent from the .ts file path depth. So instead of require('../lib/mylib.ts') in one file and '../../../lib/mylib.ts' in another we can just require('lib/mylib.ts'). Tracking relative position of styles/assets files is even more annoying. I was fully prepared to forgo this convenience to achieve typescript@next side compilation (w/o webpack) but was pleasantly surprised to find out that it is working perfectly fine probably because tsconfig.json location is considered a root location for global modules by ts@next . |
@alexdrel You're absolutely right, thanks for pointing this out. It highlights the complexities involved. There is a fairly wide gap between what a loader can do (any loader, not just webpack) in terms of configuring module resolution and what TypeScript can currently do. The good news is that they're aware of the issue and there is an effort to fix it. I still think TypeScript is in error with allowing relative imports without './' or '../' personally. That doesn't really seem to be a "thing" as near as I can tell. Resolving relative to the root is pretty cool though, I'll have to keep that in mind for my own stuff 👍 thanks |
The TypeScript folks have confirmed that this is (essentially) a bug in TypeScript in the sense that the ES6 module resolution logic right now is quick and dirty and is changing soon. The new default logic should require './' or '../' for relative modules. Going to close this as the loader itself is working as intended. |
Hello @jbrantly !
Thank you for this loader!
I have question
for example we uses import * as mm from '../../MyFolder/myModule';
not will be resolved when call ts-loader, but if we use
'./../../MyFolder/myModule' - all cool.
ts compiler allow use relative paths, which can starts with './' and without it.
What do you think about it?
The text was updated successfully, but these errors were encountered: