-
Notifications
You must be signed in to change notification settings - Fork 67
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
fix: handling of css module imports #220
base: master
Are you sure you want to change the base?
fix: handling of css module imports #220
Conversation
I'm struggling to understand why this is necessary? Can you provide some more details and examples (particularly, test cases)? Right now, I'm hesitant to merge this and seems like there's maybe a configuration issue or something and the tool should already solve this? |
Let me try to explain this as thoroughly as possible: Lets say you have this SCSS file
.my-class {
composes: some-other-class from './some-other.scss'
} Here is the compilation process (at a high-level) where I called out with exclamation marks where the issue occurs. (Look for flowchart TD;
typed-scss-modules{{typed-scss-modules}};
sass{{sass}}
postcss-modules{{postcss-modules}};
parseSass{parse SASS}
extractTypesFromSCSS{extract types from SCSS}
compileCSSModules{compile CSS Modules};
parseCSSModules{parse CSS Modules};
findCSSModuleImports{find CSS Module imports};
typed-scss-modules -- my-scss-file.scss -->extractTypesFromSCSS;
extractTypesFromSCSS -- my-scss-file.scss --> sass;
sass -- my-scss-file.scss --> parseSass;
parseSass -- Plain CSS for my-scss-file.scss --> compileCSSModules;
compileCSSModules -- Plain CSS for my-scss-file.scss --> postcss-modules;
postcss-modules -- Plain CSS for my-scss-file.scss --> parseCSSModules;
parseCSSModules -- Plain CSS for my-scss-files.scss --> findCSSModuleImports;
findCSSModuleImports -- !!!SASS CODE!!! some-other.scss --> postcss-modules;
linkStyle 7 stroke:red;
The solution in this PR is to stub CSS modules that are imported via flowchart TD;
typed-scss-modules{{typed-scss-modules}};
sass{{sass}}
postcss-modules{{postcss-modules}};
parseSass{parse SASS}
extractTypesFromSCSS{extract types from SCSS}
compileCSSModules{compile CSS Modules};
parseCSSModules{parse CSS Modules};
findCSSModuleImports{find CSS Module imports};
typed-scss-modules -- my-scss-file.scss -->extractTypesFromSCSS;
extractTypesFromSCSS -- my-scss-file.scss --> sass;
sass -- my-scss-file.scss --> parseSass;
parseSass -- Plain CSS for my-scss-file.scss --> compileCSSModules;
compileCSSModules -- Plain CSS for my-scss-file.scss --> postcss-modules;
postcss-modules -- Plain CSS for my-scss-file.scss --> parseCSSModules;
parseCSSModules -- Plain CSS for my-scss-files.scss --> findCSSModuleImports;
findCSSModuleImports -- stub for some-other.scss --> postcss-modules;
linkStyle 7 stroke:green;
And to address a comment you made in the previous PR where you asked if I could use a custom (sass) importer to solve the problem. The answer to that is no because the error doesn't happen during the SASS resolve/build process. SASS importers would work if the problem surfaced when using I will follow up with test cases soon |
Note that the diagrams above are mermaid diagrams that seem to only render in Github web. In case anyone opens this in a mobile app and is confused by the code snippet 😅 |
Interesting. I need to think on the diagrams a bit more to fully wrap my head around. Since this error example code is an example included in the |
@skovy That's a valid position to take, and in fact, it was the original position I took when first tackling this problem. However, now I think that this approach is much better because 1. The resulting types will ultimately be the same. And 2. Properly handling css module imports will introduce the need for more SASS configurations, and would be much worse if there is a non-sass CSS Module import -- for example, if a project is mid-migration from some other CSS pre-processor to SASS. Using the commonly used bundlers, its perfectly reasonable that via The drawback with the stubbing approach that I took is that the CSS Module code is not ran in a way the resembles how it is intended to be used. But I think this drawback is suitable for this tool because you should not need this tool to validate the correctness of your code (at least not entirely). As long as it is able to generate the ts files, and surface errors scoped to the config and the individual SASS files, that would be enough to provide the value promised by this tool. The full correctness of the CSS Module tree should be validated by the developer's bundler. |
@skovy I reproduced the error and pushed this up so you can play around with it if you would like. I pushed it as a PR so you can easily inspect the code changes I made to reproduce the error in the original code. The changes just include the addition of a To see the results I ran Expected: test to pass
|
I'm wondering if we make |
@skovy last I checked, the Also, I'm pretty sure you can't replace
@mixin create-class ($suffix) {
.my-dynamic-class-#{$suffix} {
// ommitted
}
}
@include create-class(suffix-1);
@include create-class(suffix-2);
@include create-class(suffix-3); |
As an aside, I'm going to begin stepping back from this project as it has been taking quite long to merge these PRs and a lot of energy explaining how these things work. In my opinion, I think the solution is simple, reliable, and does not have unintended side effects. Feel free to merge it whenever you feel comfortable, close it if you feel strongly in opposition to it, or update it if you find anything worth changing. |
@skovy Checking if you've had a chance to evaluate this change. |
Problem
This tool does not properly handle errors, that is being addressed in #217. Once that fix is applied, some errors that may be occurring for many developers will begin to surface, one of which is addressed by this PR.
The error that this fixes is a compile error caused by the fact that
postcss-modules
only parses valid CSS. So in the scenario where a developer uses the CSS Modules syntax tocompose
or import a value (via@value
) from an SCSS file,postcss-modules
will hit a syntax error if it encounters any SASS specific syntax.Solution
This solution stubs any CSS Module that is imported from the file being compiled.
References
The original discovery of this issue was called out here but the PR had other changes that made it hard to follow: