11
11
12
12
</div >
13
13
14
+ > 👋** Version 3** migration notice: ` import { discoverProjectStyles } from 'used-styles/node' ` . That's it
15
+
16
+ ---
17
+
14
18
> Bundler and framework independent CSS part of SSR-friendly code splitting
15
19
16
20
Detects used ` css ` files from the given HTML, and/or ** inlines critical styles** . Supports sync or ** stream** rendering.
@@ -149,18 +153,21 @@ Also it may be useful for you, if you want to save on the size of your container
149
153
#### During your build
150
154
151
155
1 . Add separate script to generate style lookup and store it as you like.
156
+
152
157
``` js
153
158
// project/scripts/generate_styles_lookup.mjs
154
- import { serializeStylesLookup , discoverProjectStyles } from ' used-styles'
155
- import { writeFileSync } from ' fs'
159
+ import { serializeStylesLookup , discoverProjectStyles } from ' used-styles' ;
160
+ import { writeFileSync } from ' fs' ;
156
161
157
162
const stylesLookup = discoverProjectStyles (' ./path/to/dist/client' );
158
163
159
164
await stylesLookup;
160
165
161
- writeFileSync (' ./path/to/dist/server/styles-lookup.json' , JSON .stringify (serializeStyles (lookup)))
166
+ writeFileSync (' ./path/to/dist/server/styles-lookup.json' , JSON .stringify (serializeStylesLookup (lookup)));
162
167
```
168
+
163
169
2 . Run this code after your build
170
+
164
171
``` sh
165
172
yarn build
166
173
node ./scripts/generate_styles_lookup.mjs
@@ -171,14 +178,15 @@ Notice, that you can store serialized lookup in any way, that suits you and your
171
178
#### During your runtime
172
179
173
180
1 . Access previously created and stored styles lookup, convert it to ` StyleDef ` with ` loadSerializedLookup ` and use it normally
181
+
174
182
``` js
175
- import { loadSerializedLookup } from ' used-styles'
183
+ import { loadSerializedLookup } from ' used-styles' ;
176
184
177
- const stylesLookup = loadSerializedLookup (require (' ./dist/server/styles-lookup.json' );
185
+ const stylesLookup = loadSerializedLookup (require (' ./dist/server/styles-lookup.json' )) ;
178
186
179
187
// ...
180
188
181
- getCriticalStyles (markup, stylesLookup)
189
+ getCriticalStyles (markup, stylesLookup);
182
190
```
183
191
184
192
# Example
@@ -195,7 +203,8 @@ getCriticalStyles(markup, stylesLookup)
195
203
There is nothing interesting here - just render, just ` getUsedStyles ` .
196
204
197
205
``` js
198
- import {discoverProjectStyles , getUsedStyles } from ' used-styles' ;
206
+ import {getUsedStyles } from ' used-styles' ;
207
+ import {discoverProjectStyles } from ' used-styles/node' ;
199
208
200
209
201
210
// generate lookup table on server start
@@ -245,13 +254,8 @@ similar how StyledComponents works
245
254
246
255
` ` ` js
247
256
import express from ' express' ;
248
- import {
249
- discoverProjectStyles ,
250
- loadStyleDefinitions ,
251
- createCriticalStyleStream ,
252
- createStyleStream ,
253
- createLink ,
254
- } from ' used-styles' ;
257
+ import { discoverProjectStyles } from ' used-styles/node' ;
258
+ import { loadStyleDefinitions , createCriticalStyleStream , createStyleStream , createLink } from ' used-styles' ;
255
259
256
260
const app = express ();
257
261
@@ -380,7 +384,8 @@ That's all are streams, concatenated in a right order. It's possible to interlea
380
384
a ` hydrate` .
381
385
382
386
` ` ` js
383
- import { discoverProjectStyles , createStyleStream , createLink } from ' used-styles' ;
387
+ import { createStyleStream , createLink } from ' used-styles' ;
388
+ import { discoverProjectStyles } from ' used-styles/node' ;
384
389
import MultiStream from ' multistream' ;
385
390
386
391
// .....
0 commit comments