0

I've configured Typescript with Webpack for a React project. Problem is, I can't debug from my browser using the .JS source. Only the bundle.js is being generated.

tsconfig.json

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

webpack.config.js (UPDATED)

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"
    }
};

Now this is my output in my dist directory

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

My intention is to get the .js sources for all my .tsx files so I can debug in my browser by just using the console's source tab.

2 Answers 2

1

You'll need to add this to your webpack config and read the appopriate documentation.

devtool: 'source-map',
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Shanon I tried that before and it doesn't work either.
Try get rid of this as you want source maps from typescript not javascript { enforce: "pre", test: /\.js?$/, loader: "source-map-loader" } just the loader
same issue. I also tried that. Only the bundle.js and bundle.js.map is being created.
0

I also encountered the same problem before. set webpack config devtool to cheap-module-eval-source-map

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.