Skip to content

Commit c27d60b

Browse files
committed
Fix define issue, Support source map
1 parent e0777f5 commit c27d60b

File tree

15 files changed

+207
-32
lines changed

15 files changed

+207
-32
lines changed

.changeset/big-plants-camp.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@devup-ui/rsbuild-plugin": patch
3+
"@devup-ui/webpack-plugin": patch
4+
"@devup-ui/wasm": patch
5+
"@devup-ui/vite-plugin": patch
6+
"@devup-ui/react": patch
7+
---
8+
9+
Support source map

.changeset/forty-games-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/vite-plugin": patch
3+
---
4+
5+
Fix define issue

bindings/devup-ui-wasm/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static GLOBAL_STYLE_SHEET: Lazy<Mutex<StyleSheet>> =
1414
pub struct Output {
1515
code: String,
1616
styles: HashSet<ExtractStyleValue>,
17+
map: Option<String>,
1718
}
1819
#[wasm_bindgen]
1920
extern "C" {
@@ -31,6 +32,11 @@ impl Output {
3132
self.code.clone()
3233
}
3334

35+
#[wasm_bindgen(getter)]
36+
pub fn map(&self) -> Option<String> {
37+
self.map.clone()
38+
}
39+
3440
/// Get the css
3541
#[wasm_bindgen(getter)]
3642
pub fn css(&self) -> Option<String> {
@@ -141,6 +147,7 @@ pub fn code_extract(
141147
Ok(output) => Ok(Output {
142148
code: output.code,
143149
styles: output.styles,
150+
map: output.map,
144151
}),
145152
Err(error) => Err(JsValue::from_str(error.to_string().as_str())),
146153
}

libs/extractor/src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ use crate::visit::DevupVisitor;
1111
use oxc_allocator::Allocator;
1212
use oxc_ast::ast::Expression;
1313
use oxc_ast_visit::VisitMut;
14-
use oxc_codegen::Codegen;
14+
use oxc_codegen::{Codegen, CodegenOptions};
1515
use oxc_parser::{Parser, ParserReturn};
1616
use oxc_span::SourceType;
1717
use std::collections::{BTreeMap, HashSet};
1818
use std::error::Error;
19+
use std::path::PathBuf;
1920
#[derive(Debug)]
2021
pub enum ExtractStyleProp<'a> {
2122
Static(ExtractStyleValue),
@@ -80,6 +81,8 @@ pub struct ExtractOutput {
8081

8182
// output source
8283
pub code: String,
84+
85+
pub map: Option<String>,
8386
}
8487

8588
pub struct ExtractOption {
@@ -98,6 +101,7 @@ pub fn extract(
98101
return Ok(ExtractOutput {
99102
styles: HashSet::new(),
100103
code: code.to_string(),
104+
map: None,
101105
});
102106
}
103107
let allocator = Allocator::default();
@@ -113,16 +117,22 @@ pub fn extract(
113117
let mut visitor = DevupVisitor::new(
114118
&allocator,
115119
&option.package,
116-
option
120+
&option
117121
.css_file
118-
.unwrap_or(format!("{}/devup-ui.css", option.package))
119-
.as_str(),
122+
.unwrap_or(format!("{}/devup-ui.css", option.package)),
120123
);
121124
visitor.visit_program(&mut program);
125+
let result = Codegen::new()
126+
.with_options(CodegenOptions {
127+
source_map_path: Some(PathBuf::from(filename)),
128+
..Default::default()
129+
})
130+
.build(&program);
122131

123132
Ok(ExtractOutput {
124133
styles: visitor.styles,
125-
code: Codegen::new().build(&program).code,
134+
code: result.code,
135+
map: result.map.map(|m| m.to_json_string()),
126136
})
127137
}
128138

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"lint": "pnpm -F @devup-ui/* lint",
9+
"pretest": "pnpm -F @devup-ui/vite-plugin build",
910
"test": "cargo tarpaulin --out xml --out stdout --out html && vitest test --coverage --run && pnpm -r test",
1011
"build": "pnpm -F @devup-ui/* build",
1112
"dev": "pnpm -r dev",
@@ -19,7 +20,9 @@
1920
"@changesets/cli": "^2.29.5",
2021
"@types/node": "^24.0.7",
2122
"happy-dom": "^18.0.1",
22-
"@testing-library/react": "^16.3.0"
23+
"@testing-library/react": "^16.3.0",
24+
"@testing-library/jest-dom": "^6.6.3",
25+
"@devup-ui/vite-plugin": "workspace:*"
2326
},
2427
"author": "devfive",
2528
"packageManager": "[email protected]",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Box } from '@devup-ui/react'
2+
import { render } from '@testing-library/react'
3+
4+
describe('Box', () => {
5+
it('should render', () => {
6+
const { container } = render(<Box bg="blue" />)
7+
expect(container.children[0]).toHaveStyle('background-color: blue')
8+
})
9+
})

packages/react/tsconfig.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"compilerOptions": {
3-
"types": ["vite/client", "vitest/importMeta", "vitest/globals"],
3+
"types": [
4+
"vite/client",
5+
"vitest/importMeta",
6+
"vitest/globals",
7+
"@testing-library/jest-dom"
8+
],
49
"strict": true,
510
"target": "ESNext",
611
"declaration": true,
@@ -23,4 +28,4 @@
2328
"baseUrl": ".",
2429
"jsx": "react-jsx"
2530
}
26-
}
31+
}

packages/rsbuild-plugin/src/__tests__/plugin.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ describe('DevupUIRsbuildPlugin', () => {
129129
const App = () => <Box></Box>`,
130130
resourcePath: 'src/App.tsx',
131131
}),
132-
).resolves.toBe('<div></div>')
132+
).resolves.toEqual({
133+
code: '<div></div>',
134+
map: undefined,
135+
})
133136
})
134137
it('should transform with include', async () => {
135138
const plugin = DevupUIRsbuildPlugin({
@@ -157,7 +160,10 @@ const App = () => <Box></Box>`,
157160
const App = () => <Box></Box>`,
158161
resourcePath: 'src/App.tsx',
159162
})
160-
expect(ret).toBe('<div></div>')
163+
expect(ret).toEqual({
164+
code: '<div></div>',
165+
map: undefined,
166+
})
161167
expect(writeFile).toHaveBeenCalledWith(
162168
resolve('.df', 'devup-ui.css'),
163169
expect.stringMatching(/\/\* src\/App\.tsx \d+ \*\//),

packages/rsbuild-plugin/src/plugin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,22 @@ export const DevupUIRsbuildPlugin = ({
5353
: resourcePath.includes('node_modules')
5454
)
5555
return code
56-
const { code: retCode, css } = codeExtract(
57-
resourcePath,
58-
code,
59-
libPackage,
60-
cssFile,
61-
)
56+
const {
57+
code: retCode,
58+
css,
59+
map,
60+
} = codeExtract(resourcePath, code, libPackage, cssFile)
6261

6362
if (css && globalCss.length < css.length) {
6463
globalCss = css
6564
await writeFile(cssFile, `/* ${resourcePath} ${Date.now()} */`, {
6665
encoding: 'utf-8',
6766
})
6867
}
69-
return retCode
68+
return {
69+
code: retCode,
70+
map,
71+
}
7072
},
7173
)
7274
},

packages/vite-plugin/src/__tests__/plugin.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,29 @@ describe('devupUIPlugin', () => {
437437
;(plugin as any).generateBundle({}, bundle)
438438
expect(bundle['devup-ui.css'].source).toBe('no')
439439
})
440+
441+
it('should define process.env.DEVUP_UI_DEFAULT_THEME', () => {
442+
vi.mocked(getDefaultTheme).mockReturnValue('defaultTheme')
443+
const plugin = DevupUI({
444+
package: libPackage,
445+
cssFile,
446+
devupPath,
447+
interfacePath,
448+
})
449+
expect((plugin as any).config().define).toEqual({
450+
'process.env.DEVUP_UI_DEFAULT_THEME': '"defaultTheme"',
451+
})
452+
})
453+
454+
it('should undefine process.env.DEVUP_UI_DEFAULT_THEME', () => {
455+
vi.mocked(getDefaultTheme).mockReturnValue(undefined)
456+
const plugin = DevupUI({
457+
package: libPackage,
458+
cssFile,
459+
devupPath,
460+
interfacePath,
461+
})
462+
expect((plugin as any).config().define).toStrictEqual({})
463+
})
440464
})
441465
})

0 commit comments

Comments
 (0)