Stylus + Webpack: Source Map Integrity Through Autoprefixer for Deeply Nested Rules
0 reputation · 29 Mar 2023, 18:40 UTC
0 reputation · 29 Mar 2023, 18:40 UTC
Goal: compile a Stylus file that contains deeply nested selectors, generate a source map, pipe the CSS through autoprefixer, and verify that the final source map still points back to the original Stylus source.
Constraints: Using Stylus 0.63 with --source-map, the stylus-loader plugin in a Webpack 5 pipeline, and autoprefixer 10.x. The unresolved behavior is whether nested rule depth limits in Stylus (e.g., >10 levels) cause source map entries to be omitted after autoprefixer processes the CSS.
Questions:
stylus-loader or autoprefixer to increase depth or force full mapping?stylus-loader’s sourceMap option and autoprefixer’s own source map handling affect the final mapping accuracy?34775 reputation · 30 Mar 2023, 06:21 UTC
Autoprefixer (via PostCSS) preserves source map entries for deeply nested Stylus constructs, provided the mapping chain is unbroken. There is no hard-coded nesting depth limit in Stylus or Autoprefixer that triggers the omission of source map entries; however, mapping loss typically occurs due to configuration gaps between the stylus-loader and postcss-loader.
In a Webpack 5 pipeline, source maps are passed as a chain. If stylus-loader generates a map, it is passed to postcss-loader. Autoprefixer does not "rewrite" the original source; it consumes the existing map and appends its own transformations to it. If the final map points to the intermediate CSS instead of the .styl file, the chain was broken at the PostCSS stage.
To ensure integrity for deeply nested rules, verify the following settings in your Webpack configuration:
sourceMap: true. This ensures the initial mapping from Stylus to CSS is created and passed forward.postcssOptions include map: { inline: false } (or true depending on your output preference). Without the map option enabled in PostCSS, Autoprefixer may discard the incoming Stylus map and generate a new one that only references the compiled CSS.source-map to avoid the inaccuracies associated with eval-based mappings.// webpack.config.js snippet
module.exports = {
devtool: 'source-map',
module: {
rules: [{
test: /\.styl$/,
use: [
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader', options: { postcssOptions: { map: true } } },
{ loader: 'stylus-loader', options: { sourceMap: true } }
]
}]
}
};
To confirm that deep nesting is not causing omissions, perform these checks:
.css.map file. Search the "sources" array. It must contain the path to the .styl file, not just the intermediate CSS.Diagnostic Detail Needed: Are you using mini-css-extract-plugin? If so, the plugin's own source map settings can override loader settings and may require additional configuration to preserve the Stylus origin.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.