Skip to content

Does Angular Cli 6 support @import ~ anymore? #11435

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

Closed
dereklin opened this issue Jul 3, 2018 · 15 comments
Closed

Does Angular Cli 6 support @import ~ anymore? #11435

dereklin opened this issue Jul 3, 2018 · 15 comments

Comments

@dereklin
Copy link

dereklin commented Jul 3, 2018

I just create a brand new app with angular cli 6. I tried use do

@import '~styles/variables';

But I got file not found error.

Here is my code:

https://stackblitz.com/github/dereklin/myng6app?file=src%2Fapp%2Fapp.component.scss

Is @import ~ no longer supported?

@LeoUrzua
Copy link

LeoUrzua commented Jul 3, 2018

Rename the file from "/src/styles/_variables.scss" to "/src/styles/variables.scss"

"import" is part of typescript and angular 6 implements typescript

@dereklin
Copy link
Author

dereklin commented Jul 3, 2018

That's interesting. While stackblitz appears to work, running ng serve locally still produces the same error after change the file name:

ERROR in ./src/app/app.component.scss
Module build failed: 
@import '~styles/variables';
^
      File to import not found or unreadable: ~styles/variables.
      in /Users/delin/sandbox/myng6app/src/app/app.component.scss (line 1, column 1)
ℹ 「wdm」: Failed to compile.

Here is my repo: https://github.com/dereklin/myng6app

On another note, stackblitz seems to cache my old app. When I do https://stackblitz.com/github/dereklin/myng6app, I can still see _variables.scss instead of the newly named variable.scss.

But I really think _variables.scss is the right file name since they are partials and I don't want sass to compile them: https://kolosek.com/sass-import/

@LeoUrzua
Copy link

LeoUrzua commented Jul 3, 2018

Change the app.component.scss to

@import '../../styles/variables

and name the variables file as "_variables.scss"

image

@dereklin
Copy link
Author

dereklin commented Jul 3, 2018

But that's relative import. My original question is regarding the support of @import ~. It used to work before my upgrading to Angular 6. Some claim it's still working (storybookjs/storybook#3814). I want to find out if it's indeed supported still.

@VitalDoVilmate
Copy link

Did you find the way how to solve the issue?

@clydin
Copy link
Member

clydin commented Jul 3, 2018

The usage of the tilde was/is not officially supported and is actual an artifact of the internal implementation of the build system. While it may currently work in certain situations, it is not guaranteed to continue to function in the future. In the style use cases outlined above, a relative path or the use of the includePaths option (more details) is recommended.

@NickLanam
Copy link

NickLanam commented Jul 9, 2018

@clydin Is this a change of stance? On #9166 and #9184 (back in January), you noted that this syntax being unsupported was a defect. It also appears that there are unit tests in 6.0.x that check if tilde imports work.

Has something changed in policy?

EDIT: For what it's worth, I'm seeing the same behavior in a fresh v6 project using LESS, so the symptom isn't unique to SCSS.

@clydin
Copy link
Member

clydin commented Jul 9, 2018

The Angular Material documentation contained a usage of the tilde and to prevent wide spread breakage a set of tests for a limited usage of the tilde were added at that time. These tests were carried over into 6.x to attempt to ease transition. However, there is currently not a guarantee that the tilde will continue to function (even in the tested limited capacity) moving into the future.

@NickLanam
Copy link

NickLanam commented Jul 9, 2018

@clydin I see. Given that this functionality happens to work sometimes (despite not being intentionally supported), what sort of work do you think it would take to make it intentionally supported? I'd be interested in taking a stab at it if it's not too hairy. It's quite a handy thing to have, after all.

@damienwebdev
Copy link

@NickLanam I've proposed an idea that may solve this issue in #12573. Let me know if that would handle your use-case, especially if @clydin is unwilling to commit to supporting ~ this may be a feasible alternative.

@NickLanam
Copy link

@damienwebdev That proposal would add a fair bit of flexibility, but I'm not sure that it achieves the same use case. In the issue here, a working implementation avoids the need for manually specifying where to find each thing. It defaults to finding node_modules (no matter how deep in the tree the file you're working on might be).

@Albejr
Copy link

Albejr commented Dec 8, 2018

In my case (Angular 7):

angular.json

"projects": {
    "MyProject": {
      "sourceRoot": "src"
}}

styles.scss
@import "assets/css/variables.scss";

@rtyx
Copy link

rtyx commented May 23, 2019

What about server side applications?

In my angular.json file, adding:

"stylePreprocessorOptions": {
							"includePaths": [
								"src/styles"
							]
						},

to the architect.build.options seems to work, but it fails with ng run myapplication:server:dev.

Do I have to add it also in architect.server.options?

@ViieeS
Copy link

ViieeS commented Jun 7, 2019

In my case ~src label doesn't work only for SSR, because I can't define assets in options of the server scheme:

"assets": [
   "src/assets"
]

When I try to do so I get an error:

Schema validation failed with the following errors:
  Data path "" should NOT have additional properties(assets).

stylePreprocessorOptions > includePaths working though...

"@angular-devkit/build-angular": "^0.13.9"
"@angular/cli": "^7.3.9"

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants