Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript -> Babel sourcemaps using webpack

Copy paste from ts-loader issues as it might be more appropriate here:

How to pass the typescript sourcemaps to babel so the end sourcemap point to the original file and not the compiled typescript one?

Here's an example of my dev settings:

tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "jsx": "react",
    "noImplicitAny": false,
    "sourceMap": true
  },
  "exclude": ["node_modules", "gulpfile.js", "webpack.config.js", "server.js"]
}

webpack.dev.js:

var path = require("path");
var webpack = require("webpack");

module.exports = {
  devtool: "eval",
  entry: [
    "webpack-hot-middleware/client",
    "./src/app/index",
  ],
  output: {
    path: path.join(__dirname, "build"),
    filename: "app.js",
    publicPath: "/static/"
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.ProvidePlugin({
      'window.fetch': 'exports?self.fetch!whatwg-fetch'
    })
  ],
  resolve: {
    extensions: ['', '.ts', '.tsx', '.js']
  },
  module: {
    noParse: [
      /\/sinon.js/
    ],
    preLoaders: [{
      test: /\.ts(x?)$/,
      loader: "tslint",
      exclude: /node_modules/
    }],
    loaders: [
      {
        test: /\.tsx?$/,
        loader: 'babel-loader!ts-loader',
        exclude: /node_modules/,
        include: path.join(__dirname, 'src')
      }
    ]
  }
};
like image 675
Vincent P Avatar asked Mar 08 '16 15:03

Vincent P


People also ask

Can I use Babel with Webpack?

There is a specific folder structure to be followed when using Webpack and Babel in your project. We need two separate folders, dist for the output files and src for the input files. You have to keep the index. html file inside the dist folder.

What is Sourcemaps in Webpack?

Source maps connect the bundle file with corresponding source files. Source maps are not a Webpack only concept. It is a common debugging technique which is respected by all modern browsers. Webpack has the ability to generate source maps. Let us try generating a source map.

Can Webpack work with TypeScript?

With webpack, TypeScript code is compiled into a JavaScript file that is browser-friendly. With webpack loaders, you can also convert SASS and LESS files into a single CSS bundle file.


2 Answers

You can use source-map-loader for webpack. Here is my webpack.config.js:

module.exports = {
    entry: "./app.ts",
    output: {
        filename: "./bundle.js",
    },

    devtool: "source-map",

    resolve: {
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".js"]
    },

    module: {
        loaders: [
            // ts -> ES6 -> babel -> ES5
            { test: /\.tsx?$/, loaders: ["babel-loader", "ts-loader"] }
        ],

        preLoaders: [
            { test: /\.js$/, loader: "source-map-loader" }
        ]
    }
};

And tsconfig.js:

{
    "compilerOptions": {
        "target": "es6",
        "sourceMap": true
    },
    "exclude": [
        "node_modules"
    ]
}

source in chrome devtools

like image 164
kimsk Avatar answered Sep 21 '22 18:09

kimsk


Old issue, but if anyone else runs across this try setting devtool to a different value in the webpack config like:

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

Expected Output Per Setting: https://webpack.js.org/configuration/devtool/#root

like image 30
theOwl Avatar answered Sep 25 '22 18:09

theOwl