-
Notifications
You must be signed in to change notification settings - Fork 12k
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
importing is cumbersome for deeply nested components, how do I set up a map to the app root? #865
Comments
I had also tried adding '@app': 'app', and it can't find any files using that. :-\ Also, I deleted the dist folder, so it appears that however this needs to be configured, it is pre-build (because the error occurs before the dist folder is even created) |
you should use map + packages in system-config.js
Also tsconfig.json in compilerOptions section
and then you can use something like that:
Note: it works without
|
@SergeySolonko can you explain a bit what each option does, and how it affects the various parts of the build process? I tried copy-pasting what you had, and I still get the error "Cannot find module '@web-app/anything'" |
@NullVoxPopuli this config works for standard folder configuration, at least first part. It should be something like this: with system-config you're creating kind-a symlink to get access to specific module, with tsconfig.json you're saying which file you're expecting to get and that it should be compiled now
I hope I've explained it clearly :) |
does nothing. The |
The path mapping is only used by TypeScript compiler to know which import corresponds to which modules. SystemJs operates on another level. |
yes correct that was redundant, but still when you're trying to compile with _prod mode - it fails |
I was able to get the system-config.js file to work correctly, but I added paths to the compilerOptions section of tsconfig.json and am unable to import, the compiler throws this error: Am I missing something? Here is my tsconfig.json...
|
I'm just starting with ng2, and it seemed very intuitive to me to try to use a full path. Why do people tend to use relative paths instead? |
@tinchou, how do you use a full path? how do you ref your app root? |
@tinchou Using relative paths can be easier when referencing a file in the same folder or a folder above. If you move a folder you don't have to fix all of the imports within that folder. But when the files are deeply nested and you are referencing something that is in the root of your source directory, I agree that full path tends to be simpler. Also, the import system is related to ES2015 modules/TypeScript/System JS more than Angular 2 itself. |
@NullVoxPopuli I haven't set it up, I'm just saying it didn't work by default. @aciccarello true, but maybe having an alias to the root in the default template would help. |
@tinchou that's what we are trying to get - to have a reference to very top folder (app folder). It works well for non-production build, for production - it fails. #865 (comment) |
@SergeySolonko this is my +1 to having an alias to the app root by default with |
The following works for me for both
tsconfig.json - Add the following
Now the app directory should be able to be imported from as follows:
Caveats App specific barrels aren't able to be referenced by directory name when imported from an app-relative path regardless of whether a barrel definition exists in system-config.ts (not sure why).
As an aside, I have no idea why the paths entry in tsconfig.json works when it should only be available in typescript@next (with the target milestone of 2.0) - microsoft/TypeScript#5728. |
yeah, we definitely need aliases. webpack makes that pretty straightforward: https://webpack.github.io/docs/configuration.html#resolve-alias obviously there's still the typescript compilation step in between which makes it a bit more tricky but the newly introduced i have a relatively small app and already have super ugly relative |
Yeah, I've realized just recently that the import must work for the compile step and then for the dist version, where the files are loaded by SystemJs. The search paths for the typescript compiler seem to be kind of hardcoded, you can't tell it to use aliased paths like with SystemJs. |
Anyone know how to do this using the webpack version? |
A more broad issue that includes this one is being tracked in #1465. Settings paths in tsconfig as shown in #865 (comment) should work. We've also removed barrels (they've become a bit unecessary with NgModules) so the problem @Blasz was experiencing with referencing them shouldn't be an overall thing. |
@filipesilva Note that #865 (comment) no longer works for me in the majority of cases with the webpack version. I can no longer import files from using the root-relative path when the file being imported contains other common imports (which it will in 95% of cases). It looks like a false positive circular-dependency related bug. |
@Blasz is that a CLI or a typescript bug though? |
@filipesilva I'm not quite sure but would lean towards CLI rather than typescript since the errors are being thrown by the webpack module resolution code rather than the typescript compiler. I'll try to investigate further and lodge a proper issue once I verify what the issue is exactly. |
@Blasz appreciated! |
@Blasz thanks a lot. Had to write a little script to convert our existing import paths. Maybe someone can use it too: https://gist.github.com/dedeibel/65ebfc4ccb53a758952e4d812c832831 (perl) |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
how do I set up a mapping for my app root to import?
I'm trying to avoid doing:
and would like to do:
it is more intuitive, to specify an app-relative path, especially when refactoring (so you don't need to open the file tree, and count how many ../ you have...
I've tried adding mappings in my system-config.ts, but I haven't found anything to work.
doesn't work -- as in:
errors on build.
The text was updated successfully, but these errors were encountered: