html - 使用 Chrome 调试 React TypeScript .tsx 文件 - Webpack

标签 html google-chrome reactjs typescript webpack

我正在使用 webpack 来捆绑我的 js 文件,所以通常我只能在 Chrome 的源代码下看到一个大的捆绑文件。但是,如果我将 debugger; 添加到我的 .tsx 文件中,我可以很好地看到单个文件。我的问题是我是否可以让 webpack 在 Chrome 源中输出我的所有文件,以便我可以在那里浏览它们,如果我不想让调试器停止,只需单击一行?

在下面的屏幕截图中,我想要一个用于Scripts/src 的文件夹,然后是我所有的文件。

enter image description here

示例代码:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
    <title>App</title>
</head>
<body>
<div id="app"></div>
<script src="/Scripts/dist/bundle.js"></script>
</body>
</html>

索引.tsx:

import * as React from "react";
import * as ReactDOM from "react-dom";
import * as ReactRouter from "react-router";
import * as ReactBootstrap from "react-bootstrap";
import { Test } from "./components/test";

ReactDOM.render(
    <div>
        <Test text="Well!" />
        <Container />
    </div>,
    document.getElementById("app")
);

测试.tsx:

import * as React from "react";

export interface ITestProps {
    text: string
}

export class Test extends React.Component<ITestProps, {}> {
    render() {
        debugger;
        return <div className="well">{this.props.text}</div>;
    }
}

webpack.config.json:

var webpack = require('webpack');
var path = require("path");
var proxy = 'localhost:61299';

module.exports = {
    entry: [
        // activate HMR for React
        'react-hot-loader/patch',

        // the entry point of our app
        './Scripts/src/index.tsx',
    ],
    output: {
        filename: "./Scripts/dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            { test: /\.tsx?$/, loader: ['react-hot-loader/webpack', 'ts-loader'] }
        ]
    },

    plugins: [
        // enable HMR globally
        new webpack.HotModuleReplacementPlugin(),         

        // prints more readable module names in the browser console on HMR updates
        new webpack.NamedModulesPlugin(),
    ],

    devServer: {
        proxy: {
            '*': {
                target: 'http://' + proxy,
            }
        },
        port: 8080,
        host: '0.0.0.0',
        hot: true,
    },
}

最佳答案

因为您已经使用 webpack (devtool: "source-map") 生成了源映射,所以这应该已经可以工作了;)

不要在“localhost”中查看源代码,而是在 chrome 调试器中使用 webpack:// 项。这是屏幕截图中的最后一项。

如果源映射的生成工作正常,您应该在那里有源文件夹结构。它可能包含在名为 ..

的文件夹中

例如: enter image description here

但我必须警告你。有时 source maps 不能正常工作,你的断点会在别处结束 :-x

关于html - 使用 Chrome 调试 React TypeScript .tsx 文件 - Webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43627243/

相关文章:

google-chrome - CSS3 FlexBox过渡

reactjs - React Query 使用旧数据进行重新获取

jquery - Chrome 中的子菜单定位错误

php - php/html中多列显示单列mysql数据

google-chrome - 仅当笔画宽度使用 2px 渲染时,Chrome 中的剪切 SVG 路径渲染问题

android - ERR_SSL_PROTOCOL_ERROR 在特定设备上使用 Let's Encrypt

javascript - 为什么我的组件检测不到 props 更改?

reactjs - react : Action vs modifying state variable

PHP + MYSQL 插入所有列为空

javascript - 通过 jquery 创建的按钮不响应点击