Skip to content

[scss] Consider custom aliases in module resolution #131954

Closed

Description

@HughxDev

Please read from tsconfig/jsconfig’s compilerOptions.paths—and perhaps other popular locations such as webpack.config’s resolve.alias—when providing IntelliSense features to SCSS/Sass files. For instance, if I specify:

tsconfig.json / jsconfig.json

{
  "compilerOptions": {
    "paths": {
      "~/*": [
        "src/*"
      ]
    }
  }
}

or

webpack.config.js

module.exports = {
  resolve: {
    alias: {
      '~': path.resolve( __dirname, 'src' ),
    }
  }
};

…then I want to get IntelliSense features for this, such as cmd-clicking on the underlined import to jump to the relevant file. Currently if I do this, e.g. in:

src/components/Foo/Foo.scss

@import '~/globals';

.foo {
  /* … */
}

…VS Code looks for src/components/Foo/~/globals, when I want it to look for src/globals, as I’ve set it up. (I understand that the tilde character already maps to node_modules in Sass, but this is a tilde + a forward slash, and the character could just as easily be @ or anything else.)

Thank you for your consideration.

Activity

  1. aeschli commented on Aug 31, 2021

    @aeschli
    Contributor

    The built-in scss support does not load any imports. See #100217

    The only thing we have is a link on the Import (A place for python bower components) (@import) that is aware of the ~ to load from node modules.

    How does the tsconfig file impact a scss file? Isn't it only used for TypeScript?

  2. HughxDev commented on Aug 31, 2021

    @HughxDev
    Author

    Martin Aeschlimann (@aeschli) It doesn’t necessarily, but it seems to be VS Code’s intent that jsconfig/tsconfig are as much for the IDE as they are for the compiler. From the docs on jsconfig:

    The presence of jsconfig.json file in a directory indicates that the directory is the root of a JavaScript Project. The jsconfig.json file specifies the root files and the options for the features provided by the JavaScript language service.

    While the section on tsconfig doesn’t say exactly the same thing, it stands to reason that it serves a similar function to jsconfig but for TypeScript projects.

    I only suggest these files because they are already being read by VS Code to modify its behavior. The configuration that actually impacts SCSS files in my workflows is webpack.config.js, but I know there are plenty of bundlers out there so maybe VS Code doesn’t want to play favorites, or go down a path of including OOTB support for every bundler.

    Specifically, SCSS files are impacted if you use a loader that respects Webpack’s module resolution rules while following Sass imports. This is how webpack-contrib/sass-loader works, the most popular library for doing this in that ecosystem. For instance if you do this:

    webpack.config.js

    module.exports = {
      resolve: {
        alias: {
          '~': path.resolve( __dirname, 'src' ),
        }
      },
      module: {
        rules: [
          {
            test: /\.(s[ac]ss|css)$/i,
            use: ['style-loader', 'css-loader', 'sass-loader'],
          },
        ],
      },
    }

    …then doing @import '~/foo' will reference module.exports.resolve.alias and resolve to @import '/path/to/src/foo.scss' (or /path/to/src/foo/index.scss, as the case may be), without any Sass-specific configuration. So the request here is to allow me to tell VS Code to resolve modules the same way that my bundler does. Currently there is a disconnect between the two, such that when I Cmd + click on a Sass import, it fails to bring me to the appropriate file—it looks for current-directory/~/foo and fails, bringing up an alert box about how it can’t find anything.

    However, if I Cmd + click on an identically aliased path inside of JS/TS, it works, because VS Code looks at jsconfig/tsconfig’s compilerOptions.paths, where I have set ~/* to src/*.

  3. aeschli commented on Sep 1, 2021

    @aeschli
    Contributor

    tsconfig.json is the well-defined way of configuring a TypeScript projects.
    From what I know, for scss there's no such standard. Getting the information out of webpack, or any other build script is hard and error prone. We might not understand the full webpack config or build script and we might confuse users with wrong results. That's why I don't think we should go that way. We might fix your use case but get it wrong for others.

    And, as said, currently , all that would be good for it to get the link right. We don't do any loading of imports. If we were to do that, I agree this is a problem we would have to solve (e.g. by introducing a scss configuration file).

    Closing as out of scope / won't fix.

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

Metadata

Metadata

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