Skip to content

Commit 74d4892

Browse files
fix(bundleless, css): in target: 'node', empty js files(css transform) should be deleted (#809)
Co-authored-by: Timeless0911 <[email protected]>
1 parent b010220 commit 74d4892

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

packages/core/src/css/LibCssExtractPlugin.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { type Rspack, rspack } from '@rsbuild/core';
2-
import { RSLIB_CSS_ENTRY_FLAG } from './cssConfig';
32
import {
43
ABSOLUTE_PUBLIC_PATH,
54
AUTO_PUBLIC_PATH,
@@ -20,21 +19,6 @@ class LibCssExtractPlugin implements Rspack.RspackPluginInstance {
2019
}
2120

2221
apply(compiler: Rspack.Compiler): void {
23-
// 1. mark and remove the normal css asset
24-
// 2. preserve CSS Modules asset
25-
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
26-
compilation.hooks.chunkAsset.tap(pluginName, (_chunk, filename) => {
27-
const asset = compilation.getAsset(filename);
28-
if (!asset) {
29-
return;
30-
}
31-
const needRemove = Boolean(asset.name.match(RSLIB_CSS_ENTRY_FLAG));
32-
if (needRemove) {
33-
compilation.deleteAsset(filename);
34-
}
35-
});
36-
});
37-
3822
/**
3923
* The following code is modified based on
4024
* https://github.com/webpack-contrib/mini-css-extract-plugin/blob/3effaa0319bad5cc1bf0ae760553bf7abcbc35a4/src/index.js#L1597

packages/core/src/css/cssConfig.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,28 @@ export async function cssExternalHandler(
130130

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

133+
// 1. replace CssExtractPlugin.loader with libCssExtractLoader
134+
// 2. replace CssExtractPlugin with LibCssExtractPlugin
133135
const pluginLibCss = (
134136
rootDir: string,
135137
banner?: string,
136138
footer?: string,
137139
): RsbuildPlugin => ({
138140
name: PLUGIN_NAME,
139141
setup(api) {
142+
// 1. mark and remove the "normal css asset" (contain RSLIB_CSS_ENTRY_FLAG)
143+
// 2. preserve CSS Modules asset
144+
api.processAssets(
145+
{ stage: 'additional' }, // deleteAsset as soon as possible for small perf
146+
({ assets, compilation }) => {
147+
for (const key of Object.keys(assets)) {
148+
if (key.match(RSLIB_CSS_ENTRY_FLAG)) {
149+
compilation.deleteAsset(key);
150+
}
151+
}
152+
},
153+
);
154+
140155
api.modifyBundlerChain((config, { CHAIN_ID }) => {
141156
let isUsingCssExtract = false;
142157
for (const ruleId of [

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,15 @@ test('should extract css successfully in bundle-false', async () => {
133133
}
134134
`);
135135
});
136+
137+
test('should not emit css and css related js in target: "node"', async () => {
138+
const fixturePath = join(__dirname, 'node-bundle-false');
139+
const { js, css, dts } = await buildAndGetResults({
140+
fixturePath,
141+
type: 'all',
142+
});
143+
144+
expect(js.files).toMatchInlineSnapshot('{}');
145+
expect(css.files).toMatchInlineSnapshot('{}');
146+
expect(dts.files).toMatchInlineSnapshot('{}');
147+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "node-css-bundle-false-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({ bundle: false }),
7+
generateBundleCjsConfig({ bundle: false }),
8+
],
9+
source: {
10+
entry: {
11+
index: ['../__fixtures__/basic/src/**/*.css'],
12+
},
13+
},
14+
});

0 commit comments

Comments
 (0)