Skip to content

Commit

Permalink
fix: allow to enable Rspack experiments.css (#3068)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 30, 2024
1 parent 253ca44 commit cb548a1
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 1 deletion.
45 changes: 45 additions & 0 deletions e2e/cases/css/enable-experiments-css/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { build, proxyConsole, rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';

rspackOnlyTest('should allow to enable Rspack experiments.css', async () => {
const { logs, restore } = proxyConsole();

const rsbuild = await build({
cwd: __dirname,
});
const files = await rsbuild.unwrapOutputJSON();
const content =
files[Object.keys(files).find((file) => file.endsWith('index.css'))!];

expect(content).toEqual('body{color:red}');
// should have no warnings
expect(logs.some((log) => log.includes('Compile Warning'))).toBeFalsy();

restore();
});

rspackOnlyTest(
'should allow to enable Rspack experiments.css with style-loader',
async () => {
const { logs, restore } = proxyConsole();

const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
output: {
injectStyles: true,
},
},
});

const files = await rsbuild.unwrapOutputJSON();
const content =
files[Object.keys(files).find((file) => file.endsWith('index.js'))!];
expect(content).toContain('color: red;');

// should have no warnings
expect(logs.some((log) => log.includes('Compile Warning'))).toBeFalsy();

restore();
},
);
14 changes: 14 additions & 0 deletions e2e/cases/css/enable-experiments-css/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
tools: {
rspack: {
experiments: {
css: true,
},
},
},
output: {
filenameHash: false,
},
});
3 changes: 3 additions & 0 deletions e2e/cases/css/enable-experiments-css/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: red;
}
1 change: 1 addition & 0 deletions e2e/cases/css/enable-experiments-css/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './index.css';
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/mini-css-extract-plugin/dist/loader.js",
Expand Down Expand Up @@ -412,6 +413,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/mini-css-extract-plugin/dist/loader.js",
Expand Down Expand Up @@ -772,6 +774,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/packages/core/dist/ignoreCssLoader.cjs",
Expand Down Expand Up @@ -1074,6 +1077,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/packages/core/dist/ignoreCssLoader.cjs",
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ export const pluginCss = (): RsbuildPlugin => ({
handler: async (chain, utils) => {
const rule = chain.module.rule(utils.CHAIN_ID.RULE.CSS);
const { config } = utils.environment;
rule.test(CSS_REGEX);

rule
.test(CSS_REGEX)
// specify type to allow enabling Rspack `experiments.css`
.type('javascript/auto');

await applyCSSRule({
rule,
utils,
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/tests/__snapshots__/css.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`plugin-css > should use custom cssModules rule when using output.cssMod
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down Expand Up @@ -59,6 +60,7 @@ exports[`plugin-css injectStyles > should apply ignoreCssLoader when injectStyle
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/packages/core/src/ignoreCssLoader.cjs",
Expand Down Expand Up @@ -98,6 +100,7 @@ exports[`plugin-css injectStyles > should use css-loader + style-loader when inj
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/packages/core/compiled/style-loader",
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down Expand Up @@ -432,6 +433,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down Expand Up @@ -862,6 +864,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/packages/core/dist/ignoreCssLoader.cjs",
Expand Down Expand Up @@ -1193,6 +1196,7 @@ exports[`tools.rspack > should match snapshot 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,7 @@ exports[`environment config > tools.rspack / bundlerChain can be used in environ
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down Expand Up @@ -1729,6 +1730,7 @@ exports[`environment config > tools.rspack / bundlerChain can be used in environ
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/packages/core/src/ignoreCssLoader.cjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ exports[`plugins/react > should work with swc-loader 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-rem/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports[`plugin-rem > should not run htmlPlugin with enableRuntime is false 1`]
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down Expand Up @@ -73,6 +74,7 @@ exports[`plugin-rem > should run rem plugin with custom config 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down Expand Up @@ -144,6 +146,7 @@ exports[`plugin-rem > should run rem plugin with default config 1`] = `
},
"sideEffects": true,
"test": /\\\\\\.css\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader.js",
Expand Down

0 comments on commit cb548a1

Please sign in to comment.