question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sourcemap doesn't work with devtool option contain "eval"

See original GitHub issue
  • Operating System:
  • Node Version: 10.15.2
  • NPM Version: 6.4.1
  • webpack Version: 4.41.6
  • mini-css-extract-plugin Version: 0.9.0

Expected Behavior

Use the devtool options with “eval” such as “cheap-module-eval-source-map”, the extract css file can map to the really css or sass file. image

Actual Behavior

My entry file ‘main.js’ which import ‘testSass.scss’, with the devtool options “cheap-module-eval-source-map” to get the fast rebuild . But it’can not map the body style to the real sass file. image If I change devtool to “cheap-module-source-map” without ‘eval’, It works.

Code

// webpack.config.js
module.exports = {
  mode: 'development', // 'production'
  devtool: 'cheap-module-source-map',
  entry: {
    'learn': './src/main.js',
  },
  module: {
    rules: [ // 添加对模块处理特定规则
      {
        test: /\.s[ac]ss$/i,
        use: [{
          loader: MiniCssExtractPlugin.loader,
          options: {hmr: true}
          },
          {loader: 'css-loader', options: {sourceMap: true}},
          {loader: 'sass-loader', options: {sourceMap: true}},]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css'
    }),
    new HtmlWebpackPlugin(),
  ],
  devServer: {
    port: 9000,
    hot: true
  }
}

How Do We Reproduce?

You can use the config above . If cannot reproduce , I will give a repo later.

Addition

I found an hack with add the sourcemapPlugin with devtools in https://github.com/webpack-contrib/mini-css-extract-plugin/issues/29#issuecomment-382424129

new webpack.SourceMapDevToolPlugin({ 
      filename: "[file].map",
      exclude: ["/vendor/"]
    }),
```js

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
alexander-akaitcommented, Aug 19, 2020

Yes, it is limitation, eval doesn’t work with CSS, you need to use source-map, we need documented it

3reactions
andy0130twcommented, Jan 25, 2022

Follow-up: After reading webpack’s source code a little bit more, I manage to adjust the configuration accordingly so it produces the inline source map instead, without touching the source code of mini-css-extract-plugin:

{
  /* ... */
  plugins: [
    new MiniCssExtractPlugin(),
    new webpack.SourceMapDevToolPlugin({
      test: /\.css$/i,
      filename: null,
      append: '/*# sourceMappingURL=[url] */',
    })
  ]
}

Normally we would turn off the devtool option, but if we are using an “eval-*-source-map” as its value, it is applying a different plugin EvalSourceMapDevToolPlugin. As long as the plugin we add is NOT touching JS files, we won’t interfere that option: https://github.com/webpack/webpack/blob/v5.67.0/lib/WebpackOptionsApply.js#L225-L246

This also solves issue #29 and seems more elegant than both the original and my previous solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack - devtool source-map VS eval-source-map
it looks like this person has better luck using source-map for CSS file mapping whereas eval-source-map is more helpful for JS files. I...
Read more >
Devtool - webpack
devtool performance production quality (none) build: fastest rebuild: fastest yes bundle eval build: fast rebuild: fastest no generated eval‑cheap‑source‑map build: ok rebuild: fast no transformed
Read more >
Source Maps | webpack surviveJS - GitHub Pages
Source maps solve this problem by providing a mapping between the original and the ... eval-source-map is the highest quality option of the...
Read more >
Source Maps - SurviveJS
Often eval is the starting point and webpack issue #2145 recommends ... To override this, you have to prefix your source map option...
Read more >
EN - Webpackjs - Devtool.pdf - Repository [Root Me
Never use both the devtool option and plugin together. ... eval fastest fastest no generated code eval-cheap-source-map ... You have to make sure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found