Skip to content

Commit d7ee41b

Browse files
authored
Merge pull request #68 from theKashey/v3
V3
2 parents 52b5691 + 64e8f1d commit d7ee41b

19 files changed

+350
-296
lines changed

CHANGELOG.md

+49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
1+
## [2.6.5](https://github.com/theKashey/used-styles/compare/v2.6.4...v2.6.5) (2024-05-02)
2+
3+
## [2.6.4](https://github.com/theKashey/used-styles/compare/v2.6.3...v2.6.4) (2024-03-29)
4+
5+
## [2.6.3](https://github.com/theKashey/used-styles/compare/v2.6.2...v2.6.3) (2024-01-21)
6+
7+
## [2.6.2](https://github.com/theKashey/used-styles/compare/v2.6.1...v2.6.2) (2023-11-15)
8+
9+
## [2.6.1](https://github.com/theKashey/used-styles/compare/v2.6.0...v2.6.1) (2023-11-14)
10+
11+
### Bug Fixes
12+
13+
- expose moveStyles API. fixes [#52](https://github.com/theKashey/used-styles/issues/52) ([02731fd](https://github.com/theKashey/used-styles/commit/02731fd2958dba2ab83773cca7451a25f3078118))
14+
15+
# [2.6.0](https://github.com/theKashey/used-styles/compare/v2.5.2...v2.6.0) (2023-09-16)
16+
17+
### Bug Fixes
18+
19+
- **docs:** typo in style extraction info ([58a704e](https://github.com/theKashey/used-styles/commit/58a704e281f153b4ec65df32c4371a10330e603f))
20+
21+
## [2.5.2](https://github.com/theKashey/used-styles/compare/v2.5.1...v2.5.2) (2023-07-27)
22+
23+
## [2.5.1](https://github.com/theKashey/used-styles/compare/v2.5.0...v2.5.1) (2023-07-27)
24+
25+
# [2.5.0](https://github.com/theKashey/used-styles/compare/v2.4.3...v2.5.0) (2023-07-26)
26+
27+
### Bug Fixes
28+
29+
- use smarter class separation, fixes [#46](https://github.com/theKashey/used-styles/issues/46) ([ee2132d](https://github.com/theKashey/used-styles/commit/ee2132d785c608ea0acdd144470a7293cb95ee5b))
30+
31+
## [2.4.3](https://github.com/theKashey/used-styles/compare/v2.4.2...v2.4.3) (2023-05-13)
32+
33+
### Bug Fixes
34+
35+
- add simple extraction example; ease usage of sync types ([fc75e1a](https://github.com/theKashey/used-styles/commit/fc75e1ae6a43c3b444bfaa4e8bc0b2f576538c57))
36+
37+
## [2.4.2](https://github.com/theKashey/used-styles/compare/v2.4.1...v2.4.2) (2023-02-21)
38+
39+
### Bug Fixes
40+
41+
- add support for class names which are keys from object prototype ([ed2d5f9](https://github.com/theKashey/used-styles/commit/ed2d5f94fa1f20d980b71181c33ae021af5c42fc))
42+
- correct support for multiple classes, fixes [#43](https://github.com/theKashey/used-styles/issues/43) ([fde1497](https://github.com/theKashey/used-styles/commit/fde1497a1a271ac86e320c3056f25a9ed06bf69a))
43+
44+
## [2.4.1](https://github.com/theKashey/used-styles/compare/v2.4.0...v2.4.1) (2022-03-31)
45+
46+
### Bug Fixes
47+
48+
- refactor class introduction ([b85fd08](https://github.com/theKashey/used-styles/commit/b85fd08c3250b6bfc46ddb80a068c493fe2ee1c8))
49+
150
# [2.4.0](https://github.com/theKashey/used-styles/compare/v2.3.3...v2.4.0) (2022-03-31)
251

352
### Features

README.md

+20-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
</div>
1313

14+
> 👋**Version 3** migration notice: `import { discoverProjectStyles } from 'used-styles/node'`. That's it
15+
16+
---
17+
1418
> Bundler and framework independent CSS part of SSR-friendly code splitting
1519
1620
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
149153
#### During your build
150154

151155
1. Add separate script to generate style lookup and store it as you like.
156+
152157
```js
153158
// 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';
156161

157162
const stylesLookup = discoverProjectStyles('./path/to/dist/client');
158163

159164
await stylesLookup;
160165

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)));
162167
```
168+
163169
2. Run this code after your build
170+
164171
```sh
165172
yarn build
166173
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
171178
#### During your runtime
172179

173180
1. Access previously created and stored styles lookup, convert it to `StyleDef` with `loadSerializedLookup` and use it normally
181+
174182
```js
175-
import { loadSerializedLookup } from 'used-styles'
183+
import { loadSerializedLookup } from 'used-styles';
176184

177-
const stylesLookup = loadSerializedLookup(require('./dist/server/styles-lookup.json');
185+
const stylesLookup = loadSerializedLookup(require('./dist/server/styles-lookup.json'));
178186

179187
// ...
180188

181-
getCriticalStyles(markup, stylesLookup)
189+
getCriticalStyles(markup, stylesLookup);
182190
```
183191

184192
# Example
@@ -195,7 +203,8 @@ getCriticalStyles(markup, stylesLookup)
195203
There is nothing interesting here - just render, just `getUsedStyles`.
196204

197205
```js
198-
import {discoverProjectStyles, getUsedStyles} from 'used-styles';
206+
import {getUsedStyles} from 'used-styles';
207+
import {discoverProjectStyles} from 'used-styles/node';
199208

200209

201210
// generate lookup table on server start
@@ -245,13 +254,8 @@ similar how StyledComponents works
245254
246255
```js
247256
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';
255259

256260
const app = express();
257261

@@ -380,7 +384,8 @@ That's all are streams, concatenated in a right order. It's possible to interlea
380384
a `hydrate`.
381385
382386
```js
383-
import { discoverProjectStyles, createStyleStream, createLink } from 'used-styles';
387+
import { createStyleStream, createLink } from 'used-styles';
388+
import { discoverProjectStyles } from 'used-styles/node';
384389
import MultiStream from 'multistream';
385390

386391
// .....

__tests__/react.integration.spec.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import {
88
createCriticalStyleStream,
99
createLink,
1010
createStyleStream,
11-
discoverProjectStyles,
1211
enableReactOptimization,
1312
getCriticalStyles,
1413
getUsedStyles,
1514
parseProjectStyles,
1615
serializeStylesLookup,
1716
loadSerializedLookup,
1817
} from '../src';
18+
import { discoverProjectStyles } from '../src/index-node';
1919
import { StyleDefinition } from '../src/types';
2020

2121
describe('File based css stream', () => {

example/react-18-streaming/src/server.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import express from 'express';
22

33
import {
4-
discoverProjectStyles,
54
createCriticalStyleStream,
65
// createStyleStream,
76
// createLink,
87
} from '../../../';
8+
import { discoverProjectStyles } from '../../../node';
99

1010
import { renderApp } from './entry-server';
1111

example/ssr-react-streaming-ts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"react": "^18.2.0",
1717
"react-dom": "^18.2.0",
1818
"sirv": "^2.0.4",
19-
"used-styles": "^2.6.4"
19+
"used-styles": "^3.0.0"
2020
},
2121
"devDependencies": {
2222
"@types/express": "^4.17.21",
+63-65
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,118 @@
1-
import fs from 'node:fs/promises'
2-
import express from 'express'
3-
import {
4-
discoverProjectStyles,
5-
loadStyleDefinitions,
6-
createCriticalStyleStream,
7-
} from 'used-styles'
1+
import fs from 'node:fs/promises';
2+
3+
import express from 'express';
4+
import { loadStyleDefinitions, createCriticalStyleStream } from 'used-styles';
5+
import { discoverProjectStyles } from 'used-styles/node';
86

97
// Constants
10-
const isProduction = process.env.NODE_ENV === 'production'
11-
const port = process.env.PORT || 5173
12-
const base = process.env.BASE || '/'
13-
const ABORT_DELAY = 10000
8+
const isProduction = process.env.NODE_ENV === 'production';
9+
const port = process.env.PORT || 5173;
10+
const base = process.env.BASE || '/';
11+
const ABORT_DELAY = 10000;
1412

1513
// generate lookup table on server start
1614
const stylesLookup = isProduction
1715
? discoverProjectStyles('./dist/client')
18-
// in dev mode vite injects all styles to <head/> element
19-
: loadStyleDefinitions(async () => [])
16+
: // in dev mode vite injects all styles to <head/> element
17+
loadStyleDefinitions(async () => []);
2018

2119
// Cached production assets
22-
const templateHtml = isProduction
23-
? await fs.readFile('./dist/client/index.html', 'utf-8')
24-
: ''
25-
const ssrManifest = isProduction
26-
? await fs.readFile('./dist/client/.vite/ssr-manifest.json', 'utf-8')
27-
: undefined
20+
const templateHtml = isProduction ? await fs.readFile('./dist/client/index.html', 'utf-8') : '';
21+
const ssrManifest = isProduction ? await fs.readFile('./dist/client/.vite/ssr-manifest.json', 'utf-8') : undefined;
2822

2923
// Create http server
30-
const app = express()
24+
const app = express();
3125

3226
// Add Vite or respective production middlewares
33-
let vite
27+
let vite;
28+
3429
if (!isProduction) {
35-
const { createServer } = await import('vite')
30+
const { createServer } = await import('vite');
31+
3632
vite = await createServer({
3733
server: { middlewareMode: true },
3834
appType: 'custom',
39-
base
40-
})
41-
app.use(vite.middlewares)
35+
base,
36+
});
37+
38+
app.use(vite.middlewares);
4239
} else {
43-
const compression = (await import('compression')).default
44-
const sirv = (await import('sirv')).default
45-
app.use(compression())
46-
app.use(base, sirv('./dist/client', { extensions: [] }))
40+
const compression = (await import('compression')).default;
41+
const sirv = (await import('sirv')).default;
42+
app.use(compression());
43+
app.use(base, sirv('./dist/client', { extensions: [] }));
4744
}
4845

4946
// Serve HTML
5047
app.use('*', async (req, res) => {
5148
try {
52-
await stylesLookup
49+
await stylesLookup;
5350

54-
const url = req.originalUrl.replace(base, '')
51+
const url = req.originalUrl.replace(base, '');
52+
53+
let template;
54+
let render;
5555

56-
let template
57-
let render
5856
if (!isProduction) {
5957
// Always read fresh template in development
60-
template = await fs.readFile('./index.html', 'utf-8')
61-
template = await vite.transformIndexHtml(url, template)
62-
render = (await vite.ssrLoadModule('/src/entry-server.tsx')).render
58+
template = await fs.readFile('./index.html', 'utf-8');
59+
template = await vite.transformIndexHtml(url, template);
60+
render = (await vite.ssrLoadModule('/src/entry-server.tsx')).render;
6361
} else {
64-
template = templateHtml
65-
render = (await import('./dist/server/entry-server.js')).render
62+
template = templateHtml;
63+
render = (await import('./dist/server/entry-server.js')).render;
6664
}
6765

68-
const styledStream = createCriticalStyleStream(stylesLookup)
66+
const styledStream = createCriticalStyleStream(stylesLookup);
6967

70-
let didError = false
68+
let didError = false;
7169

7270
const { pipe, abort } = render(url, ssrManifest, {
7371
onShellError() {
74-
res.status(500)
75-
res.set({ 'Content-Type': 'text/html' })
76-
res.send('<h1>Something went wrong</h1>')
72+
res.status(500);
73+
res.set({ 'Content-Type': 'text/html' });
74+
res.send('<h1>Something went wrong</h1>');
7775
},
7876
// Can use also `onAllReady` callback
7977
onShellReady() {
80-
res.status(didError ? 500 : 200)
81-
res.set({ 'Content-Type': 'text/html' })
78+
res.status(didError ? 500 : 200);
79+
res.set({ 'Content-Type': 'text/html' });
8280

83-
let [htmlStart, htmlEnd] = template.split(`<!--app-html-->`)
81+
let [htmlStart, htmlEnd] = template.split(`<!--app-html-->`);
8482

85-
// React 19 supports document metadata out of box,
83+
// React 19 supports document metadata out of box,
8684
// but for react 18 we can use `react-helmet-async` here:
8785
// htmlStart = htmlStart.replace(`<!--app-head-->`, helmet.title.toString())
88-
89-
res.write(htmlStart)
9086

91-
styledStream.pipe(res, { end: false })
87+
res.write(htmlStart);
88+
89+
styledStream.pipe(res, { end: false });
9290

93-
pipe(styledStream)
91+
pipe(styledStream);
9492

9593
styledStream.on('end', () => {
96-
res.end(htmlEnd)
97-
})
94+
res.end(htmlEnd);
95+
});
9896
},
9997
onError(error) {
100-
didError = true
101-
console.error(error)
98+
didError = true;
99+
console.error(error);
102100
// You can log crash reports here:
103101
// logServerCrashReport(error)
104-
}
105-
})
102+
},
103+
});
106104

107105
setTimeout(() => {
108-
abort()
109-
}, ABORT_DELAY)
106+
abort();
107+
}, ABORT_DELAY);
110108
} catch (e) {
111-
vite?.ssrFixStacktrace(e)
112-
console.log(e.stack)
113-
res.status(500).end(e.stack)
109+
vite?.ssrFixStacktrace(e);
110+
console.log(e.stack);
111+
res.status(500).end(e.stack);
114112
}
115-
})
113+
});
116114

117115
// Start http server
118116
app.listen(port, () => {
119-
console.log(`Server started at http://localhost:${port}`)
120-
})
117+
console.log(`Server started at http://localhost:${port}`);
118+
});

example/ssr-react-streaming/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"react": "^18.2.0",
1717
"react-dom": "^18.2.0",
1818
"sirv": "^2.0.4",
19-
"used-styles": "^2.6.4"
19+
"used-styles": "^3.0.0"
2020
},
2121
"devDependencies": {
2222
"@vitejs/plugin-react": "^4.2.1",

0 commit comments

Comments
 (0)