Skip to content

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

Closed

Description

@dereklin

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?

Activity

  1. LeoUrzua commented on Jul 3, 2018

    @LeoUrzua

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

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

  2. dereklin commented on Jul 3, 2018

    @dereklin
    Author

    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/

  3. LeoUrzua commented on Jul 3, 2018

    @LeoUrzua

    Change the app.component.scss to

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

    and name the variables file as "_variables.scss"

    image

  4. dereklin commented on Jul 3, 2018

    @dereklin
    Author

    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.

  5. VitalDoVilmate commented on Jul 3, 2018

    @VitalDoVilmate

    Did you find the way how to solve the issue?

  6. clydin commented on Jul 3, 2018

    @clydin
    Member

    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.

  7. NickLanam commented on Jul 9, 2018

    @NickLanam

    @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.

  8. clydin commented on Jul 9, 2018

    @clydin
    Member

    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.

  9. NickLanam commented on Jul 9, 2018

    @NickLanam

    @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.

  10. damienwebdev commented on Oct 12, 2018

    @damienwebdev

    @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.

  11. NickLanam commented on Oct 12, 2018

    @NickLanam

    @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).

  12. Albejr commented on Dec 8, 2018

    @Albejr

    In my case (Angular 7):

    angular.json

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

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

  13. rtyx commented on May 23, 2019

    @rtyx

    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?

  14. ViieeS commented on Jun 7, 2019

    @ViieeS

    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"

  15. angular-automatic-lock-bot commented on Sep 9, 2019

    @angular-automatic-lock-bot

    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.

  16. locked and limited conversation to collaborators on Sep 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions