Skip to content

Commit 85668a0

Browse files
committed
Merge branch 'main' of github.com:elycruz/rollup-plugin-sass into dev
2 parents db55ef8 + 62365ca commit 85668a0

18 files changed

+1883
-961
lines changed

README.md

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default {
2222
};
2323
```
2424

25-
#### rollup.config.ts
25+
### rollup.config.ts
2626

2727
Add `allowSyntheticDefaultImports`, or `esModuleInterop` (enables `allowSyntheticDefaultImports`), to tsconfig.json:
2828

@@ -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

@@ -51,11 +51,12 @@ Profit.
5151

5252
### `output`
5353

54-
- Type: `Boolean|String|Function` _(default: false)_
54+
- Type: `Boolean|String|Function`
55+
- Default: `false`
5556

5657
```js
5758
sass({
58-
// Default behaviour disable output
59+
// Default behavior disable output
5960
output: false,
6061

6162
// Write all styles to the bundle destination where .js is replaced by .css
@@ -64,7 +65,7 @@ sass({
6465
// Filename to write all styles
6566
output: 'bundle.css',
6667

67-
// Callback that will be called ongenerate with two arguments:
68+
// Callback that will be called on generate bundle with two arguments:
6869
// - styles: the concatenated styles in order of imported
6970
// - styleNodes: an array of style objects:
7071
// [
@@ -79,7 +80,8 @@ sass({
7980

8081
### `insert`
8182

82-
- Type: `Boolean` _(default: false)_
83+
- Type: `Boolean`
84+
- Default: `false`
8385

8486
If you specify `true`, the plugin will insert compiled CSS into `<head/>` tag, via utility function that it will output
8587
in your build bundle.
@@ -111,7 +113,7 @@ sass({
111113
});
112114
```
113115

114-
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.
115117

116118
```js
117119
sass({
@@ -131,29 +133,60 @@ Otherwise, you could do:
131133
import style, { foo, bar } from 'stylesheet';
132134
```
133135

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+
134172
#### Exporting sass variable to \*.js
135173

136-
Example showing how to use [icss-utils](https://www.npmjs.com/package/icss-utils) to extract resulting sass vars
137-
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:
138175

139176
```js
140177
const config = {
141178
input: 'test/fixtures/processor-promise/with-icss-exports.js',
142179
plugins: [
143180
sass({
144-
processor: (css) =>
145-
new Promise((resolve, reject) => {
146-
const pcssRootNodeRslt = postcss.parse(css),
147-
extractedIcss = extractICSS(pcssRootNodeRslt, true),
148-
cleanedCss = pcssRootNodeRslt.toString(),
149-
out = Object.assign({}, extractedIcss.icssExports, {
150-
css: cleanedCss,
151-
});
152-
// console.table(extractedIcss);
153-
// console.log(out);
154-
resolve(out);
155-
}),
156-
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+
},
157190
}),
158191
],
159192
};
@@ -164,14 +197,36 @@ the exported vars.
164197

165198
### `runtime`
166199

167-
- Type: `Object` _(default: sass)_
200+
- Type: `Object`
201+
- Default: `sass`
168202

169203
If you specify an object, it will be used instead of [sass](https://github.com/sass/dart-sass). You can use this to pass a different sass compiler (for example the `node-sass` npm package).
170204

205+
### `api`
206+
207+
- Type: `'legacy'|'modern'`
208+
- Default: `'legacy'`
209+
210+
```js
211+
sass(); // default to legacy
212+
213+
sass({ api: 'modern' });
214+
215+
sass({
216+
api: 'modern',
217+
options: {
218+
style: 'compressed',
219+
},
220+
});
221+
```
222+
171223
### `options`
172224

173225
- Type: `Object`
174226

227+
> [!NOTE]
228+
> The content of `options` is sensible to the value specified in `api` option
229+
175230
Options for [sass](https://github.com/sass/dart-sass) or your own runtime sass compiler.
176231

177232
If you specify `data`, the plugin will treat as prepend sass string.
@@ -185,6 +240,22 @@ sass({
185240
});
186241
```
187242

243+
---
244+
245+
> [!TIP]
246+
> If your are working with npm packages, consider to use
247+
> [NodePackageImporter](https://sass-lang.com/documentation/js-api/classes/nodepackageimporter/)
248+
>
249+
> ```js
250+
> import * as sass from 'sass';
251+
>
252+
> sass({
253+
> options: {
254+
> importers: [new sass.NodePackageImporter()],
255+
> },
256+
> });
257+
> ```
258+
188259
### `include`
189260
190261
- Type: `string | string[]`

0 commit comments

Comments
 (0)