-
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
Add project source dir as a module #1465
Comments
Have seen similar thing in ng-bootstrap. The demo project inside refers to the main project using @ng-bootstrap and it is using webpack |
In terms of webpack implementation would we be taking this @app property and mapping them to the webpack alias for module resolution @filipesilva |
Something important that came up: editor tooling would never be able to find |
There are also tsconfig's baseUrl and paths options. This would only apply to typescript files but allows editors (e.g., vscode) to process and follow the mapping. To make the current version of vscode pick up the settings, you'll need to add a workspace settings file with
UPDATE: The above additions to tsconfig and the inclusion of the TsConfigPathsPlugin in the CLI's web pack config work well so far. It definitely simplifies and improves readability of typescript imports. |
Can't wait on this one :) Also struggling with FontAwesome Fonts implementation. Did a CDN for now... |
Another thing to be aware of with aliases is tests. Just because webpack knows @app as an Ali's doesn't mean mocha or karma etc. will know how to resolve it |
I've tried this, the TypeScript compiles, but I get bundle errors from webpack. I.e. I've added this to tsconfig.json:
but e.g.
throws the error:
I feel like I have to update webpack configuration somewhere? |
@clydin How did you include TsConfigPathsPlugin, as the webpack configuration is blackboxed? And I had a look, and it's not in there? |
I have basically @rolandoldengarm's issue, but there's a case that might be worth testing for someone: npm link. When I attempted anything using symbolic links it seemed that webpack would resolve the real path instead of the alias. Combined with 'tsconfig:paths' not working yet, I cannot fathom solution to 'develop using modules' that's supportable long term. Mostly want to call this out because at the moment this is listed as 'nice to have', and I really can't imagine being able to use the tool in this state long term. I tried npm link, obviously. But also a few direct symbolic links - the craziest failure was when I symbolically linked a module that depends on another. Consider B depends on A: With both A and B On the chance this might be of interest to someone here I added some scripts to my repos to help demo it. If not interested, no worries. The repositories I'm working on are https://github.com/ggranum/revector and https://github.com/ggranum/revector-demo Specifically, if you clone both of those into the same parent and run
Everything should work. Now stop the server and run:
and you should reproduce the errors I'm seeing [though note that the sed command in the bash scripts may break on non-macs] : ERROR in ../revector/src/lib/email-password-top-nav-login/top-nav-login.component.ts
Module not found: Error: Can't resolve '@revector/auth-service' in '/Users/ggranum/github/ggranum/revector/src/lib/email-password-top-nav-login'
@ ../revector/src/lib/email-password-top-nav-login/top-nav-login.component.ts 12:0-68
@ ../revector/src/lib/email-password-top-nav-login/email-password-top-nav-login.module.ts
@ ../revector/src/lib/email-password-top-nav-login/index.ts
@ ./src/app/app.module.ts
@ ./src/app/index.ts
@ ./src/main.ts
@ multi main There's a bash script to create npm links as well (but run the uninstall-revector.sh script first). If nothing else, hopefully this post serves to point out how hard it will be to write modular code in external projects without this feature. Thanks much |
We're using symlink (Windows) and it's working fine with system-config. Have not tested npm link yet. And I agree priority should be increased. Previously our code was quite a mess with all the ../../../.. and ../../.., etc., and requiring changing all the imports when moving stuff around. |
@rolandoldengarm I am now deeply curious: what do you mean when you referenced system-config? Are you using system-config in combination with the webpack version of the CLI? If so, I'd love to hear more... As for symlinks... this is unquestionably a design failure of Node. I have a few horrible, evil ideas about how to make the ' --preserve-symlinks' flag propagate through Node's call chain, but nothing I'd be proud of. And certainly nothing that would 'just work' when I clone my repository onto another machine, for example. It might be possible that there is something relatively simple the CLI team could do to enable symlink'd directories to work properly with node/webpack... but a) I doubt it and b) why bother? Leveraging tsconfig:paths just makes so much more sense. |
@ggranum no sorry, I'm still on the "old" Angular CLI that uses system-config and systemJS. We've got a symlink called "app" in our node_modules folder, pointing to the app folder. This works OK in the "old" CLI, but not with webpack. And, as the webpack angular-cli does not support the way it should work in TS2.0 yet, we're stuck at the old CLI. Very disappointing. |
Just curious, is there any solution for us in the Webpack CLI at this stage? As said, right now we add a symlink to node_modules (Windows) pointing to the app folder. |
This should definitely be bumped in the priority. With the old CLI using systemjs I got it to work by myself (without symlinks in |
index.ts does not work well with webpack either??
so we would be back to
Unacceptable for us. How come not many people see this as priority?? |
The not-working barrels are another issue. I can understand that imports relative to project root are more like syntactic sugar, but it would be really, really, really nice to have, not just "p3: (nice to have)" |
Relative imports are just a configuration change, but as that is blackboxed we cannot change it... But technically it would be a minor change. |
@rolandoldengarm: I'm not sure what you mean about index.ts not working. The scenario you indicate as
You might need to use My assumption is that once Something I've noticed that could possibly be in play here is that webpack doesn't pick up on new paths during the watch builds (e.g. ng serve). So you'll get path not found warnings until you kill the process and start it again. |
@ggranum I don't know about index.ts not working, that's what @christiandreher said. I just don't want relative paths. That's so ugly. It's just a simple configuration change in Webpack, but apparently nobody here thinks it's important enough to bother. |
can some one consolidate a guide on how to config and use this? |
I'm still running into this using the latest release of the cli. The path resolves using ❯ tsc --traceResolution | grep @app
======== Resolving module '@app/types/element.type' from '/path/to/my/project/src/app/components/library/element-list/element-list.component.ts'. ========
'baseUrl' option is set to '/Users/wayne/src/github.com/overhq/over-cms', using this value to resolve non-relative module name '@app/types/element.type'
'paths' option is specified, looking for a pattern to match module name '@app/types/element.type'.
Module name '@app/types/element.type', matched pattern '@app/*'.
======== Module name '@app/types/element.type' was successfully resolved to '/path/to/my/project/src/app/types/element.type.ts'. ======== ❯ ng build
Hash: 3c4a820798b36084d24b
Time: 12929ms
chunk {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 231 kB {4} [initial] [rendered]
chunk {1} main.bundle.js, main.bundle.js.map (main) 260 kB {3} [initial] [rendered]
chunk {2} styles.bundle.js, styles.bundle.js.map (styles) 15.2 kB {4} [initial] [rendered]
chunk {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 4.51 MB [initial] [rendered]
chunk {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]
ERROR in /path/to/my/project/src/app/components/library/element-list/element-list.component.ts (2,23): Cannot find module '@app/types/element.type'.) Here are my versions: {
"dependencies": {
"@angular/common": "2.4.9",
"@angular/compiler": "2.4.9",
"@angular/core": "2.4.9",
"@angular/forms": "2.4.9",
"@angular/http": "2.4.9",
"@angular/platform-browser": "2.4.9",
"@angular/platform-browser-dynamic": "2.4.9",
"@angular/router": "3.4.9",
"@types/moment-timezone": "^0.2.34",
"angular2-notifications": "0.4.53",
"core-js": "2.4.1",
"ie-shim": "0.1.0",
"ionicons": "3.0.0",
"less": "2.7.2",
"lodash": "^4.17.4",
"moment": "2.17.1",
"moment-timezone": "^0.5.11",
"ng2-bs3-modal": "0.10.4",
"ng2-select": "1.2.0",
"pace-progress": "1.0.2",
"rxjs": "5.2.0",
"zone.js": "0.7.8"
},
"devDependencies": {
"@angular/cli": "1.0.0-rc.1",
"@angular/compiler-cli": "2.4.9",
"@types/jasmine": "2.5.38",
"@types/node": "7.0.8",
"codelyzer": "2.0.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "3.2.0",
"karma": "1.4.1",
"karma-chrome-launcher": "2.0.0",
"karma-cli": "1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "5.1.0",
"ts-node": "2.1.0",
"tslint": "4.5.1",
"typescript": "2.2.1"
}
} Here's my {
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "project"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"plugins",
"dist",
"static",
"bootstrap",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"mobile": false,
"styles": [
"styles.css"
],
"environmentSource": "environments/environment.ts",
"environments": {
"local": "environments/environment.local.ts",
"dev": "environments/environment.dev.ts",
"staging": "environments/environment.staging.ts",
"prod": "environments/environment.prod.ts",
"dynamic": "environments/environment.dynamic.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"files": "src/**/*.ts",
"project": "src/tsconfig.app.json"
},
{
"files": "src/**/*.spec.ts",
"project": "src/tsconfig.spec.json"
},
{
"files": "e2e/**/*.ts",
"project": "e2e/tsconfig.e2e.json"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"component": {
"inlineTemplate": false,
"spec": true
}
}
} |
Does anyone know how to resolve this issue? Property paths in tsconfig doesn't help at all. Actually, it helps on .ts compilation phase, but it fails in WebPack then. |
@antonberezan I actually found using the default cli setup, this kinda works out of the box now. You can |
To me is giving me cannot find module. |
Just split up my |
@clydin Is there a similar solution for WebStorm/PHPStorm? Update: Found out the cause and the solution, see https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000158164-Angular-CLI-with-paths-in-tsconfig |
After struggling by searching over internet n trying to understand what exactly problem and trying different troubleshooting option, I came to know baseUrl and Path how works together Note: This solution is for Angular Cli 1.x . Not sure about other tool, If you use baseUrl:"." like below it works in VScode but not while compiling
As far my understanding and my working app and checked in angular aio code, I suggest use as baseUrl:""src like below
By having base URL as source(src directory), compiler properly resolves modules. I hope this helps to people resolve this kind of issue. |
I don't get how something so simple can be so hard to achieve. Fortunately this guide worked for me. In tsconfig.json "compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "."
} in webpack.config.js module.exports = {
"resolve": {
"extensions": [
".ts",
".js"
],
"modules": [
"./node_modules",
".",
]
} Now let's imagine you have the next project structue.
If you wanna import the file example 2 from example1, from now on you can use
|
... and it should be all good. |
Here's how I handled it:
Example: |
They removed the above mentioned solution with If I use something like If I put
into then the editor reports an error inside
|
@abner @filipesilva does the new Is it possible to reopen this issue? |
Seems to work for me if all 3 (
|
Inside
Try to use something like |
For me this worked fine ( "baseUrl": "./",
"paths": {
"~/*": [ "*" ],
"@app/*": [ "src/app/*" ],
"@core/*": [ "src/app/core/*" ],
"@shared/*": [ "src/app/shared/*" ],
"@feature/*": [ "src/app/feature/*" ]
} the critical part that I was missing at the beginning was VIsual Studio recognized the aliases after I restarted it. |
Here is my solution
Be aware that you must restart the CLI each time you make any changes in the tsconfig.json, so that they can take effect, also be aware that although the routes will work, you probably wont have any autocomplete for them. |
Works for me this way (in tsconfig.json only):
And import it:
|
@Xapuu worked for me too. thanks |
@clydin / @filipesilva How do you think we should handle this in a CLI v6 project with libraries ? Especially when following angular/devkit#730 (comment). |
For a library type project, how to have these paths mappings seen by a client application ? |
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. |
It would be very useful to be able to address components via a relative path starting at the project root. A way to do this is to consider the project root as a special project, as if it was inside of
node_modules
as far as resolution is concerned.Such a design greatly reinforces the 'app as a library' idea, and plays nice with most module loading scheme.
A proposed name is
@app
but configurable viaangular-cli.json
, whereapp
is the project prefix specified inng new --prefix app
(this exists already and defaults toapp
).Example:
This would also work for CSS preprocessors.
Such implementation would address #865 and also greatly reduce the need for #900.
The text was updated successfully, but these errors were encountered: