@@ -22,7 +22,7 @@ export default {
22
22
};
23
23
```
24
24
25
- #### rollup.config.ts
25
+ ### rollup.config.ts
26
26
27
27
Add ` allowSyntheticDefaultImports ` , or ` esModuleInterop ` (enables ` allowSyntheticDefaultImports ` ), to tsconfig.json:
28
28
@@ -34,7 +34,7 @@ Add `allowSyntheticDefaultImports`, or `esModuleInterop` (enables `allowSyntheti
34
34
}
35
35
```
36
36
37
- Reference: (https://www.typescriptlang.org/tsconfig#esModuleInterop )
37
+ [ ` esModuleInterop ` reference ] ( https://www.typescriptlang.org/tsconfig#esModuleInterop )
38
38
39
39
Write rollup.config.ts:
40
40
@@ -51,11 +51,12 @@ Profit.
51
51
52
52
### ` output `
53
53
54
- - Type: ` Boolean|String|Function ` _ (default: false)_
54
+ - Type: ` Boolean|String|Function `
55
+ - Default: ` false `
55
56
56
57
``` js
57
58
sass ({
58
- // Default behaviour disable output
59
+ // Default behavior disable output
59
60
output: false ,
60
61
61
62
// Write all styles to the bundle destination where .js is replaced by .css
64
65
// Filename to write all styles
65
66
output: ' bundle.css' ,
66
67
67
- // Callback that will be called ongenerate with two arguments:
68
+ // Callback that will be called on generate bundle with two arguments:
68
69
// - styles: the concatenated styles in order of imported
69
70
// - styleNodes: an array of style objects:
70
71
// [
79
80
80
81
### ` insert `
81
82
82
- - Type: ` Boolean ` _ (default: false)_
83
+ - Type: ` Boolean `
84
+ - Default: ` false `
83
85
84
86
If you specify ` true ` , the plugin will insert compiled CSS into ` <head/> ` tag, via utility function that it will output
85
87
in your build bundle.
@@ -111,7 +113,7 @@ sass({
111
113
});
112
114
```
113
115
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.
115
117
116
118
``` js
117
119
sass ({
@@ -131,29 +133,60 @@ Otherwise, you could do:
131
133
import style , { foo , bar } from ' stylesheet' ;
132
134
```
133
135
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
+
134
172
#### Exporting sass variable to \* .js
135
173
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:
138
175
139
176
``` js
140
177
const config = {
141
178
input: ' test/fixtures/processor-promise/with-icss-exports.js' ,
142
179
plugins: [
143
180
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
+ },
157
190
}),
158
191
],
159
192
};
@@ -164,14 +197,36 @@ the exported vars.
164
197
165
198
### ` runtime `
166
199
167
- - Type: ` Object ` _ (default: sass)_
200
+ - Type: ` Object `
201
+ - Default: ` sass `
168
202
169
203
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).
170
204
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
+
171
223
### ` options `
172
224
173
225
- Type: ` Object `
174
226
227
+ > [ !NOTE]
228
+ > The content of ` options ` is sensible to the value specified in ` api ` option
229
+
175
230
Options for [ sass] ( https://github.com/sass/dart-sass ) or your own runtime sass compiler.
176
231
177
232
If you specify ` data ` , the plugin will treat as prepend sass string.
@@ -185,6 +240,22 @@ sass({
185
240
});
186
241
```
187
242
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
+
188
259
### ` include`
189
260
190
261
- Type: ` string | string[]`
0 commit comments