2017-04-26 1324 views
3

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

在下面的屏幕截图中,我想要一个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> 

index.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") 
); 

test.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, 
    }, 
} 

回答

3

既然你已经生成webpackdevtool: "source-map")源地图这应该已经工作;)

在查看源,而不是 “localhost”,则使用webpack://项目在Chrome调试器中。这是你截图中的最后一项。

如果源地图的生成工作正常,你应该有你的源文件夹结构。它可能包含在名为.的文件夹中。

例如: enter image description here

我不得不虽然警告你。有时源地图无法正常工作,并且您在其他地方断点最终上升:-x

+0

工作就像一个魅力,谢谢! :) – Ogglas

相关问题