Skip to content

feat: add support for "go to definition" for Sass #137

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

Merged
merged 3 commits into from
Nov 7, 2022
Merged

Conversation

mrmckeb
Copy link
Owner

@mrmckeb mrmckeb commented Jun 12, 2021

This adds experimental support for "go to definition" for Sass.

Resolves #34.

@mrmckeb mrmckeb requested a review from lianapache June 12, 2021 17:20
@mrmckeb mrmckeb mentioned this pull request Jun 12, 2021
Copy link
Collaborator

@lianapache lianapache left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work!
I left a few minor suggestions

if (!cssExports.sourceMap) return dts;

// Create a new source map consumer.
const smc = new SourceMapConsumer(cssExports.sourceMap);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, we could call it sourceMapConsumer instead of smc?

])
.filter(([className]) => isValidVariable(className as string));

filteredClasses.forEach(([className, hashedClassName]) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we use reduce here?
const dtsLines = filteredClasses.reduce(...

} else {
switch (fileType) {
case FileType.less:
less.render(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of breaking this function down into smaller helpers?
I have something like this in mind, e.g.

const getCustomRenderer = (...) => [transformedCss, ]
const lessRenderer = (...) => [transformedCss, ]
const sassRenderer = (...) => [transformedCss, sourceMap]
const getRenderer = (fileType) => renderer

const [transformedCss, sourceMap] = getRenderer(fileType)(...)

It's not very important though, so we can change it later

@carlbergman
Copy link

Hey @mrmckeb, would you like some assistance getting this PR moving?

PS. Noticed that an .only found it's way into one of the files: https://github.com/mrmckeb/typescript-plugin-css-modules/pull/137/files#diff-40e4d369d78e5d7b90768b9601d7f3f22f22cc9ce172e06698ad50e65303d7b0R234

@habovh
Copy link

habovh commented Mar 3, 2022

Hey there, trying to make this work on our current setup.
One of the challenges in our project is that SCSS modules files are automatically prefixed with an import that sets up globally-scoped variables and mixins.

At first I was tinkering with a custom renderer, but I soon realised that getCssExports did not receive the sourceMap from the custom renderer, only the compiled CSS.

So as a POC before thinking about adding the sourceMap option to the custom renderer, I went ahead and modified getCssExports to actually compile a hard-coded template string made of two imports: the "prepend" import and the actual fileName. Here's a source map comparison between the original getCssExports and the modified one:

sourceMap with unmodified getCssExports
{
  "version": 3,
  "sourceRoot": "",
  "sources": [
    "file:///Users/jordanbecker/Repositories/allsquare/react-app/components/club-show/pictures/styles.module.scss"
  ],
  "names": [],
  "mappings": "AAAA;EACE",
  "file": "file:///Users/jordanbecker/Repositories/allsquare/react-app/components/club-show/pictures/styles.module.css"
}
sourceMap with customized getCssExports to include prefix on any module file
{
  "version": 3,
  "sourceRoot": "",
  "sources": [
    "file:///Users/jordanbecker/Repositories/allsquare/react-app/styles/prepend.scss",
    "file:///Users/jordanbecker/Repositories/allsquare/react-app/styles/settings/_sizes.scss",
    "file:///Users/jordanbecker/Repositories/allsquare/react-app/styles/tools/_gradient.scss",
    "file:///Users/jordanbecker/Repositories/allsquare/react-app/components/club-show/pictures/styles.module.scss"
  ],
  "names": [],
  "mappings": "AAAA;AAAA;AAAA;AAAA;ACaA;;AAAA;;AAAA;AAaA;AAIA;AAKA;AAGA;AAGA;AAIA;AAGA;ADtCA;AAAA;AAAA;AAAA;AEVA;EACE;IACE;;EAEF;IACE;;EAGF;IACE;;;AAGJ;EACE;IACE;;EAGF;IACE;;EAGF;IACE;;;AAGJ;EACE;IACE;;EAGF;IACE;;EAGF;IACE;;;ACnCJ;EACE",
  "file": "file:///Users/jordanbecker/Repositories/allsquare/react-app/components/club-show/pictures/styles.module.css"
}

It seems that the format is valid and it should work, but "Go to definition" keeps on landing on the wrong lines of the .module.scss files.

Also, leaving the getCssExports function as-is still fails with the Go to definition for files that do not use any of the global prepended data.

I really tried hard and can't figure out how to get this working properly. Ideas? Am I missing something here, or is this PR not remotely working even for simple cases?

@eterv
Copy link

eterv commented Nov 2, 2022

any update? I really need this feature (scss module).

@mrmckeb mrmckeb merged commit 03f7e78 into main Nov 7, 2022
@mrmckeb mrmckeb deleted the add-sourcemaps-sass branch November 7, 2022 06:20
@bigmeow
Copy link

bigmeow commented Nov 14, 2022

I clone the code to the local computer, build it, and then use the 'yarn link' command to use in the project. However, this feature does not work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Go to definition" does not work right
6 participants