# 二.sourceMap

# 1.安装

# 1.package.json

{
  "name": "map",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "babel-loader": "^8.0.5",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 2.文件

# 1.src/index.js

console.log("index")

class Log {
  constructor() {
    console.lo("err")
  }
}
let log = new Log()
1
2
3
4
5
6
7
8

打印这里写错

# index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>webpack打包多页应用</title>
  </head>

  <body></body>
</html>
1
2
3
4
5
6
7
8
9
10
11

# 3.配置

webpack.config.js

const path = require("path")
let HtmlWebpackPlugin = require("html-webpack-plugin")

module.exports = {
  mode: "production",
  entry: {
    home: "./src/index.js",
  },
  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env"],
          },
        },
      },
    ],
  },
  //1)源码映射 会单独生成一个sourcemap文件 出错了 会标识 当前报错的列和行 大 和全
  // devtool: "source-map",//增加映射文件 可以帮我们调试源代码
  //2)不会产生单独的文件 但是可以显示行和列
  // devtool:'eval-source-map',
  //3)不会产生列 但是是一个单独的映射文件
  // devtool: "cheap-module-source-map",
  //4)不会产生文件 集成在打包后的文件中 不会产生列
  devtool: "cheap-module-eval-source-map",
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      filename: "index.html",
    }),
  ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 4.运行

npm run dev
1

查看 console 控制台有报错信息的行数和列数