optimize-css-assets-webpack-plugin: Source Maps not working with Webpack 4.8
My version of optimize-css-assets-webpack-plugin: 4.0.1
The issue I’m facing is the same as described here: https://github.com/webpack-contrib/mini-css-extract-plugin/issues/141
In summary, I’m defining source-maps on my configuration. I’m also using MiniCssExtractPlugin to extract the css modules to a specific bundle, but when using optimize-css-assets-webpack-plugin
on webpacks 4 optimization.minimizer
, the source-maps are not generated. Here is a snippet:
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({}) // <---- THIS. Commenting/Uncommenting results in: No Optimization with source-maps/ Optimization without source-maps
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': 'production'
}),
new MiniCssExtractPlugin({
filename: 'foo.min.css'
}),
],
devtools: 'source-maps'
Maybe optimize-css-assets-webpack-plugin is truncating source-maps some how?!
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 6
- Comments: 22 (5 by maintainers)
Commits related to this issue
- Source maps support Fixes #53 — committed to frenzzy/optimize-css-assets-webpack-plugin by frenzzy 6 years ago
- Source maps support (#61) Fixes #53 — committed to NMFR/optimize-css-assets-webpack-plugin by frenzzy 6 years ago
- Backport source map fix to v3 The fix was in this PR: https://github.com/NMFR/optimize-css-assets-webpack-plugin/pull/61 Fixes https://github.com/NMFR/optimize-css-assets-webpack-plugin/issues/53 F... — committed to cultureamp/optimize-css-assets-webpack-plugin by jasononeil 6 years ago
Have you read PostCSS sourcemaps options section? For example if you need inline source maps, use:
If you need external source maps, use:
Just figured it out! (this helped with a useful snippet: https://github.com/ben-eb/cssnano/issues/302)
Actually this is not a bug, but can be improved (with docs or better defaults to identify if there are settings in webpack to generate source-maps…)
You just need to set the
map
property tocssProcessorOptions
(something like this):If no other cssProcessor is specified, then
cssnano
is used by default. Then the plugin will pass the cssProcessorOptions as options to it. So searching on how to do it withcssnano
gave me that hint on how to properly configure cssnano.Maybe docs improvements could be made so that it would become clearer on how to use cssProcessorOptions (at least for the default processor, cssnano). Or maybe this plugin could somehow know if the webpack setup is configured to use source-maps or if the css itself has it to set map property to cssnano in that case (sorry if I’m talking silliness, I’m not a webpack nor a cssnano expert at all… It was just a painful debugging session…)
Or we may add a special
sourceMap
option toOptimizeCSSAssetsPlugin
which will do the same under the hood similar to postcss-loader.For example:
PR is welcome 😃
Thanks for all the effort to get the source maps working. Using the tips here and installing the
5.0
release I am now able to get external map files generated for my project and they are working in the browser.However, there is still something a bit off with the paths that are generated and placed into the map file. When I do a non production build without using this optimize css plugin the generated map file has entries in it that look like this:
When I do a production build using this optimize css plugin the map file looks like this:
I am using the config suggested above.
This still works in the browser, but the file system mappings look a bit odd in the developer tools. Is there some way to get the map file to be generated using the same
webpack:///./src
style file references? I did experiment with turning off the source maps on the css-loader and less-loaders which did change things a bit but nothing gets the same output as not using the optimize css plugin.Here is a snippet of the webpack config (
extractCSS
andgenSourceMaps
are true):I would like to fix the issue described by @medington for the sake of privacy and consistency. Note that customizing https://webpack.js.org/configuration/output/#outputdevtoolmodulefilenametemplate doesn’t help.
Is this problem solved? My sourcempas is still empty. like this:
{"version":3,"file":"./euidocs/euidocs.css","sources":[],"mappings":";A","sourceRoot":""}
@medington you can try optimize-cssnano-plugin which is more accurate with source maps.