Skip to content

Commit 2d058df

Browse files
Merge pull request #35 from joshtym/restructure-of-plugin
refactor: restructure plugin layout for better separation
2 parents d104fdd + 7b77782 commit 2d058df

6 files changed

Lines changed: 554 additions & 537 deletions

File tree

__tests__/test-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import postcss from 'postcss';
22
import nested from 'postcss-nested';
33

4-
import plugin, { PostcssThemeOptions } from '../src/index';
4+
import plugin from '../src/index';
5+
import { PostcssThemeOptions } from '../src/types';
56

67
export function normalizeResult(input: string) {
78
return input

src/common/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import path from 'path';
2+
import { PostcssThemeConfig, PostcssStrictThemeConfig, Theme } from "../types";
3+
4+
/** Get the theme variable name from a string */
5+
export const parseThemeKey = (value: string) => {
6+
const key = value.match(/@theme ([a-zA-Z-_0-9]+)/);
7+
return key ? key[1] : '';
8+
}
9+
10+
/** Replace a theme variable reference with a value */
11+
export const replaceTheme = (value: string, replace: string) => {
12+
return value.replace(/@theme ([a-zA-Z-_0-9]+)/, replace);
13+
}
14+
15+
/** Get the location of the theme file */
16+
export function getThemeFilename(cssFile: string) {
17+
return path.join(path.dirname(cssFile), 'theme');
18+
}
19+
20+
/** Remove :theme-root usage from a selector */
21+
export const replaceThemeRoot = (selector: string) =>
22+
selector.replace(/:theme-root\((\S+)\)/g, '$1').replace(/:theme-root/g, '');
23+
24+
/** Make a SimpleTheme into a LightDarkTheme */
25+
export const normalizeTheme = (config: PostcssThemeConfig | {}): PostcssStrictThemeConfig => {
26+
return Object.assign(
27+
{},
28+
...Object.entries(config).map(([theme, themeConfig]) => {
29+
if ('light' in themeConfig && 'dark' in themeConfig) {
30+
return { [theme]: themeConfig };
31+
}
32+
33+
return { [theme]: { light: themeConfig, dark: {} } };
34+
})
35+
);
36+
};
37+
38+
/** Determine if a theme has dark mode enabled */
39+
export const hasDarkMode = (theme: Theme) =>
40+
Boolean(
41+
Object.keys(theme.dark).length > 0 && Object.keys(theme.light).length > 0
42+
);
43+

0 commit comments

Comments
 (0)