Skip to content

Commit cfce87e

Browse files
committed
prepare v3
1 parent f963199 commit cfce87e

9 files changed

+75
-58
lines changed

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', () => {

node/package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"main": "../dist/es5/index-node.js",
4+
"jsnext:main": "../dist/es2015/index-node.js",
5+
"module": "../dist/es2015/index-node.js",
6+
"types": "../dist/es2015/index-node.d.ts"
7+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"files": [
3636
"dist",
37-
"moveStyles"
37+
"moveStyles",
38+
"node"
3839
],
3940
"keywords": [
4041
"nodejs",

src/index-node.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { discoverProjectStyles } from './style-discovery';

src/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import { getCriticalRules, extractCriticalRules, getCriticalStyles, getUsedStyle
44
import { alterProjectStyles } from './operations';
55
import { createCriticalStyleStream } from './reporters/critical';
66
import { createStyleStream } from './reporters/used';
7-
import { discoverProjectStyles, loadStyleDefinitions, parseProjectStyles } from './scanForStyles';
8-
7+
import { loadStyleDefinitions, parseProjectStyles } from './style-operations';
98
import { createUsedFilter as createUsedSelectorsFilter } from './utils/cache';
109

11-
export { serializeStylesLookup, loadSerializedLookup } from './serialize';
10+
/**
11+
* @deprecated please import discoverProjectStyles from 'used-styles/node'
12+
*/
13+
export const discoverProjectStyles = () => {
14+
throw new Error("Please import discoverProjectStyles from 'used-styles/node'");
15+
};
1216

17+
export { serializeStylesLookup, loadSerializedLookup } from './serialize';
1318
export { UsedTypes, StyleDefinition, SelectionFilter } from './types';
14-
1519
export {
1620
createUsedSelectorsFilter,
1721
loadStyleDefinitions,
18-
discoverProjectStyles,
1922
parseProjectStyles,
2023
alterProjectStyles,
2124
getUsedStyles,

src/style-discovery.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { readFile } from 'fs/promises';
2+
import { extname, join, relative } from 'path';
3+
4+
// @ts-ignore
5+
import scanDirectory from 'scan-directory';
6+
7+
import { loadStyleDefinitions } from './style-operations';
8+
import { StyleDefinition } from './types';
9+
10+
const RESOLVE_EXTENSIONS = ['.css'];
11+
12+
const getFileContent = (file: string) => readFile(file, 'utf8');
13+
14+
const passAll = () => true;
15+
16+
/**
17+
* auto discovers style files in a given dir applying a given "ordering" filter
18+
* @see Use {@link loadStyleDefinitions} as a full customizable variant
19+
* @param rootDir - location of the build artefact
20+
* @param fileFilter - filter and ordering, return false to skip the file, return true or null to not change file order, sort index otherwise
21+
*/
22+
export function discoverProjectStyles(
23+
rootDir: string,
24+
fileFilter: (fileName: string) => boolean | number | null = passAll
25+
): StyleDefinition {
26+
return loadStyleDefinitions(
27+
async () =>
28+
((await scanDirectory(rootDir, undefined, () => false)) as string[])
29+
.filter((name) => RESOLVE_EXTENSIONS.indexOf(extname(name)) >= 0)
30+
.map((file) => relative(rootDir, file))
31+
.sort(),
32+
(fileName) => getFileContent(join(rootDir, fileName)),
33+
fileFilter
34+
);
35+
}

src/scanForStyles.ts src/style-operations.ts

-35
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
import { readFile } from 'fs';
2-
import { extname, join, relative } from 'path';
3-
4-
import { promisify } from 'util';
5-
6-
// @ts-ignore
7-
import scanDirectory from 'scan-directory';
8-
91
import { StyleAst } from './parser/ast';
102
import { buildAst } from './parser/toAst';
113
import { StyleDefinition, StyleFiles, StylesLookupTable, SyncStyleDefinition } from './types';
124
import { flattenOrder } from './utils/order';
135

14-
const RESOLVE_EXTENSIONS = ['.css'];
15-
16-
const pReadFile = promisify(readFile);
17-
18-
export const getFileContent = (file: string) => pReadFile(file, 'utf8');
19-
206
const toFlattenArray = (ast: StyleAst): StylesLookupTable =>
217
Object.keys(ast).reduce((acc, file) => {
228
ast[file].selectors.forEach((sel) => {
@@ -141,24 +127,3 @@ export function loadStyleDefinitions(
141127

142128
return result;
143129
}
144-
145-
/**
146-
* auto discovers style files in a given dir applying a given "ordering" filter
147-
* @see Use {@link loadStyleDefinitions} as a full customizable variant
148-
* @param rootDir - location of the build artefact
149-
* @param fileFilter - filter and ordering, return false to skip the file, return true or null to not change file order, sort index otherwise
150-
*/
151-
export function discoverProjectStyles(
152-
rootDir: string,
153-
fileFilter: (fileName: string) => boolean | number | null = passAll
154-
): StyleDefinition {
155-
return loadStyleDefinitions(
156-
async () =>
157-
((await scanDirectory(rootDir, undefined, () => false)) as string[])
158-
.filter((name) => RESOLVE_EXTENSIONS.indexOf(extname(name)) >= 0)
159-
.map((file) => relative(rootDir, file))
160-
.sort(),
161-
(fileName) => getFileContent(join(rootDir, fileName)),
162-
fileFilter
163-
);
164-
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"noImplicitReturns": true,
1313
"noFallthroughCasesInSwitch": true,
1414
"noImplicitAny": true,
15-
"removeComments": true,
15+
"removeComments": false,
1616
"importHelpers": true,
1717
"target": "es6",
1818
"moduleResolution": "node",

0 commit comments

Comments
 (0)