Webpack + React + Typescript

bar*_*lov 11 typescript reactjs webpack

是否可以将Webpack与React和Typescript一起使用,并能够将它们捆绑到Web捆绑包中,但仍然能够调试原始的TypeScript和React代码?在webpack中我使用ts-loader和ts-jsx-loader加上devtool:"source-map",但是当我尝试进行浏览器调试时,我看不到原始的ts代码,而是看到代码已被更改通过webpack.

我目前的基本webpack.config.js文件:

var webpack = require('webpack');
module.exports = {
  entry: ['./app/main.ts'],
  output: {
    path: './build',
    filename: 'bundle.js'
  },
  debug: true,
  devtool: 'source-map',
  plugins: [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin()
  ],
  resolve: {
    extensions: ['', '.ts', '.js']
  },
  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'ts-loader!ts-jsx-loader'
      }
    ]
  }
};
Run Code Online (Sandbox Code Playgroud)

tsconfig.json:

{
    "compileOnSave": false,
    "version": "1.5.0-alpha",
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "noLib": false,
        "sourceMap": true,
        "noImplicitAny": true,
        "removeComments": true
    },
    "files": [
        "./AppComponent.ts",
        "./libs/jsx.d.ts",
        "./libs/react.d.ts",
        "./libs/webpack-runtime.d.ts",
        "./main.ts"
    ]
}
Run Code Online (Sandbox Code Playgroud)

例如 - 我的oryginal .ts文件看起来像:

import React = require('react');

class AppComponent extends React.Component<any, any> {
  render () {
    return React.jsx(`
      <h1>He world!</h1>
    `);
  }
};
export = AppComponent;
Run Code Online (Sandbox Code Playgroud)

在chrome调试器中,它看起来像这样:

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var React = __webpack_require__(2);
var AppComponent = (function (_super) {
    __extends(AppComponent, _super);
    function AppComponent() {
        _super.apply(this, arguments);
    }
    AppComponent.prototype.render = function () {
        return (React.createElement("h1", null, "He world!"));
    };
    return AppComponent;
})(React.Component);
;
module.exports = AppComponent;


/*****************
 ** WEBPACK FOOTER
 ** ./app/AppComponent.ts
 ** module id = 158
 ** module chunks = 0
 **/
Run Code Online (Sandbox Code Playgroud)

Jam*_*tly 1

您可能没有sourceMaptsconfig.json文件中指定,因此 TypeScript 编译器不会输出源映射。