Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/sass/source-to-class-names.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
import * as proxy from "identity-obj-proxy";
import postcss from "postcss";
import PostcssModulesPlugin from "postcss-modules";

/**
* Use identity-obj-proxy for any imported CSS modules. This is to ignore any
* css module imports via `composes: ` or `@value` statements as the rules to propoerly
* resolve these requests may be totally arbitrary and different accross projects. Additionally,
* this is a simple way to avoid needing to write a sass loader for this tool. (To handle import of
* css modules written in SASS).
*
* The identity-obj-proxy is a simple object that returns the requested key as the value.
*
* So files with the following request
*
* ```css
* .foo {
* composes: bar from "./baz.css";
* }
* ```
*
* will generate something like
*
* ```js
* {
* foo: "foo-HASH123 bar", // the `bar` class is not resolved, and stubbed by the proxy
* }
* ```
*
* This should not affect the final types for a given file, since the types are
* generated only using the classes defined in the current file and not its imports.
*
* This has the added benefit of reducing system calls and disk access,
* so may perform faster in large projects in comparison to properly resolving these requests.
*
* The only drawback is that this does not process the import tree
* the way in which it is intended to be used, so broken imports
* will not be surfaced by this tool.
*/
class IdentityObjProxyLoader {
async fetch() {
return proxy;
}
}

/**
* Converts a CSS source string to a list of exports (class names, keyframes, etc.)
*/
Expand All @@ -11,6 +53,7 @@ export const sourceToClassNames = async (
let result: Record<string, string> = {};
await postcss([
PostcssModulesPlugin({
Loader: IdentityObjProxyLoader,
getJSON: (_, json) => {
result = json;
},
Expand Down
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/reserved-words": "^0.1.0",
"@types/sass": "^1.16.0",
"@types/yargs": "^17.0.8",
"@types/identity-obj-proxy": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"babel-jest": "^29.6.2",
Expand Down Expand Up @@ -87,6 +88,7 @@
}
},
"dependencies": {
"identity-obj-proxy": "^3.0.0",
"bundle-require": "^3.0.4",
"chalk": "4.1.2",
"change-case": "^4.1.2",
Expand Down