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
40 changes: 40 additions & 0 deletions e2e/cases/css/css-raw/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { build, dev } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should allow to import raw CSS files in development mode', async ({
page,
}) => {
const rsbuild = await dev({
cwd: __dirname,
page,
});

expect(await page.evaluate('window.aRaw')).toBe(
readFileSync(path.join(__dirname, 'src/a.css'), 'utf-8'),
);
expect(await page.evaluate('window.bRaw')).toBe(
readFileSync(path.join(__dirname, 'src/b.module.css'), 'utf-8'),
);

await rsbuild.close();
});

test('should allow to import raw CSS files in production mode', async ({
page,
}) => {
const rsbuild = await build({
cwd: __dirname,
page,
});

expect(await page.evaluate('window.aRaw')).toBe(
readFileSync(path.join(__dirname, 'src/a.css'), 'utf-8'),
);
expect(await page.evaluate('window.bRaw')).toBe(
readFileSync(path.join(__dirname, 'src/b.module.css'), 'utf-8'),
);

await rsbuild.close();
});
3 changes: 3 additions & 0 deletions e2e/cases/css/css-raw/src/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header-class {
color: red;
}
3 changes: 3 additions & 0 deletions e2e/cases/css/css-raw/src/b.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.title-class {
font-size: 14px;
}
5 changes: 5 additions & 0 deletions e2e/cases/css/css-raw/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import aRaw from './a.css?raw';
import bRaw from './b.module.css?raw';

window.aRaw = aRaw;
window.bRaw = bRaw;
2 changes: 1 addition & 1 deletion examples/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './App.css';
import rawCss from './App.css?raw';

const App = () => {
return (
Expand Down
32 changes: 32 additions & 0 deletions packages/compat/webpack/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand All @@ -72,6 +75,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down Expand Up @@ -463,6 +471,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand All @@ -486,6 +497,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down Expand Up @@ -872,6 +888,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand All @@ -896,6 +915,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down Expand Up @@ -1208,6 +1232,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand All @@ -1232,6 +1259,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export const CHAIN_ID = {
TS: 'ts',
/** Rule for CSS */
CSS: 'css',
/** Rule for raw CSS */
CSS_RAW: 'css-raw',
/** Rule for less */
LESS: 'less',
/** Rule for sass */
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export const pluginCss = (): RsbuildPlugin => ({
order: 'pre',
handler: async (chain, { target, isProd, CHAIN_ID, environment }) => {
const rule = chain.module.rule(CHAIN_ID.RULE.CSS);
const rawRule = chain.module.rule(CHAIN_ID.RULE.CSS_RAW);
const { config } = environment;

rule
Expand All @@ -273,7 +274,12 @@ export const pluginCss = (): RsbuildPlugin => ({
.type('javascript/auto')
// When using `new URL('./path/to/foo.css', import.meta.url)`,
// the module should be treated as an asset module rather than a JS module.
.dependency({ not: 'url' });
.dependency({ not: 'url' })
// exclude `import './foo.css?raw'`
.resourceQuery({ not: /raw/ });

// Support for importing raw CSS files
rawRule.test(CSS_REGEX).type('asset/source').resourceQuery(/raw/);

const emitCss = config.output.emitCss ?? target === 'web';

Expand Down
8 changes: 8 additions & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -70,6 +73,11 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down
40 changes: 40 additions & 0 deletions packages/core/tests/__snapshots__/css.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exports[`plugin-css > should use custom cssModules rule when using output.cssMod
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -45,6 +48,11 @@ exports[`plugin-css > should use custom cssModules rule when using output.cssMod
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
],
},
"plugins": [
Expand Down Expand Up @@ -73,6 +81,9 @@ exports[`plugin-css injectStyles > should apply ignoreCssLoader when injectStyle
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand All @@ -97,6 +108,11 @@ exports[`plugin-css injectStyles > should apply ignoreCssLoader when injectStyle
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
],
},
"plugins": [
Expand All @@ -118,6 +134,9 @@ exports[`plugin-css injectStyles > should use css-loader + style-loader when inj
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -152,6 +171,11 @@ exports[`plugin-css injectStyles > should use css-loader + style-loader when inj
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
],
},
"plugins": [
Expand All @@ -171,6 +195,9 @@ exports[`should ensure isolation of PostCSS config objects between different bui
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -220,6 +247,11 @@ exports[`should ensure isolation of PostCSS config objects between different bui
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
]
`;

Expand All @@ -232,6 +264,9 @@ exports[`should ensure isolation of PostCSS config objects between different bui
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -281,5 +316,10 @@ exports[`should ensure isolation of PostCSS config objects between different bui
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
]
`;
32 changes: 32 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -70,6 +73,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down Expand Up @@ -459,6 +467,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -493,6 +504,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down Expand Up @@ -918,6 +934,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand All @@ -942,6 +961,11 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down Expand Up @@ -1280,6 +1304,9 @@ exports[`tools.rspack > should match snapshot 1`] = `
"resolve": {
"preferRelative": true,
},
"resourceQuery": {
"not": /raw/,
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
Expand Down Expand Up @@ -1314,6 +1341,11 @@ exports[`tools.rspack > should match snapshot 1`] = `
},
],
},
{
"resourceQuery": /raw/,
"test": /\\\\\\.css\\$/,
"type": "asset/source",
},
{
"dependency": {
"not": "url",
Expand Down
Loading
Loading