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(webpack): support passing function as postcssOptions #19495

Merged
merged 12 commits into from Jun 9, 2023

Conversation

obulat
Copy link

@obulat obulat commented Mar 7, 2023

πŸ”— Linked issue

resolves #7015
#19482
closes #8899

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 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

This is an updated version of #8899 that applies the changes to the now-default PostCSS 8 config.

If postcss config is a function, we execute the custom function, but we also apply the default config of Nuxt. Therefore we can ensure that the config has a minimum default.

Postcssconfig can either be an object (already supported) or a function which gets passed in the loadercontext. But nuxt does not support passing in a function currently.

Why

Passing in a function allows more complex uses cases, e.g. by applying different postcss steps for different ressources. (see also https://github.com/webpack-contrib/postcss-loader#function)

Solution

This approach adds a new "postcssConfig" option to the "postcss" Nuxt config. Either this is the regular postcssConfig, or it is a function. see https://webpack.js.org/loaders/postcss-loader/#postcssoptions
(only works with postcss@8, because postcss-loader >= 4.0 is required, and postcss7 nuxt setup conflicts with this version of postcss-loader)

e.g.
nuxt.config.js.

module.exports = {
  build: {
    postcss: {
      postcssOptions: loaderContext => {
        // e.g. do something else with ".sss" files
        if (/\.sss$/.test(loaderContext.resourcePath)) {
          return {
            parser: 'sugarss',
            plugins: [['postcss-short', { prefix: 'x' }], 'postcss-preset-env']
          };
        }

        return {
          plugins: [['postcss-short', { prefix: 'x' }], 'postcss-preset-env']
        };
      }
    }
  }
};

πŸ“ Checklist

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

@codesandbox
Copy link

codesandbox bot commented Mar 7, 2023

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

@obulat
Copy link
Author

obulat commented Mar 7, 2023

I am not sure that this change correctly applies the preset properties to the postcss-preset-env configuration.
I also don't know how to add tests. Is there an existing test that I could adjust for this change, @danielroe?

I would also need to open a separate PR for updating the documentation. The current section on PostCSS config sessions has not yet been updated to Nuxt 2.16 changes (https://nuxtjs.org/docs/configuration-glossary/configuration-build#postcss)

@obulat obulat marked this pull request as ready for review March 7, 2023 08:00
@obulat obulat marked this pull request as draft March 7, 2023 14:06
@obulat
Copy link
Author

obulat commented Mar 8, 2023

I am not sure that this change correctly applies the preset properties to the postcss-preset-env configuration.

I have extracted the changes to preset to a separate PR (#19518) because it makes the review faster, and I could test that it works.

I'm not sure how to test this PR, though.

@danielroe danielroe added the 2.x label Mar 11, 2023
@codecov-commenter
Copy link

Codecov Report

Patch coverage: 64.51% and project coverage change: -0.03 ⚠️

Comparison is base (83e6869) 66.18% compared to head (60735c7) 66.16%.

Additional details and impacted files
@@            Coverage Diff             @@
##              2.x   #19495      +/-   ##
==========================================
- Coverage   66.18%   66.16%   -0.03%     
==========================================
  Files          93       93              
  Lines        4096     4105       +9     
  Branches     1158     1163       +5     
==========================================
+ Hits         2711     2716       +5     
- Misses       1119     1123       +4     
  Partials      266      266              
Flag Coverage Ξ”
unittests 66.16% <64.51%> (-0.03%) ⬇️

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 77.06% <64.51%> (-1.94%) ⬇️

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.

@danielroe
Copy link
Member

@obulat would you be happy to resolve conflicts in this PR? I can help with adding a test fixture.

@danielroe
Copy link
Member

@obulat Thank you for updating. I've now added a potential way to test the postcss function config, which is now passed through unchanged through the normalisation function.

@danielroe danielroe marked this pull request as ready for review June 9, 2023 12:15
danielroe
danielroe previously approved these changes Jun 9, 2023
@danielroe
Copy link
Member

Merging to include in 2.17. Feel free to come back to me if you think there's anything missing here.

@danielroe danielroe changed the title feat(postcss-loader): support passing in a function for postcss definition feat(webpack): support passing function as postcssOptions Jun 9, 2023
@danielroe danielroe merged commit 3e4284e into nuxt:2.x Jun 9, 2023
13 checks passed
@danielroe danielroe mentioned this pull request Jun 9, 2023
@obulat
Copy link
Author

obulat commented Jun 11, 2023

Merging to include in 2.17. Feel free to come back to me if you think there's anything missing here.

Thank you for getting this PR finished, @danielroe!

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

Successfully merging this pull request may close these issues.

None yet

3 participants