Skip to content

Commit 7f7295b

Browse files
authored
chore(examples): add css and asset in umd example (#972)
1 parent af5cec4 commit 7f7295b

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed
Lines changed: 7 additions & 0 deletions
Loading

examples/react-component-umd/src/components/CounterButton/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from 'react';
1+
import type React from 'react';
2+
import logo from '../../assets/logo.svg';
23
import styles from './index.module.scss';
34

45
interface CounterButtonProps {
@@ -10,7 +11,12 @@ export const CounterButton: React.FC<CounterButtonProps> = ({
1011
onClick,
1112
label,
1213
}) => (
13-
<button type="button" className={styles.button} onClick={onClick}>
14+
<button
15+
type="button"
16+
className={`${styles.button} counter-button`}
17+
onClick={onClick}
18+
>
19+
<img src={logo} alt="react" />
1420
{label}
1521
</button>
1622
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
.counter-title {
2+
width: 100px;
3+
height: 100px;
4+
background: no-repeat url('./assets/logo.svg');
5+
background-size: cover;
6+
}
7+
18
.counter-text {
29
font-size: 50px;
310
}

examples/react-component-umd/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const Counter: React.FC = () => {
88

99
return (
1010
<div>
11+
<h1 className="counter-title">React</h1>
1112
<h2 className="counter-text">Counter: {count}</h2>
1213
<CounterButton onClick={decrement} label="-" />
1314
<CounterButton onClick={increment} label="+" />

examples/react-component-umd/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"esModuleInterop": true,
88
"forceConsistentCasingInFileNames": true,
99
"isolatedModules": true,
10-
"jsx": "react",
10+
"jsx": "react-jsx",
1111
"lib": ["DOM", "ESNext"],
1212
"moduleResolution": "node",
1313
"resolveJsonModule": true,

tests/e2e/react-component/index.pw.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ async function assetShouldWork(page: Page) {
5151
}
5252
}
5353

54+
async function inlineAssetShouldWork(page: Page) {
55+
// asset in css url('./logo.svg')
56+
const h1El = page.locator('h1');
57+
assert(h1El);
58+
expect(h1El).toHaveCSS('background', /.*data:image\/svg\+xml;base64.*/);
59+
60+
// asset by import url from './assets/logo.svg'
61+
const imgEls = await page.$$('.counter-button>img');
62+
expect(imgEls).toHaveLength(2);
63+
const srcList = await Promise.all(
64+
imgEls.map((imgEl) => imgEl.getAttribute('src')),
65+
);
66+
for (const src of srcList) {
67+
expect(src).toMatch(/.*data:image\/svg\+xml;base64.*/);
68+
}
69+
}
70+
5471
test('should render example "react-component-bundle" successfully', async ({
5572
page,
5673
}) => {
@@ -90,6 +107,7 @@ test('should render example "react-component-umd" successfully', async ({
90107
);
91108
fs.mkdirSync(path.resolve(__dirname, './public/umd'), { recursive: true });
92109
fs.copyFileSync(umdPath, path.resolve(__dirname, './public/umd/index.js'));
110+
fs.copyFileSync(umdPath, path.resolve(__dirname, './public/umd/index.css'));
93111

94112
const rsbuild = await dev({
95113
cwd: __dirname,
@@ -98,5 +116,7 @@ test('should render example "react-component-umd" successfully', async ({
98116
});
99117

100118
await counterCompShouldWork(page);
119+
await styleShouldWork(page);
120+
await inlineAssetShouldWork(page);
101121
await rsbuild.close();
102122
});

tests/e2e/react-component/rsbuild.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export default defineConfig({
4444
head: true,
4545
append: true,
4646
},
47+
{
48+
tag: 'link',
49+
attrs: {
50+
href: '/umd/index.css',
51+
rel: 'stylesheet',
52+
},
53+
head: true,
54+
append: true,
55+
},
4756
],
4857
},
4958
source: {
@@ -62,6 +71,10 @@ export default defineConfig({
6271
from: '../../../examples/react-component-umd/dist/umd/index.js',
6372
to: 'umd/index.js',
6473
},
74+
{
75+
from: '../../../examples/react-component-umd/dist/umd/index.css',
76+
to: 'umd/index.css',
77+
},
6578
{
6679
from: 'node_modules/react-18/umd/react.development.js',
6780
to: 'umd/react.development.js',

0 commit comments

Comments
 (0)