Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 9210a0f

Browse files
committed
refactor(core/method*): Improve character count and speed
1 parent af2599e commit 9210a0f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/core/method/create.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ReturnStyleType, ClassesObjectType, ExactClassesObjectType } from '../../_internal';
2-
import { isDevAndTest, sheetCompiler, isDevelopment, injectServerCSS, genBase36Hash, isServer, injectClientCSS } from '../../_internal';
2+
import { isDevAndTest, sheetCompiler, injectServerCSS, genBase36Hash, isServer, injectClientCSS } from '../../_internal';
33
import { createGlobalStyleSheetPromise, globalStyleSheetPromise, resolveGlobalStyleSheet } from './create-build-in-helper';
44
import styles from '../styles/style.module.css';
55

@@ -9,13 +9,15 @@ export function create<T extends ClassesObjectType>(object: ExactClassesObjectTy
99
if (typeof globalStyleSheetPromise === 'undefined') createGlobalStyleSheetPromise();
1010
resolveGlobalStyleSheet(styleSheet);
1111

12-
return new Proxy(object, {
13-
get: function (target, key: string) {
14-
if (typeof key === 'string' && key in target) {
12+
const injectCSS = isServer ? injectServerCSS : injectClientCSS;
13+
if (isDevAndTest) injectCSS(base36Hash, styleSheet, 'create');
14+
Object.keys(object).forEach(key => {
15+
Object.defineProperty(object, key, {
16+
get: () => {
1517
const className = key + '_' + base36Hash;
16-
if (isDevelopment) isServer ? injectServerCSS(base36Hash, styleSheet, 'create') : injectClientCSS(base36Hash, styleSheet, 'create');
1718
return isDevAndTest ? className : styles[className];
18-
}
19-
},
20-
}) as unknown as ReturnStyleType<T>;
19+
},
20+
});
21+
});
22+
return object as ReturnStyleType<T>;
2123
}

src/core/method/set.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ExtendedCSSProperties } from '../../_internal';
2-
import { isDevAndTest, isDevelopment, injectClientCSS, styleCompiler, genBase36Hash, injectServerCSS, isServer } from '../../_internal';
2+
import { isDevAndTest, injectClientCSS, styleCompiler, genBase36Hash, injectServerCSS, isServer } from '../../_internal';
33
import { createGlobalStyleSheetPromise, globalStyleSheetPromise, resolveGlobalStyleSheet } from './set-build-in-helper';
44
import styles from '../styles/style.module.css';
55

@@ -9,6 +9,7 @@ export function set(object: ExtendedCSSProperties): string {
99
if (typeof globalStyleSheetPromise === 'undefined') createGlobalStyleSheetPromise();
1010
resolveGlobalStyleSheet(styleSheet);
1111

12-
if (isDevelopment) isServer ? injectServerCSS(base36Hash, styleSheet, 'set') : injectClientCSS(base36Hash, styleSheet, 'set');
12+
const injectCSS = isServer ? injectServerCSS : injectClientCSS;
13+
if (isDevAndTest) injectCSS(base36Hash, styleSheet, 'set');
1314
return isDevAndTest ? base36Hash : styles[base36Hash];
1415
}

0 commit comments

Comments
 (0)