Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"updateInternalDependencies": "patch",
"ignore": [
"*-example",
"*-benchmark"
"*-benchmark",
"landing"
]
}
5 changes: 5 additions & 0 deletions .changeset/eleven-days-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/wasm": patch
---

Optimize media and Change sorting css var
5 changes: 5 additions & 0 deletions .changeset/loud-carrots-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@devup-ui/webpack-plugin": patch
---

Fix collecting css
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
if: github.ref == 'refs/heads/main'
1 change: 1 addition & 0 deletions apps/landing/devup.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"base": "#000000",
"negativeBase": "#FFFFFF",
"title": "#FAFAFA",
"caption": "#A9A8B4",
"shadow": "#62626240",
"codeBg": "#2E303C",
"cardBg": "#28272B",
Expand Down
5 changes: 5 additions & 0 deletions apps/landing/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default function RootLayout({
}>) {
return (
<html lang="en">
<head>
<base
href={process.env.NODE_ENV === 'production' ? '/devup-ui' : '/'}
/>
</head>
<body>
{children}
<Footer />
Expand Down
2 changes: 1 addition & 1 deletion libs/sheet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl StyleSheet {
sorted_props.append(&mut select_props);

let inner_css = sorted_props
.iter()
.into_iter()
.map(|prop| prop.extract())
.collect::<Vec<String>>()
.join("");
Expand Down
61 changes: 42 additions & 19 deletions libs/sheet/src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};

pub struct ColorTheme {
data: HashMap<String, String>,
Expand Down Expand Up @@ -71,15 +71,15 @@ impl Color {
.map(Some)
.unwrap_or_else(|| self.themes.keys().next());
if let Some(default_theme_key) = default_theme_key {
for (theme_name, theme_properties) in self.themes.iter() {
let mut entries: Vec<_> = self.themes.iter().collect();
entries.sort_by_key(|(k, _)| *k);
entries.reverse();
for (theme_name, theme_properties) in entries {
let theme_key = if *theme_name == *default_theme_key {
None
} else {
Some(theme_name)
};
if theme_properties.is_empty() {
continue;
}
if let Some(theme_key) = theme_key {
theme_declaration
.push_str(format!(":root[data-theme={}]{{", theme_key).as_str());
Expand Down Expand Up @@ -127,7 +127,7 @@ impl Typography {
pub struct Theme {
pub colors: Color,
pub break_points: Vec<u16>,
pub typography: HashMap<String, Vec<Typography>>,
pub typography: BTreeMap<String, Vec<Typography>>,
}

impl Default for Theme {
Expand All @@ -141,7 +141,7 @@ impl Theme {
Self {
colors: Color::new(),
break_points: vec![0, 480, 768, 992, 1280],
typography: HashMap::new(),
typography: BTreeMap::new(),
}
}

Expand All @@ -166,6 +166,7 @@ impl Theme {

pub fn to_css(&self) -> String {
let mut css = self.colors.to_css();
let mut level_map = BTreeMap::<u8, Vec<String>>::new();
for ty in self.typography.iter() {
for t in ty.1.iter() {
let css_content = format!(
Expand Down Expand Up @@ -195,17 +196,24 @@ impl Theme {
continue;
}
let typo_css = format!(".typo-{}{{{}}}", ty.0, css_content);

if t.level == 0 {
css.push_str(typo_css.as_str());
} else {
let media = self
.break_points
.get(t.level as usize)
.map(|v| format!("(min-width:{}px)", v));
if let Some(media) = media {
css.push_str(format!("\n@media {}{{{}}}", media, typo_css).as_str());
}
level_map
.get_mut(&t.level)
.map(|v| v.push(typo_css.clone()))
.unwrap_or_else(|| {
level_map.insert(t.level, vec![typo_css.clone()]);
});
}
}
for (level, css_vec) in level_map {
if level == 0 {
css.push_str(css_vec.join("").as_str());
} else {
let media = self
.break_points
.get(level as usize)
.map(|v| format!("(min-width:{}px)", v));
if let Some(media) = media {
css.push_str(format!("\n@media {}{{{}}}", media, css_vec.join("")).as_str());
}
}
}
Expand All @@ -227,6 +235,9 @@ mod tests {
let mut color_theme = ColorTheme::new();
color_theme.add_color("primary".to_string(), "#000".to_string());
theme.add_color_theme("default".to_string(), color_theme);
let mut color_theme = ColorTheme::new();
color_theme.add_color("primary".to_string(), "#fff".to_string());
theme.add_color_theme("dark".to_string(), color_theme);
theme.add_typography(
"default".to_string(),
vec![
Expand All @@ -248,10 +259,22 @@ mod tests {
),
],
);

theme.add_typography(
"default1".to_string(),
vec![Typography::new(
Some("Arial".to_string()),
Some("24px".to_string()),
Some("400".to_string()),
Some("1.5".to_string()),
Some("0.5".to_string()),
1,
)],
);
let css = theme.to_css();
assert_eq!(
css,
":root{--primary:#000;}\n.typo-default{font-family:Arial;font-size:16px;font-weight:400;line-height:1.5;letter-spacing:0.5}\n@media (min-width:480px){.typo-default{font-family:Arial;font-size:24px;font-weight:400;line-height:1.5;letter-spacing:0.5}}"
":root{--primary:#000;}\n:root[data-theme=dark]{--primary:#fff;}\n.typo-default{font-family:Arial;font-size:16px;font-weight:400;line-height:1.5;letter-spacing:0.5}\n@media (min-width:480px){.typo-default{font-family:Arial;font-size:24px;font-weight:400;line-height:1.5;letter-spacing:0.5}.typo-default1{font-family:Arial;font-size:24px;font-weight:400;line-height:1.5;letter-spacing:0.5}}"
);
}
}
4 changes: 4 additions & 0 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"./loader": {
"import": "./dist/loader.js",
"require": "./dist/loader.cjs"
},
"./css-loader": {
"import": "./dist/css-loader.js",
"require": "./dist/css-loader.cjs"
}
},
"files": [
Expand Down
16 changes: 16 additions & 0 deletions packages/webpack-plugin/src/__tests__/css-loader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getCss } from '@devup-ui/wasm'

import devupUICssLoader from '../css-loader'

vi.mock('@devup-ui/wasm')

describe('devupUICssLoader', () => {
it('should invoke callback', () => {
vi.mocked(getCss).mockReturnValue('css')
const callback = vi.fn()
devupUICssLoader.bind({
callback,
} as any)(Buffer.from(''), '')
expect(callback).toBeCalledWith(null, 'css')
})
})
7 changes: 7 additions & 0 deletions packages/webpack-plugin/src/css-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getCss } from '@devup-ui/wasm'
import type { RawLoaderDefinitionFunction } from 'webpack'

const devupUICssLoader: RawLoaderDefinitionFunction = function () {
this.callback(null, getCss())
}
export default devupUICssLoader
12 changes: 12 additions & 0 deletions packages/webpack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ export class DevupUIWebpackPlugin {
if (!existsSync(this.options.cssFile)) {
writeFileSync(this.options.cssFile, '', { encoding: 'utf-8' })
}

compiler.options.module.rules.push({
test: this.options.cssFile,
use: [
{
loader: createRequire(import.meta.url).resolve(
'@devup-ui/webpack-plugin/css-loader',
),
},
],
})

compiler.options.module.rules.push({
test: /\.(tsx|ts|js|mjs|jsx)$/,
exclude: /node_modules/,
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-plugin/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default defineConfig({
entry: {
index: 'src/index.ts',
loader: 'src/loader.ts',
['css-loader']: 'src/css-loader.ts',
},
},
outDir: 'dist',
Expand Down
Loading