Skip to content

Commit 3a6b86c

Browse files
authored
✨ feat: remove @emotion/server to adapt SSR in edge runtime (#169)
1 parent 97f1121 commit 3a6b86c

File tree

6 files changed

+67
-11
lines changed

6 files changed

+67
-11
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
# [3.7.0-beta.1](https://github.com/ant-design/antd-style/compare/v3.6.3...v3.7.0-beta.1) (2024-09-27)
4+
5+
### ✨ Features
6+
7+
- Try to remove @emotion/server to suit SSR in edge runtime ([a7d0394](https://github.com/ant-design/antd-style/commit/a7d0394))
8+
39
## [3.6.3](https://github.com/ant-design/antd-style/compare/v3.6.2...v3.6.3) (2024-09-12)
410

511
### 🐛 Bug Fixes

package.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "antd-style",
3-
"version": "3.6.3",
3+
"version": "3.7.0-beta.1",
44
"description": "css-in-js solution for application combine with antd v5 token system and emotion",
55
"keywords": [
66
"antd",
@@ -69,18 +69,17 @@
6969
]
7070
},
7171
"dependencies": {
72-
"@ant-design/cssinjs": "^1.18.5",
72+
"@ant-design/cssinjs": "^1.21.1",
7373
"@babel/runtime": "^7.24.1",
7474
"@emotion/cache": "^11.11.0",
7575
"@emotion/css": "^11.11.2",
7676
"@emotion/react": "^11.11.4",
7777
"@emotion/serialize": "^1.1.3",
78-
"@emotion/server": "^11.11.0",
7978
"@emotion/utils": "^1.2.1",
8079
"use-merge-value": "^1.2.0"
8180
},
8281
"devDependencies": {
83-
"@ant-design/icons": "^5.3.5",
82+
"@ant-design/icons": "^5.5.1",
8483
"@commitlint/cli": "^17.8.1",
8584
"@emotion/jest": "^11.11.0",
8685
"@emotion/styled": "^11.11.0",
@@ -97,16 +96,16 @@
9796
"@types/testing-library__jest-dom": "^5.14.9",
9897
"@umijs/lint": "^4.1.5",
9998
"@vitest/coverage-v8": "0.34.6",
100-
"antd": "^5.15.3",
99+
"antd": "^5.21.1",
101100
"babel-plugin-antd-style": "^1.0.4",
102101
"chalk": "^4.1.2",
103102
"classnames": "^2.5.1",
104103
"commitlint": "^17.8.1",
105104
"commitlint-config-gitmoji": "^2.3.1",
106105
"concurrently": "^7.6.0",
107106
"cross-env": "^7.0.3",
108-
"dumi": "^2.2.17",
109-
"dumi-theme-antd-style": "latest",
107+
"dumi": "^2.4.12",
108+
"dumi-theme-antd-style": "^0.31.1",
110109
"eslint": "^8.57.0",
111110
"father": "^4.4.0",
112111
"framer-motion": "^8.5.5",

src/functions/extractStaticStyle.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import CacheEntity from '@ant-design/cssinjs/es/Cache';
33
import { EmotionCache } from '@emotion/css/create-instance';
44
import { version } from 'antd';
55

6+
import { createExtractCritical } from '@/utils/createEmotionServer';
7+
68
const createExtractCriticalWithoutHtml = (cache: EmotionCache) => ({
79
ids: Object.keys(cache.inserted),
810
css: Object.values(cache.inserted)
@@ -81,11 +83,11 @@ export const extractStaticStyle = (html?: string, options?: ExtractStyleOptions)
8183
// copy from emotion ssr
8284
// https://github.com/vercel/next.js/blob/deprecated-main/examples/with-emotion-vanilla/pages/_document.js
8385
const styles = global.__ANTD_STYLE_CACHE_MANAGER_FOR_SSR__.getCacheList().map((cache) => {
84-
const createEmotionServer = require('@emotion/server/create-instance').default;
86+
const extractHtml = createExtractCritical(cache);
8587

8688
const result: { ids: string[]; css: string } = !html
8789
? createExtractCriticalWithoutHtml(cache)
88-
: createEmotionServer(cache).extractCritical(html);
90+
: extractHtml(html);
8991

9092
if (!result.css) return null;
9193

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// copy from
2+
// https://github.com/emotion-js/emotion/blob/main/packages/server/src/create-instance/extract-critical.js
3+
4+
import { EmotionCache } from '@emotion/utils';
5+
6+
export const createExtractCritical = (cache: EmotionCache) => {
7+
if (cache.compat !== true) {
8+
// is this good? should we do this automatically?
9+
// this is only for when using the new apis (not emotion or create-emotion)
10+
cache.compat = true;
11+
}
12+
13+
return (html: string) => {
14+
// parse out ids from html
15+
// reconstruct css/rules/cache to pass
16+
let RGX = new RegExp(cache.key + '-([a-zA-Z0-9-_]+)', 'gm');
17+
let o: {
18+
html: string;
19+
ids: string[];
20+
css: string;
21+
} = {
22+
html: html,
23+
ids: [],
24+
css: '',
25+
};
26+
let match;
27+
let ids: Record<string, boolean> = {};
28+
29+
while ((match = RGX.exec(html)) !== null) {
30+
if (ids[match[1]] === undefined) {
31+
ids[match[1]] = true;
32+
}
33+
}
34+
35+
o.ids = Object.keys(cache.inserted).filter((id) => {
36+
if (
37+
(ids[id] !== undefined || cache.registered[cache.key + '-' + id] === undefined) &&
38+
cache.inserted[id] !== true
39+
) {
40+
o.css += cache.inserted[id];
41+
return true;
42+
}
43+
44+
return false;
45+
});
46+
47+
return o;
48+
};
49+
};

tests/functions/__snapshots__/createInstance.test.tsx.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports[`createInstance > 自定义 prefixCls 时,会采用 instance 的 prefi
1212
}
1313
1414
<button
15-
class="cp-btn cp-btn-default emotion-0"
15+
class="cp-btn cp-btn-default cp-btn-color-default cp-btn-variant-outlined emotion-0"
1616
type="button"
1717
>
1818
<span

tests/functions/__snapshots__/createStyles.test.tsx.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ exports[`createStyles > styles 对象的使用 > createStyleFn 通过函数方
2525
}
2626
2727
<button
28-
class="cp-btn cp-btn-default emotion-0"
28+
class="cp-btn cp-btn-default cp-btn-color-default cp-btn-variant-outlined emotion-0"
2929
type="button"
3030
>
3131
<span

0 commit comments

Comments
 (0)