Skip to content

Commit 5c294d8

Browse files
authored
feat: support banner and footer in bundleless emitted css (#712)
1 parent dfe8301 commit 5c294d8

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

packages/core/src/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,12 @@ async function composeLibRsbuildConfig(
14141414
rootPath,
14151415
cssModulesAuto,
14161416
);
1417-
const cssConfig = composeCssConfig(lcp, config.bundle);
1417+
const cssConfig = composeCssConfig(
1418+
lcp,
1419+
config.bundle,
1420+
banner?.css,
1421+
footer?.css,
1422+
);
14181423
const assetConfig = composeAssetConfig(bundle, format!);
14191424

14201425
const entryChunkConfig = composeEntryChunkConfig({

packages/core/src/css/cssConfig.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ export async function cssExternalHandler(
130130

131131
const PLUGIN_NAME = 'rsbuild:lib-css';
132132

133-
const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
133+
const pluginLibCss = (
134+
rootDir: string,
135+
banner?: string,
136+
footer?: string,
137+
): RsbuildPlugin => ({
134138
name: PLUGIN_NAME,
135139
setup(api) {
136140
api.modifyBundlerChain((config, { CHAIN_ID }) => {
@@ -149,6 +153,8 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
149153
.loader(require.resolve('./libCssExtractLoader.js'))
150154
.options({
151155
rootDir,
156+
banner,
157+
footer,
152158
});
153159
}
154160
}
@@ -165,13 +171,15 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
165171
export const composeCssConfig = (
166172
rootDir: string | null,
167173
bundle = true,
174+
banner?: string,
175+
footer?: string,
168176
): EnvironmentConfig => {
169177
if (bundle || rootDir === null) {
170178
return {};
171179
}
172180

173181
return {
174-
plugins: [pluginLibCss(rootDir)],
182+
plugins: [pluginLibCss(rootDir, banner, footer)],
175183
tools: {
176184
cssLoader: {
177185
// Otherwise, external variables will be executed by css-extract and cause an error.

packages/core/src/css/libCssExtractLoader.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export interface CssExtractRspackLoaderOptions {
3838
defaultExport?: boolean;
3939

4040
rootDir?: string;
41+
banner?: string;
42+
footer?: string;
4143
}
4244

4345
const LOADER_NAME = 'LIB_CSS_EXTRACT_LOADER';
@@ -87,6 +89,8 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
8789
const callback = this.async();
8890
const filepath = this.resourcePath;
8991
const rootDir = options.rootDir ?? this.rootContext;
92+
const banner = options.banner;
93+
const footer = options.footer;
9094

9195
let { publicPath } = this._compilation!.outputOptions;
9296

@@ -279,7 +283,16 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
279283
m.set(distFilepath, `${m.get(distFilepath)}\n${sourceMappingURL}`);
280284
}
281285
}
282-
for (const [distFilepath, content] of m.entries()) {
286+
for (let [distFilepath, content] of m.entries()) {
287+
// add banner and footer to css files in bundleless mode
288+
if (banner) {
289+
content = `${banner}\n${content}`;
290+
}
291+
292+
if (footer) {
293+
content = `${content}\n${footer}\n`;
294+
}
295+
283296
this.emitFile(distFilepath, content);
284297
}
285298

tests/integration/banner-footer/rslib.config.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ export default defineConfig({
5151
dts: {
5252
bundle: false,
5353
},
54-
// TODO: bundleless css
5554
source: {
5655
entry: {
57-
index: ['./src/**/*.ts'],
56+
index: ['./src/**'],
5857
},
5958
},
6059
...bannerFooterConfig,
@@ -70,10 +69,9 @@ export default defineConfig({
7069
dts: {
7170
bundle: false,
7271
},
73-
// TODO: bundleless css
7472
source: {
7573
entry: {
76-
index: ['./src/**/*.ts'],
74+
index: ['./src/**'],
7775
},
7876
},
7977
...bannerFooterConfig,
@@ -97,4 +95,7 @@ export default defineConfig({
9795
index: './src/index.ts',
9896
},
9997
},
98+
output: {
99+
target: 'web',
100+
},
100101
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import './index.css';
1+
import './index.css';
22
import { foo } from './foo';
33

44
export const text = foo;

tests/integration/style/css-modules/index.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ test('should extract css-modules successfully in bundle', async () => {
2424

2525
test('should extract css-modules successfully in bundle-false', async () => {
2626
const fixturePath = join(__dirname, 'bundle-false');
27-
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
28-
const { contents: jsContents } = await buildAndGetResults({ fixturePath });
27+
const result = await buildAndGetResults({ fixturePath, type: 'all' });
28+
29+
const contents = result.css.contents;
30+
const jsContents = result.js.contents;
2931

3032
const esmFiles = Object.keys(contents.esm);
3133
expect(esmFiles).toMatchInlineSnapshot(`
@@ -56,8 +58,10 @@ test('should extract css-modules successfully in bundle-false', async () => {
5658

5759
test('should extract css-modules successfully in bundle-false with output.cssModules.auto config', async () => {
5860
const fixturePath = join(__dirname, 'bundle-false-auto');
59-
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
60-
const { contents: jsContents } = await buildAndGetResults({ fixturePath });
61+
const result = await buildAndGetResults({ fixturePath, type: 'all' });
62+
63+
const contents = result.css.contents;
64+
const jsContents = result.js.contents;
6165

6266
const esmFiles = Object.keys(contents.esm);
6367
expect(esmFiles).toMatchInlineSnapshot(`

tests/integration/style/stylus/index.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ test('should extract css with pluginStylus in bundle', async () => {
5656

5757
test('should extract css with pluginStylus in bundle-false', async () => {
5858
const fixturePath = join(__dirname, 'bundle-false');
59-
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
60-
const { contents: jsContents } = await buildAndGetResults({ fixturePath });
59+
const result = await buildAndGetResults({ fixturePath, type: 'all' });
60+
61+
const contents = result.css.contents;
62+
const jsContents = result.js.contents;
6163

6264
const esmCssFiles = Object.keys(contents.esm);
6365
const esmCssContents = Object.values(contents.esm);

0 commit comments

Comments
 (0)