2017-04-26 1322 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

相關問題