【问题标题】:Can't debug React+Typescript in browser. JS files not generated in output directory无法在浏览器中调试 React+Typescript。 JS文件未在输出目录中生成
【发布时间】:2019-05-05 05:39:31
【问题描述】:

我已经为 React 项目配置了带有 Webpack 的 Typescript。问题是,我无法使用 .JS 源从我的浏览器进行调试。仅生成bundle.js。

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react"
    },
    "include": [
        "./src/**/**/*"
    ]
}

webpack.config.js(更新)

const path = require('path')
module.exports = {
    mode: 'development',
    entry: "./src/index.tsx",
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, './dist')
    },
    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx", ".js", ".json"]
    },
    devtool: 'source-map',
    module: {
        rules: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
            {  test: /\.tsx?$/, loader: "ts-loader" },
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { enforce: "pre", test: /\.js?$/, loader: "source-map-loader" }
        ]
    },
    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM",
        "react-router-dom": "ReactRouterDOM",
        "reactstrap": "Reactstrap",
        "flux" : "Flux"
    }
};

现在这是我在dist 目录中的输出

.
├── bundle.js
└── bundle.js.map

我的目的是为我的所有 .tsx 文件获取 .js 源,这样我就可以通过使用控制台的源选项卡在浏览器中进行调试。

【问题讨论】:

    标签: javascript reactjs typescript webpack


    【解决方案1】:

    您需要将它添加到您的 webpack 配置中并阅读相应的文档。

    devtool: 'source-map',
    

    【讨论】:

    • 嗨@Shanon,我以前试过,但也没有用。
    • 尝试摆脱这个,因为你想要来自 typescript 而不是 javascript { enforce: "pre", test: /\.js?$/, loader: "source-map-loader" } 只是加载器的源映射
    • 同样的问题。我也试过了。只有bundle.js 和bundle.js.map 正在创建中。
    【解决方案2】:

    我之前也遇到过同样的问题。 将 webpack config devtool 设置为 Cheap-module-eval-source-map

    devtool: 'cheap-module-eval-source-map'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-19
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2013-11-28
      • 2012-02-07
      • 2023-01-31
      相关资源
      最近更新 更多