Skip to content

Commit 50440a0

Browse files
committed
test: add redirect-style-false style-inject
1 parent ee0ecf6 commit 50440a0

File tree

15 files changed

+139
-10
lines changed

15 files changed

+139
-10
lines changed

packages/core/src/css/cssConfig.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,7 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
116116
name: pluginName,
117117
setup(api) {
118118
api.modifyBundlerChain((config, { CHAIN_ID }) => {
119-
const cssExtract = CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT;
120-
config.plugins.delete(cssExtract);
121-
config
122-
.plugin(RemoveCssExtractAssetPlugin.name)
123-
.use(RemoveCssExtractAssetPlugin, [
124-
{
125-
include: new RegExp(`^${RSLIB_CSS_ENTRY_FLAG}`),
126-
},
127-
]);
128-
119+
let isUsingCssExtract = false;
129120
for (const ruleId of [
130121
CHAIN_ID.RULE.CSS,
131122
CHAIN_ID.RULE.SASS,
@@ -134,6 +125,7 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
134125
]) {
135126
const rule = config.module.rule(ruleId);
136127
if (rule.uses.has(CHAIN_ID.USE.MINI_CSS_EXTRACT)) {
128+
isUsingCssExtract = true;
137129
rule
138130
.use(CHAIN_ID.USE.MINI_CSS_EXTRACT)
139131
.loader(require.resolve('./libCssExtractLoader.js'))
@@ -142,6 +134,18 @@ const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
142134
});
143135
}
144136
}
137+
138+
if (isUsingCssExtract) {
139+
const cssExtract = CHAIN_ID.PLUGIN.MINI_CSS_EXTRACT;
140+
config.plugins.delete(cssExtract);
141+
config
142+
.plugin(RemoveCssExtractAssetPlugin.name)
143+
.use(RemoveCssExtractAssetPlugin, [
144+
{
145+
include: new RegExp(`^${RSLIB_CSS_ENTRY_FLAG}`),
146+
},
147+
]);
148+
}
145149
});
146150
},
147151
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { buildAndGetResults } from 'test-helper';
2+
import { expectFileContainContent } from 'test-helper/vitest';
3+
import { expect, test } from 'vitest';
4+
5+
test('should extract css successfully when using redirect.style = false', async () => {
6+
const fixturePath = __dirname;
7+
const { contents } = await buildAndGetResults(fixturePath, 'js');
8+
const esmFiles = Object.keys(contents.esm);
9+
expect(esmFiles).toMatchInlineSnapshot(`
10+
[
11+
"<ROOT>/tests/integration/style/redirect-style-false/dist/esm/index.js",
12+
]
13+
`);
14+
expectFileContainContent(contents.esm, 'index.js', 'import "./index.less";');
15+
16+
const cjsFiles = Object.keys(contents.cjs);
17+
expect(cjsFiles).toMatchInlineSnapshot(`
18+
[
19+
"<ROOT>/tests/integration/style/redirect-style-false/dist/cjs/index.cjs",
20+
]
21+
`);
22+
expectFileContainContent(
23+
contents.cjs,
24+
'index.cjs',
25+
'require("./index.less")',
26+
);
27+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "css-bundle-false-redirect-style-false-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
bundle: false,
8+
redirect: {
9+
style: false,
10+
},
11+
}),
12+
generateBundleCjsConfig({
13+
bundle: false,
14+
redirect: {
15+
style: false,
16+
},
17+
}),
18+
],
19+
source: {
20+
entry: {
21+
index: ['./src/index.ts'],
22+
},
23+
},
24+
output: {
25+
copy: [{ from: './src/index.less' }, { from: './src/style.module.less' }],
26+
},
27+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.text-blue {
2+
color: blue;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './index.less';
2+
import styles from './style.module.less';
3+
4+
styles;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.text-blue {
2+
color: blue;
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import 'https://cdnjs.cloudflare.com/ajax/libs/modern-normalize/1.1.0/modern-normalize.css';
2+
@import url('https://cdnjs.cloudflare.com/ajax/libs/modern-normalize/1.0.0/modern-normalize.css');
3+
@import url('lib1.css');
4+
@import 'lib2.css';
5+
6+
.import {
7+
background-image: url('https://cdnjs.cloudflare.com/ajax/libs/modern-normalize/1.1.0/modern-normalize.css');
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './import.css';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.lib1 {
2+
color: red;
3+
}

0 commit comments

Comments
 (0)