Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack not generating source maps

webpack.config.js

const path = require('path');
const webpack = require('webpack');

module.exports = {
    entry: './src/index.js',
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ],
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    devServer: {
        hot: true,
        contentBase: path.join(__dirname, 'dist'),
    },
    devtool: "source-map"
}

package.json

  "scripts": {
    "build": "webpack --progress --colors",
    "dev": "webpack-dev-server -d --watch --progress --colors"
  },

index.js

let h = "hello world"
console.log(h)

I run both npm build and npm run dev but neither seem to generate source maps. The way I'm checking if they show up is if I can see the original source in Chrome dev tools or Safari dev tools.

Thanks for any help!

Update 0

  • Running npm run build (npm build doesn't do anything) does indeed generate bundle.js.map and I can use the source maps successfully in Google Chrome (yay!)

  • Running npm run dev seems to compile fine and serves my webpage successfully but the dist directory doesn't contain bundle.js or bundle.js.map - but when I go to localhost:8080 it still works (without the source maps).

like image 259
Carpetfizz Avatar asked Jun 19 '17 18:06

Carpetfizz


People also ask

How do I enable source map in webpack?

Using Webpack, specifying devtool: "source-map" in your Webpack config will enable source maps, and Webpack will output a sourceMappingURL directive in your final, minified file. You can customize the source map filename itself by specifying sourceMapFilename .

How do I fix a source map error?

The only workaround is to manually change the map URL to a public one (http://localhost:1234/file.map.js) and start a local webserver at this port.

What is inline sourceMap?

Inline source map This is a special comment placed in your normal JavaScript file to tells your browser how to link the compiled code to the original version.

What is webpack Devtool?

The webpack devtool configuration option controls source map generation. It has many options, and there are a few things to consider when choosing which one to use in different scenarios. This quick guide introduces the feature/performance differences between each one, and what to consider when choosing.


2 Answers

I recently had this problem with webpack 3.6.0. No source map files were being generated even though I had devtool: 'source-map' in webpack.config.js.

In my case, the problem was that I was passing -d to webpack on the command line, which is a shortcut for all of this (notice the second option and its argument):

--debug --devtool cheap-module-eval-source-map --output-pathinfo

Instead of passing -d, I now pass --debug --output-pathinfo and source map files are generated as expected.

like image 83
Rob Johansen Avatar answered Oct 27 '22 01:10

Rob Johansen


I could not generate source maps, because my output.filename did not end with '.js'.

so i need to use 'test' to set SourceMapDevToolPlugin

    new webpack.SourceMapDevToolPlugin({
      test: new RegExp('\.[js|css|mjs].*'),
    }),


like image 38
scil Avatar answered Oct 27 '22 00:10

scil