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
[BUG] Generated CSS modules class names are deleted from the HTML on production builds with optimize #3243
Comments
Also, did you try the latest version? 3.3.6 or 3.3.7? Some fixes went in right before you posted this. |
I believe this is a duplicate of #2998, and it’s a current limitation of esbuild, which does not support CSS Modules. Open to suggestions on temporary workarounds! |
I'm pretty sure there must be some kind of regression in there since he says it works with the webpack plugin in 3.3.2 😅. But yes, the fact that it doesn't work with the built-in optimizer is to be expected. |
This also happens when not using the optimize config and using |
We are running into the same problem with the optimization strategy (expected) and Looking at the build output, it seems that the JSON keys are being renamed. While the JSX output still uses the original properties. The CSS does contain the correct classNames, but it also includes the Source: import styles from './Auth.module.scss';
const Auth = () => {
return (
<Layout>
<div className={styles.Auth} />
</Layout>
);
}; Build output: @use "../../styles/variables";
@use "../../styles/theme";
._22ImGiStC_zj9-aVAVSHUL{display:flex;justify-content:center;align-items:center;height:100vh} se={_Auth_13g8k_1:"_22ImGiStC_zj9-aVAVSHUL"} u.b.createElement(B,null,u.b.createElement("div",{className:se.Auth})) |
Came here with same issue
styles into the bundled css file, but in the js file, it became like this
here's how to reproduce the problem https://github.com/ChapmanCheng/snowpack_webpack_build_error |
if seems like it's not a problem with CSS loaders, but a problem when parsing JS files, the parser cannot correctly resolve the component, e.g.
though the however, the final js file is still showing hope this helps a bit more |
It looks like it is creating a valid build when removing the The following config removes the loader from the Webpack config:
|
I was just about to post the same thing. I even removed all rules from the webpack config which creates a working bundle. The main drawback is that there are no CSS files generated which is not ideal performance wise. ['@snowpack/plugin-webpack',
{
extendConfig: (config) => {
config.module.rules = [];
return config;
},
}] |
I've debugged the problem even further and I found the cause of the issue. The problem is that after the Snowpack build, CSS modules are being generated with unique When running Webpack optimize, the The following suggestions might fix this problem.
However, I don't know the impact of these changes. I did found a temporary solution. The following Webpack loader strips the postCssModules uniqueness from the selectors in all scripts/webpack/css-modules-fix.js module.exports = function cssModulesFix(source) {
return source.replace(/_(\w+)_[\w\d]{5}_\d+/g, function(fullMatch, originalSelector) {
return originalSelector;
});
}; snowpack.config.js ['@snowpack/plugin-webpack', {
extendConfig: (config) => {
const cssModulesRule = config.module.rules.find((rule) =>
rule && rule.use && rule.use.find((use) => use && use.loader && use.loader.includes('css-loader') && use.options && use.options.modules));
if (cssModulesRule) {
cssModulesRule.use.unshift({
loader: require.resolve('./scripts/webpack/css-modules-fix.js'),
});
}
return config;
},
}], @monken this fix does create a single CSS file. You must re-enable the Snowpack loaders as well. |
Bug Report Quick Checklist
Describe the bug
When using either
@snowpack/plugin-webpack
or theoptimize
config, all class names from CSS modules get removed from the markup.Example markup from the project where this happens:
To Reproduce
npm init snowpack-app --template @snowpack/app-template-react
snowpack build
Expected behavior
For the class names to appear. https://github.com/finiam/phoenix_starter I have a starter repo (elixir + react) that is stuck on 3.3.2 and it works there. But from 3.3.3 onwards it doesn't.
On that project, with the 3.3.2 version, class names are generated correctly:
The text was updated successfully, but these errors were encountered: