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
7 changes: 6 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,12 @@ async function composeLibRsbuildConfig(
rootPath,
cssModulesAuto,
);
const cssConfig = composeCssConfig(lcp, config.bundle);
const cssConfig = composeCssConfig(
lcp,
config.bundle,
banner?.css,
footer?.css,
);
const assetConfig = composeAssetConfig(bundle, format!);

const entryChunkConfig = composeEntryChunkConfig({
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/css/cssConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ export async function cssExternalHandler(

const PLUGIN_NAME = 'rsbuild:lib-css';

const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
const pluginLibCss = (
rootDir: string,
banner?: string,
footer?: string,
): RsbuildPlugin => ({
name: PLUGIN_NAME,
setup(api) {
api.modifyBundlerChain((config, { CHAIN_ID }) => {
Expand All @@ -149,6 +153,8 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
.loader(require.resolve('./libCssExtractLoader.js'))
.options({
rootDir,
banner,
footer,
});
}
}
Expand All @@ -165,13 +171,15 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
export const composeCssConfig = (
rootDir: string | null,
bundle = true,
banner?: string,
footer?: string,
): EnvironmentConfig => {
if (bundle || rootDir === null) {
return {};
}

return {
plugins: [pluginLibCss(rootDir)],
plugins: [pluginLibCss(rootDir, banner, footer)],
tools: {
cssLoader: {
// Otherwise, external variables will be executed by css-extract and cause an error.
Expand Down
15 changes: 14 additions & 1 deletion packages/core/src/css/libCssExtractLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface CssExtractRspackLoaderOptions {
defaultExport?: boolean;

rootDir?: string;
banner?: string;
footer?: string;
}

const LOADER_NAME = 'LIB_CSS_EXTRACT_LOADER';
Expand Down Expand Up @@ -87,6 +89,8 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
const callback = this.async();
const filepath = this.resourcePath;
const rootDir = options.rootDir ?? this.rootContext;
const banner = options.banner;
const footer = options.footer;

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

Expand Down Expand Up @@ -279,7 +283,16 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
m.set(distFilepath, `${m.get(distFilepath)}\n${sourceMappingURL}`);
}
}
for (const [distFilepath, content] of m.entries()) {
for (let [distFilepath, content] of m.entries()) {
// add banner and footer to css files in bundleless mode
if (banner) {
content = `${banner}\n${content}`;
}

if (footer) {
content = `${content}\n${footer}\n`;
}

this.emitFile(distFilepath, content);
}

Expand Down
9 changes: 5 additions & 4 deletions tests/integration/banner-footer/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export default defineConfig({
dts: {
bundle: false,
},
// TODO: bundleless css
source: {
entry: {
index: ['./src/**/*.ts'],
index: ['./src/**'],
},
},
...bannerFooterConfig,
Expand All @@ -70,10 +69,9 @@ export default defineConfig({
dts: {
bundle: false,
},
// TODO: bundleless css
source: {
entry: {
index: ['./src/**/*.ts'],
index: ['./src/**'],
},
},
...bannerFooterConfig,
Expand All @@ -97,4 +95,7 @@ export default defineConfig({
index: './src/index.ts',
},
},
output: {
target: 'web',
},
});
2 changes: 1 addition & 1 deletion tests/integration/banner-footer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import './index.css';
import './index.css';
import { foo } from './foo';

export const text = foo;
12 changes: 8 additions & 4 deletions tests/integration/style/css-modules/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ test('should extract css-modules successfully in bundle', async () => {

test('should extract css-modules successfully in bundle-false', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
const { contents: jsContents } = await buildAndGetResults({ fixturePath });
const result = await buildAndGetResults({ fixturePath, type: 'all' });

const contents = result.css.contents;
const jsContents = result.js.contents;

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

test('should extract css-modules successfully in bundle-false with output.cssModules.auto config', async () => {
const fixturePath = join(__dirname, 'bundle-false-auto');
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
const { contents: jsContents } = await buildAndGetResults({ fixturePath });
const result = await buildAndGetResults({ fixturePath, type: 'all' });

const contents = result.css.contents;
const jsContents = result.js.contents;

const esmFiles = Object.keys(contents.esm);
expect(esmFiles).toMatchInlineSnapshot(`
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/style/stylus/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ test('should extract css with pluginStylus in bundle', async () => {

test('should extract css with pluginStylus in bundle-false', async () => {
const fixturePath = join(__dirname, 'bundle-false');
const { contents } = await buildAndGetResults({ fixturePath, type: 'css' });
const { contents: jsContents } = await buildAndGetResults({ fixturePath });
const result = await buildAndGetResults({ fixturePath, type: 'all' });

const contents = result.css.contents;
const jsContents = result.js.contents;

const esmCssFiles = Object.keys(contents.esm);
const esmCssContents = Object.values(contents.esm);
Expand Down
Loading