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(config): move preset to inner postcssOptions #19518

Merged
merged 21 commits into from Mar 16, 2023

Conversation

obulat
Copy link

@obulat obulat commented Mar 8, 2023

πŸ”— Linked issue

#19482

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Resolves #19482. Nuxt 2.16 has updated PostCSS to version 8, and introduced a change into the nuxt.config build.postcss settings. It did not, however, update the way the defaults are set, which made it impossible to set preset.

This PR moves the default preset from build.postcss to build.postcss.postcssOptions. It also adds a test for the default setting, and updates the snapshots.

This PR was extracted from #19495 to make it easier to review.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented Mar 8, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

if (postcssOptions.preset || postcssLoaderOptions.preset) {
postcssOptions.plugins = {
...postcssOptions.plugins || {},
'postcss-preset-env': merge({}, postcssOptions.preset || postcssLoaderOptions.preset)
Copy link
Member

Choose a reason for hiding this comment

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

it might be worth merging all of them to support legacy modules (e.g. tailwind module) that are still interacting with nuxt config based on the old patterns.

Copy link
Author

Choose a reason for hiding this comment

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

Do you mean merging all plugins, @danielroe? The postcss.plugins with postcss.postcssOptions.plugins? I think that that is done on lines 199...202.

Currently, the type for postcss is set to postcss?: string[] | boolean | { postcssOptions: PostcssConfiguration | (() => PostcssConfiguration) }, so it actually disallows the old way of setting the options. Should this type be ignored or updated to include the values that will be deprecated but are still in use?

I tried refactoring a normalize function (https://gist.github.com/obulat/4fd4cf28ec816c20e5818a053b15f3c5) to move all the top-level properties (plugins, order, preset) inside the inner postcssOptions, prioritizing the values that are already within postcssOptions. Would this be useful?

Copy link
Member

Choose a reason for hiding this comment

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

The normalize function sounds great! My primary concern is not to support users explicitly passing in an old format but rather to handle the case where modules set nuxt.options.build.postcss.plugins (or similar). This happens after we initially normalise the config.

Copy link
Author

Choose a reason for hiding this comment

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

I didn't know about modules setting plugins :) Do they set it after getting the config using postcss.config()? Then this solution will not work. But if they update the build config, and then call postcss.config with it, then this normalize function should work:

The config gets the outer postcss object, and then converts it by moving any top-level plugins, preset and order values to the inner postcssOptions object. Thus, the config object that we use to load plugins is {postcssOptions: {plugins: {}|[], preset: {}, order: {}}, with no other top-level properties but postcssOptions.

I spent a lot of time trying to debug the plugins that postcss-preset-env loads. I incorrectly understood the documentation to say, "the higher you set the stage, the more plugins will be added", and in fact, it is the opposite. While reading the documentation, I also saw that the default stage is already set in postcss-preset-env at 2, so I removed the default stage option of {preset: { stage: 2 }} from the settings here (replaced with {preset: {} }. Please, let me know if it's better to add it back.

@danielroe danielroe added the 2.x label Mar 9, 2023
@obulat obulat marked this pull request as draft March 10, 2023 12:02
@obulat obulat marked this pull request as ready for review March 12, 2023 05:38
@codecov-commenter
Copy link

codecov-commenter commented Mar 12, 2023

Codecov Report

Patch coverage: 47.61% and project coverage change: -0.23 ⚠️

Comparison is base (59b8085) 66.18% compared to head (872442d) 65.96%.

Additional details and impacted files
@@            Coverage Diff             @@
##              2.x   #19518      +/-   ##
==========================================
- Coverage   66.18%   65.96%   -0.23%     
==========================================
  Files          93       93              
  Lines        4096     4113      +17     
  Branches     1158     1165       +7     
==========================================
+ Hits         2711     2713       +2     
- Misses       1119     1133      +14     
- Partials      266      267       +1     
Flag Coverage Ξ”
unittests 65.96% <47.61%> (-0.23%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Ξ”
packages/config/src/config/build.js 100.00% <ΓΈ> (ΓΈ)
packages/webpack/src/utils/postcss.js 69.23% <47.61%> (-9.77%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

β˜” View full report in Codecov by Sentry.
πŸ“’ Do you have feedback about the report comment? Let us know in this issue.

@obulat obulat requested a review from danielroe March 15, 2023 06:36
@danielroe danielroe requested a review from clarkdo March 15, 2023 13:23
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

This looks great! ❀️

packages/webpack/src/utils/postcss.js Outdated Show resolved Hide resolved
packages/webpack/src/utils/postcss.js Outdated Show resolved Hide resolved
packages/webpack/src/utils/postcss.js Outdated Show resolved Hide resolved
@danielroe danielroe changed the title fix(config): move preset from postcss to inner postcssOptions fix(config): move preset to inner postcssOptions Mar 16, 2023
@danielroe danielroe merged commit e86850a into nuxt:2.x Mar 16, 2023
13 checks passed
@danielroe
Copy link
Member

Thank you for all your work on this ❀️

@obulat
Copy link
Author

obulat commented Mar 16, 2023

Thank you for all your work on this ❀️

Thank you for helping me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants