javascript - 在reactjs中sourceMapSupport有什么用?

标签 javascript node.js reactjs react-router react-redux

我正在从这里学习服务端渲染 https://www.youtube.com/watch?v=duhudXkHRf4&t=927s

我想知道他为什么在server.js中使用这个?这段代码有什么用?

if (process.env.NODE_ENV === "development") {
     console.log("===============")
     sourceMapSupport.install();
 }

他已经在 webpack.config.js 中生成了 server.js 的源映射,如下所示。

const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");


const browserConfig = {
    entry: "./src/app.js",
    output: {
        path: __dirname,
        filename: "./public/bundle.js"
    },
    devtool: "cheap-module-source-map",
    module: {
        rules: [
            {
                test: [/\.svg$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
                loader: "file-loader",
                options: {
                    name: "public/media/[name].[ext]",
                    publicPath: url => url.replace(/public/, "")
                }
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    use: [
                        {
                            loader: "css-loader",
                            options: { importLoaders: 1 }
                        }
                    ]
                })
            },
            {
                test: /js$/,
                exclude: /(node_modules)/,
                loader: "babel-loader",
                query: { presets: ["es2015", "react"]  }
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename: "public/css/[name].css"
        })
    ]
};

const serverConfig = {
    entry: "./server/index.js",
    target: "node",
    output: {
        path: __dirname,
        filename: "server.js",
        libraryTarget: "commonjs2"
    },
    devtool: "cheap-module-source-map",//here is generated source map.
    module: {
        rules: [
            {
                test: [/\.svg$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
                loader: "file-loader",
                options: {
                    name: "public/media/[name].[ext]",
                    publicPath: url => url.replace(/public/, ""),
                    emit: false
                }
            },
            {
                test: /\.css$/,
                use: [
                    {
                        loader: "css-loader/locals"
                    }
                ]
            },
            {
                test: /js$/,
                exclude: /(node_modules)/,
                loader: "babel-loader",
                query: { presets: ["es2015", "react"]  }
            }
        ]
    }
};

module.exports = [browserConfig, serverConfig];

我将如何使用这个sourceMapSupport.install();请帮助我理解

最佳答案

在 Chrome 中运行项目时,F12 转到控制台。在源选项卡下,您将在左侧看到文件夹列表。在 Webpack 下,您将看到文件列表。您可以通过单击行号来插入中断。

这在故障排除时非常有用,它与 Visual Studio 的做法类似。

关于javascript - 在reactjs中sourceMapSupport有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49064473/

相关文章:

java - 为什么 JavaScript 被称为 JavaScript,因为它与 Java 无关?

javascript - D3js 重置拖动行为的起源

javascript - Node 中的请求模块的 POST 请求不起作用

reactjs - 与 setInterval/clearInterval 一起使用时 useRef 的类型 [react-typescript]

javascript - 必须使用 import 加载 ES Module .eslintrc.js

javascript - 如何缩小页面中间的导航栏并使其在滚动时粘在页面顶部?

javascript - 如何通过将模式引用推送到用户的数组字段来更新用户对象

node.js - 将文本作为下载文件发送

node.js - Dockerizing 一个 React App : The app starts inside the container, 但它不能从暴露的端口访问

css - 改变antdesign标题的颜色