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
1 change: 1 addition & 0 deletions examples/react-component-bundle/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default defineConfig({
],
output: {
target: 'web',
assetPrefix: 'auto', // TODO: move this line to packages/core/src/asset/assetConfig.ts
},
plugins: [pluginReact(), pluginSass()],
});
7 changes: 7 additions & 0 deletions examples/react-component-bundle/src/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions examples/react-component-bundle/src/index.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.counter-title {
width: 100px;
height: 100px;
background: no-repeat url('./assets/logo.svg');
background-size: cover;
}

.counter-text {
font-size: 50px;
}
1 change: 1 addition & 0 deletions examples/react-component-bundle/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const Counter: React.FC = () => {

return (
<div>
<h1 className="counter-title">React</h1>
<h2 className="counter-text">Counter: {count}</h2>
<CounterButton onClick={decrement} label="-" />
<CounterButton onClick={increment} label="+" />
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/asset/assetConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { RsbuildConfig } from '@rsbuild/core';
import type { Format } from '../types';

export const composeAssetConfig = (
bundle: boolean,
format: Format,
): RsbuildConfig => {
if (format === 'esm' || format === 'cjs') {
if (bundle) {
return {
output: {
dataUriLimit: 0, // default: no inline asset
// assetPrefix: 'auto', // TODO: will turn on this with js support together in the future
},
};
}
// TODO: bundleless
return {};
}

// mf and umd etc
return {};
};
4 changes: 4 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
rspack,
} from '@rsbuild/core';
import { glob } from 'tinyglobby';
import { composeAssetConfig } from './asset/assetConfig';
import {
DEFAULT_CONFIG_EXTENSIONS,
DEFAULT_CONFIG_NAME,
Expand Down Expand Up @@ -1221,6 +1222,8 @@ async function composeLibRsbuildConfig(
cssModulesAuto,
);
const cssConfig = composeCssConfig(lcp, config.bundle);
const assetConfig = composeAssetConfig(bundle, format!);

const entryChunkConfig = composeEntryChunkConfig({
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
});
Expand Down Expand Up @@ -1251,6 +1254,7 @@ async function composeLibRsbuildConfig(
targetConfig,
entryConfig,
cssConfig,
assetConfig,
entryChunkConfig,
minifyConfig,
dtsConfig,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
"mode": "production",
"output": {
"dataUriLimit": 0,
"distPath": {
"css": "./",
"cssAsync": "./",
Expand Down Expand Up @@ -248,6 +249,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
"mode": "production",
"output": {
"dataUriLimit": 0,
"distPath": {
"css": "./",
"cssAsync": "./",
Expand Down
18 changes: 15 additions & 3 deletions tests/e2e/react-component/index.pw.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import { type Page, expect, test } from '@playwright/test';
Expand Down Expand Up @@ -27,9 +28,19 @@ async function styleShouldWork(page: Page) {

const buttonEl = page.locator('#root button');
const [subtractEl, addEl] = await buttonEl.all();
subtractEl &&
expect(subtractEl).toHaveCSS('background-color', 'rgb(255, 255, 0)');
addEl && expect(addEl).toHaveCSS('background-color', 'rgb(255, 255, 0)');
assert(subtractEl);
assert(addEl);
expect(subtractEl).toHaveCSS('background-color', 'rgb(255, 255, 0)');
expect(addEl).toHaveCSS('background-color', 'rgb(255, 255, 0)');
}

async function assetShouldWork(page: Page) {
// asset in css url('./logo.svg')
const h1El = page.locator('h1');
assert(h1El);
expect(h1El).toHaveCSS('background', /static\/svg\/logo/);

// TODO: asset in js
}

test('should render example "react-component-bundle" successfully', async ({
Expand All @@ -43,6 +54,7 @@ test('should render example "react-component-bundle" successfully', async ({

await counterCompShouldWork(page);
await styleShouldWork(page);
await assetShouldWork(page);
await rsbuild.close();
});

Expand Down
1 change: 1 addition & 0 deletions tests/e2e/react-component/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default defineConfig({
},
output: {
target: 'web',
dataUriLimit: 0, // always emit asset for test
},
plugins: [
pluginReact({
Expand Down
9 changes: 6 additions & 3 deletions tests/integration/asset/limit/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ export default defineConfig({
distPath: {
root: './dist/esm/inline',
},
dataUriLimit: {
svg: 4096,
},
},
}),
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/external',
},
dataUriLimit: {
svg: 0,
},
},
}),
generateBundleEsmConfig({
Expand All @@ -26,6 +26,9 @@ export default defineConfig({
distPath: {
root: './dist/esm/inline-bundleless',
},
dataUriLimit: {
svg: 4096,
},
},
}),
generateBundleEsmConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
body {
// background: url(./logo.svg);
background: url(./logo.svg);
}
9 changes: 4 additions & 5 deletions tests/integration/style/sass/__fixtures__/src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// @import '~lib1/index.css';
@import './foundation/index.scss';

// TODO: asset support
// $url: './foundation/logo.svg';
$url: './foundation/logo.svg';
$border-dark: rgba($base-color, 0.88);

// .url-variable {
// background: url($url);
// }
.url-variable {
background: url($url);
}

.alert {
border: 1px solid $border-dark;
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/style/sass/bundle/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export default defineConfig({
],
output: {
target: 'web',
// dataUriLimit: {
// svg: 0,
// },
assetPrefix: 'auto', // TODO: move this line to packages/core/src/asset/assetConfig.ts
},
});
2 changes: 2 additions & 0 deletions tests/integration/style/sass/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test('should extract css with pluginSass in bundle-false', async () => {
[
"<ROOT>/tests/integration/style/sass/bundle-false/dist/esm/foundation/_code.css",
"<ROOT>/tests/integration/style/sass/bundle-false/dist/esm/foundation/_lists.css",
"<ROOT>/tests/integration/style/sass/bundle-false/dist/esm/foundation/index.css",
"<ROOT>/tests/integration/style/sass/bundle-false/dist/esm/index.css",
]
`);
Expand All @@ -38,6 +39,7 @@ test('should extract css with pluginSass in bundle-false', async () => {
[
"<ROOT>/tests/integration/style/sass/bundle-false/dist/cjs/foundation/_code.css",
"<ROOT>/tests/integration/style/sass/bundle-false/dist/cjs/foundation/_lists.css",
"<ROOT>/tests/integration/style/sass/bundle-false/dist/cjs/foundation/index.css",
"<ROOT>/tests/integration/style/sass/bundle-false/dist/cjs/index.css",
]
`);
Expand Down
Loading