Skip to content

Commit cd748b2

Browse files
authored
Merge pull request #174 from marcalexiei/processor-css-modules
feat: add processor css modules support
2 parents f1d1721 + 75013b1 commit cd748b2

File tree

13 files changed

+521
-133
lines changed

13 files changed

+521
-133
lines changed

README.md

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Add `allowSyntheticDefaultImports`, or `esModuleInterop` (enables `allowSyntheti
3434
}
3535
```
3636

37-
Reference: (https://www.typescriptlang.org/tsconfig#esModuleInterop)
37+
[`esModuleInterop` reference](https://www.typescriptlang.org/tsconfig#esModuleInterop)
3838

3939
Write rollup.config.ts:
4040

@@ -113,7 +113,7 @@ sass({
113113
});
114114
```
115115

116-
The `processor` also support object result. Reverse `css` filLed for stylesheet, the rest of the properties can be customized.
116+
The `processor` also support object result. Reverse `css` filled for stylesheet, the rest of the properties can be customized.
117117

118118
```js
119119
sass({
@@ -133,29 +133,60 @@ Otherwise, you could do:
133133
import style, { foo, bar } from 'stylesheet';
134134
```
135135

136+
#### Create CSS modules using processor `cssModules` output
137+
138+
When returning a `cssModules` property inside a processor's output,
139+
the plugin will change the module's default export to the value
140+
of `cssModules` instead of the compiled CSS code.
141+
142+
The following example uses [`postcss-modules`](https://www.npmjs.com/package/postcss-modules) to create css modules:
143+
144+
```js
145+
import postcss from 'postcss';
146+
import postcssModules from 'postcss-modules';
147+
148+
sass({
149+
async processor(css, id) {
150+
let cssModules = {};
151+
const postcssProcessResult = await postcss([
152+
postcssModules({
153+
getJSON: (_, json) => {
154+
if (json) cssModules = json;
155+
},
156+
}),
157+
]).process(css, { from: id });
158+
159+
return { css: postcssProcessResult.css, cssModules };
160+
},
161+
});
162+
```
163+
164+
Which allows you to write something like:
165+
166+
```js
167+
import style from 'stylesheet';
168+
169+
style['some-classes'];
170+
```
171+
136172
#### Exporting sass variable to \*.js
137173

138-
Example showing how to use [icss-utils](https://www.npmjs.com/package/icss-utils) to extract resulting sass vars
139-
to your \*.js bundle:
174+
Example showing how to use [`icss-utils`](https://www.npmjs.com/package/icss-utils) to extract resulting sass vars to your \*.js bundle:
140175

141176
```js
142177
const config = {
143178
input: 'test/fixtures/processor-promise/with-icss-exports.js',
144179
plugins: [
145180
sass({
146-
processor: (css) =>
147-
new Promise((resolve, reject) => {
148-
const pcssRootNodeRslt = postcss.parse(css),
149-
extractedIcss = extractICSS(pcssRootNodeRslt, true),
150-
cleanedCss = pcssRootNodeRslt.toString(),
151-
out = Object.assign({}, extractedIcss.icssExports, {
152-
css: cleanedCss,
153-
});
154-
// console.table(extractedIcss);
155-
// console.log(out);
156-
resolve(out);
157-
}),
158-
options: sassOptions,
181+
processor: (css) => {
182+
const pcssRootNodeRslt = postcss.parse(css);
183+
const extractedIcss = extractICSS(pcssRootNodeRslt, true);
184+
const cleanedCss = pcssRootNodeRslt.toString();
185+
const out = { css: cleanedCss, ...extractedIcss.icssExports };
186+
// console.table(extractedIcss);
187+
// console.log(out);
188+
return out;
189+
},
159190
}),
160191
],
161192
};

package-lock.json

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"icss-utils": "^5.1.0",
8686
"nyc": "^17.0.0",
8787
"postcss": "^8.4.16",
88+
"postcss-modules": "^6.0.1",
8889
"prettier": "^3.3.3",
8990
"rollup": "^1 || ^2 || ^3",
9091
"sinon": "^18.0.0",

src/insertStyle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
export default function insertStyle(
1111
css: string | undefined,
1212
): string | undefined {
13-
if (!css || typeof window === 'undefined') {
14-
return;
15-
}
13+
if (!css || typeof window === 'undefined') return;
1614

1715
const style = document.createElement('style');
1816
style.setAttribute('type', 'text/css');

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ export type RollupPluginSassProcessorFnOutput =
1717
| string
1818
| {
1919
css: string;
20+
21+
/** If provided, the default export of the CSS file will be the map returned here */
22+
cssModules?: Record<string, string>;
23+
2024
// User processor might add additional exports
2125
[key: string]: unknown;
2226
};
27+
2328
export type RollupPluginSassProcessorFn<T = RollupPluginSassProcessorFnOutput> =
2429
(styles: string, id: string) => Promise<T> | T;
2530

0 commit comments

Comments
 (0)