Skip to content

Commit 0a8feb4

Browse files
committed
feat: add hydration addon
1 parent 67b1552 commit 0a8feb4

File tree

6 files changed

+53
-35
lines changed

6 files changed

+53
-35
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ __Tiny [5<sup>th</sup> generation](https://github.com/streamich/freestyler/blob/
3434
- [`keyframes()`](./docs/keyframes.md)
3535
- [`unitless`](./docs/unitless.md)
3636
- [`!important`](./docs/important.md)
37-
- [`global`](./docs/global.md)
37+
- [`:global`](./docs/global.md)
3838
- [Server-side rendering](./docs/SSR.md)
3939

4040

addon/cache.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
exports.addon = function (renderer) {
44
var cache = {};
55

6-
renderer.cache = function (styles) {
7-
if (!styles) return '';
6+
renderer.cache = function (css) {
7+
if (!css) return '';
88

9-
var key = renderer.hash(styles);
9+
var key = renderer.hash(css);
1010

1111
if (!cache[key]) {
12-
cache[key] = renderer.rule(styles, key);
12+
cache[key] = renderer.rule(css, key);
1313
}
1414

1515
return cache[key];

addon/hydrate.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
exports.addon = function (renderer, id) {
4+
id = id || 'nano-css';
5+
6+
if (process.env.NODE_ENV !== 'production') {
7+
require('./__dev__/warnOnMissingDependencies')('hydrate', renderer, ['put']);
8+
}
9+
10+
if (renderer.client) {
11+
var hydrated = {};
12+
var stylesheet = document.getElementById(id);
13+
14+
if (!stylesheet) {
15+
if (process.env.NODE_ENV !== 'production') {
16+
console.error('Hydration stylesheet with id "' + id + '" was not found.');
17+
}
18+
19+
return;
20+
}
21+
22+
var cssRules = stylesheet.cssRules;
23+
24+
for (var i = 0; i < cssRules.length; i++) {
25+
var rule = cssRules[i];
26+
hydrated[rule.selectorText] = 1;
27+
}
28+
29+
var put = renderer.put;
30+
31+
renderer.put = function (selector, css) {
32+
if (selector in hydrated) {
33+
if (process.env.NODE_ENV !== 'production') {
34+
// eslint-disable-next-line
35+
console.info('Hydrated selector: ' + selector);
36+
}
37+
38+
return;
39+
}
40+
41+
put(selector, css);
42+
};
43+
}
44+
};

addon/rule.js

+3-28
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,10 @@ exports.addon = function (renderer) {
55
require('./__dev__/warnOnMissingDependencies')('rule', renderer, ['put']);
66
}
77

8-
renderer.rule = function (styles, block) {
9-
if (!block) {
10-
block = renderer.hash(styles);
11-
}
12-
13-
if (process.env.NODE_ENV !== 'production') {
14-
if (renderer.client) {
15-
/*
16-
if (document.getElementById('css-' + block)) {
17-
console.error(
18-
'ezcss detected class name collision "css-' + block + '". ' +
19-
'Multiple components use the same class name.'
20-
);
21-
}
22-
*/
23-
if (renderer.cns[block]) {
24-
if (process.env.NODE_ENV !== 'production') {
25-
// eslint-disable-next-line
26-
console.info('Hydration cache hit: "' + block + '"');
27-
}
28-
29-
return;
30-
}
31-
}
32-
}
33-
8+
renderer.rule = function (css, block) {
9+
block = block || renderer.hash(css);
3410
block = renderer.pfx + block;
35-
renderer.put('.' + block, styles);
36-
renderer.cns[block] = 1;
11+
renderer.put('.' + block, css);
3712

3813
return ' ' + block;
3914
};

docs/Addons.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ plenty more to chose from. Below is a list of addons shipped with `nano-css`.
2222
- [`keyframes()`](./keyframes.md)
2323
- [`unitless`](./unitless.md)
2424
- [`!important`](./important.md)
25-
- [`global`](./global.md)
25+
- [`:global`](./global.md)
2626

2727

2828
## Addon Installation

index.js

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ exports.create = function (config) {
1616

1717
var renderer = assign({
1818
raw: '',
19-
cns: {},
2019
pfx: '_',
2120
client: typeof window === 'object',
2221
assign: assign,

0 commit comments

Comments
 (0)