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

fix(webpack): fix loader config for global styles #14323

Merged
merged 1 commit into from
Jan 12, 2023
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
11 changes: 11 additions & 0 deletions e2e/react/src/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe('React Applications', () => {
`
);

// Make sure global stylesheets are properly processed.
const stylesPath = `apps/${appName}/src/styles.css`;
updateFile(
stylesPath,
`
.foobar {
background-image: url('/bg.png');
}
`
);

const libTestResults = await runCLIAsync(`test ${libName}`);
expect(libTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
Expand Down
111 changes: 89 additions & 22 deletions packages/webpack/src/utils/with-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ export function withWeb() {
const cssModuleRules: RuleSetRule[] = [
{
test: /\.module\.css$/,
exclude: globalStylePaths,
use: getCommonLoadersForCssModules(options, includePaths),
},
{
test: /\.module\.(scss|sass)$/,
exclude: globalStylePaths,
use: [
...getCommonLoadersForCssModules(options, includePaths),
{
Expand All @@ -129,6 +131,7 @@ export function withWeb() {
},
{
test: /\.module\.less$/,
exclude: globalStylePaths,
use: [
...getCommonLoadersForCssModules(options, includePaths),
{
Expand All @@ -143,6 +146,7 @@ export function withWeb() {
},
{
test: /\.module\.styl$/,
exclude: globalStylePaths,
use: [
...getCommonLoadersForCssModules(options, includePaths),
{
Expand All @@ -160,10 +164,12 @@ export function withWeb() {
const globalCssRules: RuleSetRule[] = [
{
test: /\.css$/,
exclude: globalStylePaths,
use: getCommonLoadersForGlobalCss(options, includePaths),
},
{
test: /\.scss$|\.sass$/,
exclude: globalStylePaths,
use: [
...getCommonLoadersForGlobalCss(options, includePaths),
{
Expand All @@ -183,6 +189,7 @@ export function withWeb() {
},
{
test: /\.less$/,
exclude: globalStylePaths,
use: [
...getCommonLoadersForGlobalCss(options, includePaths),
{
Expand All @@ -199,6 +206,7 @@ export function withWeb() {
},
{
test: /\.styl$/,
exclude: globalStylePaths,
use: [
...getCommonLoadersForGlobalCss(options, includePaths),
{
Expand All @@ -214,33 +222,72 @@ export function withWeb() {
},
];

const rules: RuleSetRule[] = [
const globalStyleRules: RuleSetRule[] = [
Copy link
Member Author

Choose a reason for hiding this comment

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

Expand global stylesheets to process less, stylus as well. It was missed previously.

{
test: /\.css$|\.scss$|\.sass$|\.less$|\.styl$/,
oneOf: [
...cssModuleRules,
...globalCssRules,
// load global css as css files
test: /\.css$/,
include: globalStylePaths,
use: getCommonLoadersForGlobalStyle(options, includePaths),
},
{
test: /\.scss$|\.sass$/,
include: globalStylePaths,
use: [
...getCommonLoadersForGlobalStyle(options, includePaths),
{
test: /\.css$|\.scss$|\.sass$|\.less$|\.styl$/,
include: globalStylePaths,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: { esModule: true },
loader: require.resolve('sass-loader'),
options: {
implementation: require('sass'),
sourceMap: options.sourceMap,
sassOptions: {
fiber: false,
// bootstrap-sass requires a minimum precision of 8
precision: 8,
includePaths,
},
{ loader: require.resolve('css-loader') },
{
loader: require.resolve('postcss-loader'),
options: {
implementation: require('postcss'),
postcssOptions: postcssOptionsCreator(options, includePaths),
},
},
},
],
},
{
test: /\.less$/,
include: globalStylePaths,
use: [
...getCommonLoadersForGlobalStyle(options, includePaths),
{
loader: require.resolve('less-loader'),
options: {
sourceMap: options.sourceMap,
lessOptions: {
javascriptEnabled: true,
...lessPathOptions,
},
],
},
},
],
},
{
test: /\.styl$/,
include: globalStylePaths,
use: [
...getCommonLoadersForGlobalStyle(options, includePaths),
{
loader: require.resolve('stylus-loader'),
options: {
sourceMap: options.sourceMap,
stylusOptions: {
include: includePaths,
},
},
},
],
},
];

const rules: RuleSetRule[] = [
{
test: /\.css$|\.scss$|\.sass$|\.less$|\.styl$/,
oneOf: [...cssModuleRules, ...globalCssRules, ...globalStyleRules],
},
];

plugins.push(
Expand Down Expand Up @@ -305,7 +352,6 @@ export function withWeb() {
...config.module,
rules: [
...(config.module.rules ?? []),
...rules,
{
test: /\.(bmp|png|jpe?g|gif|webp|avif)$/,
type: 'asset',
Expand All @@ -322,6 +368,7 @@ export function withWeb() {
name: `[name]${hashFormat.file}.[ext]`,
},
},
...rules,
],
};
processed.add(config);
Expand Down Expand Up @@ -421,7 +468,27 @@ function getCommonLoadersForGlobalCss(
? MiniCssExtractPlugin.loader
: require.resolve('style-loader'),
},
{ loader: require.resolve('css-loader') },
{ loader: require.resolve('css-loader'), options: { url: false } },
{
loader: require.resolve('postcss-loader'),
options: {
implementation: require('postcss'),
postcssOptions: postcssOptionsCreator(options, includePaths),
},
},
];
}

function getCommonLoadersForGlobalStyle(
Copy link
Member Author

Choose a reason for hiding this comment

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

Keep this separate from getCommonLoadersForGlobalCss, because there are minor differences and we want to avoid bad abstractions like we did previously.

options: NormalizedWebpackExecutorOptions,
includePaths: string[]
) {
return [
{
loader: MiniCssExtractPlugin.loader,
options: { esModule: true },
},
{ loader: require.resolve('css-loader'), options: { url: false } },
{
loader: require.resolve('postcss-loader'),
options: {
Expand Down