Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use CssExtractRspackPlugin to extract CSS #1577

Merged
merged 35 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4a133a8
feat: use mini-css-extract
9aoy Jan 30, 2024
46d6fc9
fix: update css plugin
9aoy Feb 18, 2024
a3e4b08
fix: merge conflict
9aoy Feb 18, 2024
9e8e07d
fix: bump rspack
9aoy Feb 19, 2024
08a7b22
fix: snapshot
9aoy Feb 19, 2024
02568f5
fix: loader name
9aoy Feb 19, 2024
5ca93fd
fix: update test case config
9aoy Feb 22, 2024
83a518d
fix: resolve conflicts
9aoy Feb 22, 2024
885d96e
fix: bump rspack
9aoy Feb 22, 2024
94c99ca
Merge branch 'main' into feat/use-mini-css-extract
9aoy Feb 22, 2024
263ad32
fix: resolve conflicts
9aoy Feb 22, 2024
3515d63
fix: use rspack 0.5.7 canary
9aoy Apr 7, 2024
59f070e
fix: bump rspack
9aoy Apr 7, 2024
b6e955a
fix: bump rspack
9aoy Apr 8, 2024
0dd7269
fix: bump rspack
9aoy Apr 9, 2024
5d23ea7
fix: merge conflict
9aoy Apr 9, 2024
790e832
fix: add css-assets test case
9aoy Apr 9, 2024
fee5153
fix: bump rspack again
9aoy Apr 11, 2024
7673a15
fix: merge conflict
9aoy Apr 11, 2024
9813973
fix: newTreeshaking already default true
9aoy Apr 11, 2024
387525a
fix: bump +11
9aoy Apr 11, 2024
6f07c4e
fix: merge conflict
9aoy Apr 11, 2024
e8562d5
fix: merge confilct & namedExport test case
9aoy Apr 26, 2024
41ff7ff
fix: lint
9aoy Apr 26, 2024
637e63b
fix: snapshot
9aoy Apr 26, 2024
ab01984
fix: update rspack
9aoy Apr 30, 2024
9bf4d5d
fix: update test case
9aoy Apr 30, 2024
54df855
fix: bump rspack
9aoy Apr 30, 2024
38a40d9
fix: use latest rspack
9aoy May 7, 2024
bc67f89
Merge branch 'main' into feat/use-mini-css-extract
9aoy May 10, 2024
e9b1d8f
Update package.json
9aoy May 13, 2024
ac3f082
Merge branch 'main' into feat/use-mini-css-extract
9aoy May 13, 2024
7253ecf
fix: dynamic import mini-css-extract-plugin
9aoy May 13, 2024
9b7251a
Merge branch 'main' into feat/use-mini-css-extract
chenjiahan May 13, 2024
6e88d85
fix: update css assets
chenjiahan May 13, 2024
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
9 changes: 9 additions & 0 deletions e2e/cases/css/css-assets/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should build correctly', async () => {
await expect(build({
cwd: __dirname,
rsbuildConfig: {},
})).resolves.toBeDefined();
});
7 changes: 7 additions & 0 deletions e2e/cases/css/css-assets/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
output: {}
});
7 changes: 7 additions & 0 deletions e2e/cases/css/css-assets/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function load(key: string) {
import(`./test/${key}`).then(res => {
console.log('res', res);
})
}

load('a');
5 changes: 5 additions & 0 deletions e2e/cases/css/css-assets/src/test/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import styles from './index.module.less';

console.log('s', styles);

export const a = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a smaller asset, and all assets should be placed at e2e/assets (to be reused)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions e2e/cases/css/css-assets/src/test/index.module.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header {
background: url('./assets/header-banner.png') no-repeat;
}
10 changes: 10 additions & 0 deletions e2e/cases/css/css-modules-named-export/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ rspackOnlyTest(
const rsbuild = await build({
cwd: __dirname,
runServer: true,
rsbuildConfig: {
tools: {
cssLoader: {
modules: {
// TODO: css-loader need support named export when namedExports: false.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do we need to do to support this

namedExport: true
}
}
}
}
});
const files = await rsbuild.unwrapOutputJSON();

Expand Down
6 changes: 6 additions & 0 deletions e2e/cases/server/hmr/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
});
21 changes: 21 additions & 0 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ export async function dev({

const rsbuild = await createRsbuild(options, plugins);

rsbuild.addPlugins([
{
// fix hmr problem temporary (only appears in rsbuild repo, because css-loader is not in node_modules/ )
// https://github.com/web-infra-dev/rspack/issues/5723
name: 'fix-react-refresh-in-rsbuild',
setup(api) {
api.modifyBundlerChain({
order: 'post',
handler: (chain, { CHAIN_ID }) => {
if (chain.plugins.has(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH)) {
chain.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH).tap((config) => {
config[0].exclude = [/node_modules/, /css-loader/];
return config;
});
}
},
});
},
},
]);

const result = await rsbuild.startDevServer();

return {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"esbuild": "~0.19.0",
"array.prototype.flat": "npm:@nolyfill/array.prototype.flat@1.0.28",
"@rsbuild/core": "link:packages/core",
"@rsbuild/plugin-react": "link:packages/plugin-react"
"@rsbuild/plugin-react": "link:packages/plugin-react"
9aoy marked this conversation as resolved.
Show resolved Hide resolved
},
"allowedDeprecatedVersions": {
"vue": "2.x",
Expand Down
116 changes: 7 additions & 109 deletions packages/compat/webpack/src/plugins/css.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import {
type BundlerChainRule,
type CSSExtractOptions,
CSS_REGEX,
type ModifyChainUtils,
type NormalizedConfig,
type RsbuildContext,
type RsbuildPlugin,
getBrowserslistWithDefault,
getCssLoaderOptions,
getCssModuleLocalIdentName,
getPostcssLoaderOptions,
getSharedPkgCompiledPath,
isUseCssExtract,
mergeChainedOptions,
resolvePackage,
applyCSSRule,
} from '@rsbuild/shared';

export async function applyBaseCSSRule({
rule,
config,
context,
utils: { target, isProd, isServer, CHAIN_ID, isWebWorker },
utils,
importLoaders = 1,
}: {
rule: BundlerChainRule;
Expand All @@ -29,108 +21,14 @@ export async function applyBaseCSSRule({
utils: ModifyChainUtils;
importLoaders?: number;
}) {
const browserslist = await getBrowserslistWithDefault(
context.rootPath,
config,
target,
);

// 1. Check user config
const enableExtractCSS = isUseCssExtract(config, target);
const enableCSSModuleTS = Boolean(config.output.enableCssModuleTSDeclaration);

// 2. Prepare loader options
const localIdentName = getCssModuleLocalIdentName(config, isProd);

const cssLoaderOptions = getCssLoaderOptions({
return applyCSSRule({
rule,
config,
context,
utils,
importLoaders,
isServer,
isWebWorker,
localIdentName,
cssExtractPlugin: require('mini-css-extract-plugin'),
9aoy marked this conversation as resolved.
Show resolved Hide resolved
});

// 3. Create webpack rule
// Order: style-loader/mini-css-extract -> css-loader -> postcss-loader
if (!isServer && !isWebWorker) {
// use mini-css-extract-plugin loader
if (enableExtractCSS) {
const extraCSSOptions: Required<CSSExtractOptions> =
typeof config.tools.cssExtract === 'object'
? config.tools.cssExtract
: {
loaderOptions: {},
pluginOptions: {},
};

rule
.use(CHAIN_ID.USE.MINI_CSS_EXTRACT)
.loader(require('mini-css-extract-plugin').loader)
.options(extraCSSOptions.loaderOptions)
.end();
}
// use style-loader
else {
const styleLoaderOptions = mergeChainedOptions({
defaults: {},
options: config.tools.styleLoader,
});

rule
.use(CHAIN_ID.USE.STYLE)
.loader(getSharedPkgCompiledPath('style-loader'))
.options(styleLoaderOptions)
.end();
}

// use css-modules-typescript-loader
if (enableCSSModuleTS && cssLoaderOptions.modules) {
rule
.use(CHAIN_ID.USE.CSS_MODULES_TS)
.loader(
resolvePackage(
'@rsbuild/shared/css-modules-typescript-loader',
__dirname,
),
)
.options({
modules: cssLoaderOptions.modules,
})
.end();
}
} else {
rule
.use(CHAIN_ID.USE.IGNORE_CSS)
.loader(resolvePackage('@rsbuild/shared/ignore-css-loader', __dirname))
.end();
}

rule
.use(CHAIN_ID.USE.CSS)
.loader(getSharedPkgCompiledPath('css-loader'))
.options(cssLoaderOptions)
.end();

if (!isServer && !isWebWorker) {
const postcssLoaderOptions = await getPostcssLoaderOptions({
browserslist,
config,
root: context.rootPath,
});

rule
.use(CHAIN_ID.USE.POSTCSS)
.loader(getSharedPkgCompiledPath('postcss-loader'))
.options(postcssLoaderOptions)
.end();
}

// CSS imports should always be treated as sideEffects
rule.merge({ sideEffects: true });

// Enable preferRelative by default, which is consistent with the default behavior of css-loader
// see: https://github.com/webpack-contrib/css-loader/blob/579fc13/src/plugins/postcss-import-parser.js#L234
rule.resolve.preferRelative(true);
}

export const pluginCss = (): RsbuildPlugin => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`plugin-css > should not apply postcss-loader when target is node 1`] =
"test": /\\\\\\.css\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand Down
12 changes: 6 additions & 6 deletions packages/compat/webpack/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"test": /\\\\\\.css\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand All @@ -1314,7 +1314,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand Down Expand Up @@ -1354,7 +1354,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"test": /\\\\\\.less\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand Down Expand Up @@ -1634,7 +1634,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"test": /\\\\\\.css\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand All @@ -1660,7 +1660,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"test": /\\\\\\.s\\(\\?:a\\|c\\)ss\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand Down Expand Up @@ -1700,7 +1700,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"test": /\\\\\\.less\\$/,
"use": [
{
"loader": "@rsbuild/shared/ignore-css-loader",
"loader": "<ROOT>/packages/shared/dist/loaders/ignoreCssLoader",
},
{
"loader": "<ROOT>/packages/shared/compiled/css-loader",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export { createContext, createPublicContext } from './createContext';
export { initPlugins, createPluginManager } from './pluginManager';
export { initHooks, type Hooks } from './initHooks';
export { initRsbuildConfig } from './provider/initConfigs';
export { applyBaseCSSRule } from './provider/plugins/css';
export { getPluginAPI } from './initPlugins';
export { applyBaseCSSRule, applyCSSModuleRule } from './provider/plugins/css';
export type { InternalContext } from './types';
export { setHTMLPlugin, getHTMLPlugin } from './htmlUtils';
export { formatStats, getStatsOptions } from './provider/shared';
Expand Down
Loading
Loading