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

wp-scripts build doesn't minify CSS that was @import #55885

Closed
cawa-93 opened this issue Nov 6, 2023 · 2 comments · Fixed by #56516
Closed

wp-scripts build doesn't minify CSS that was @import #55885

cawa-93 opened this issue Nov 6, 2023 · 2 comments · Fixed by #56516
Labels
Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Status] In Progress Tracking issues with work in progress [Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended

Comments

@cawa-93
Copy link
Contributor

cawa-93 commented Nov 6, 2023

Description

When building, part of the CSS is imported from another file. The CSS at the entry point is minified and processed with prefixes. However, the imported CSS is inserted "as is" without minification and prefix processing.

Step-by-step reproduction instructions

Create external.css with

.external {
    /* Comment */
    appearance: none;
}

Create entry.css with

@import "external.css";

.entry {
    /* Comment  */
    appearance: none;
}

In package.json add browserslist property:

{
  "browserslist": [
    "ff 10"
  ],
}

In project root add webpack.config.js:

const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const path = require( 'path' );

module.exports = {
    ...defaultConfig,
    mode: 'production',
    entry: 'path/to/entry.css',
};

Run wp-scripts build

After that you should find build/main.css folder in project root.

Expected main.css content

.external{-moz-appearance:none;appearance:none}
.entry{-moz-appearance:none;appearance:none}

/* or */
.entry,.external{-webkit-appearance:none;appearance:none}

Actual main.css content

.external {
    /* Comment */
    appearance: none;
}

.entry{-moz-appearance:none;appearance:none}

Screenshots, screen recording, code snippet

No response

Environment info

@wordpress/scripts: 26.16.0,

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@cawa-93 cawa-93 added the [Type] Bug An existing feature does not function as intended label Nov 6, 2023
@t-hamano t-hamano added the [Tool] WP Scripts /packages/scripts label Nov 6, 2023
@gziolo
Copy link
Member

gziolo commented Nov 24, 2023

I was able to reproduce the issue. It looks like adding importLoaders option to the css-loader configuration in the default webpack config does the trick:

Screenshot 2023-11-24 at 13 57 54 Screenshot 2023-11-24 at 13 57 31

I used:

	{
		loader: require.resolve( 'css-loader' ),
		options: {
			sourceMap: ! isProduction,
			importLoaders: 1,
			modules: {
				auto: true,
			},
		},
	},

This way PostCSS also runs on the imported files.

@gziolo gziolo added Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts labels Nov 24, 2023
benoitchantre added a commit to benoitchantre/gutenberg that referenced this issue Nov 24, 2023
Runs PostCSS on the imported files.

Closes WordPress#55885.
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Nov 24, 2023
@benoitchantre
Copy link
Contributor

Thank you @gziolo !

I was also able to reproduce and can confirm that your suggestion works.

gziolo added a commit that referenced this issue Dec 5, 2023
* Fix CSS imports not minified

Runs PostCSS on the imported files.

Closes #55885.

* Update CHANGELOG.md

---------

Co-authored-by: Greg Ziółkowski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Status] In Progress Tracking issues with work in progress [Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants