打字稿 - >使用webpack的Babel源图

时间:2016-03-08 15:07:44

标签: typescript webpack babeljs

从ts-loader问题复制粘贴,因为它可能更合适:

如何将打字稿源图传递给babel,以便结束源图指向原始文件,而不是编译的打字稿?

以下是我的开发设置示例:

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')
      }
    ]
  }
};

3 个答案:

答案 0 :(得分:2)

您可以将source-map-loader用于网络包。这是我的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" }
        ]
    }
};

tsconfig.js

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

source in chrome devtools

答案 1 :(得分:0)

请参阅loaders: [ // note that babel-loader is configured to run after ts-loader { test: /\.ts(x?)$/, loader: 'babel-loader!ts-loader' } ]

答案 2 :(得分:0)

旧问题,但如果其他人遇到此问题,请尝试在 webpack 配置中将 devtool 设置为不同的值,例如:

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

每个设置的预期输出: https://webpack.js.org/configuration/devtool/#root