This issue is a: Question / support request
Problem: I'd like to use webpack & postcss for css development. I'm creating a react-component library and publishing these components to a repo for shared use. I'm using NWB to bootstrap the components and for creating the commonjs builds. I thought I'd be able to use webpack & postcss after reading the docs, but I'm not seeing the postcss have any effect on the commonjs build.
Here's my nwb config below...
var config = {
type: 'react-component',
npm: {
esModules: true,
umd: false
},
webpack: {
rules: {
css: {
modules: true,
localIdentName: '[local]__[path][name]__[hash:base64:5]'
},
postcss: {
plugins: [
require('autoprefixer')
]
}
}
}
};
module.exports = config;
If I run the demo for the component, I do see postcss working as intended. But if I publish the commonjs build and reference it another project, the component is missing:
Is this something that is not supported? Any help would be appreciated.
More info:
Demo/dist/*.css looks like this:
.button-container__src-styles__hQ8y-{display:-webkit-box;display:-ms-flexbox;display:flex}.button-12345__src-styles__2O_vv{padding:8px 16px}
But both /es/.css & /lib/.css have:
`.button-container {
display: flex;
}
.button-12345 {
padding: 8px 16px 8px 16px;
}
`
Is webpack/postcss not supported for lib/es builds? Is there a way to enable it?
Webpack isn't involved in creating lib/ and es/, we're just running the Babel CLI.
Styles work as expected in the demo because that's using Webpack (that's also why you'll get a CSS bundle in the umd/ directory, which was accidental!).
The only component I've published CSS alongside uses vanilla CSS in a top-level css/ directory and the component module imports it using ../css/, so it works from es/, lib/ and src/ (and depends on the user having a bundler which supports that).
We can probably make nwb do whatever needs to be done to support publishing compiled stylesheets to npm, but I don't know what the common practices are for that, so any pointers would be welcome.
We already have postcss available as a transitive dependency, so nwb could use it to compile stylesheets (and the nwb Sass, Stylus and Less plugins could expose a new method to use them to compile).
If we have some solid use cases, we can figure out what needs to be done, so we need answers to things like:
Here's a Babel transform which would replace extensions for stylesheet preprocessors with .css:
To summarise some of the above, is this a desirable outcome for React component/library builds?
(Where components import same-named stylesheets)
src/ (es|lib)/
A.js A.js
A.scss ==> A.css
B.js B.js
B.scss B.css
umd/
my-library.js
my-library.css
my-library.min.js
my-library.min.css
What about if it's using CSS modules? https://github.com/michalkvasnicak/babel-plugin-css-modules-transform?
Sorry for leaving this open, I eventually decided upon using https://www.styled-components.com/ in my project which negated the need for postcss. Styled components works with both the demo and the published libraries.
Most helpful comment
To summarise some of the above, is this a desirable outcome for React component/library builds?
What about if it's using CSS modules? https://github.com/michalkvasnicak/babel-plugin-css-modules-transform?