我正在使用来自grunt任务的webpack和ts-loader来转换Typescript并生成用于调试生产的源图。我的团队希望能够看到原始的Typescript源代码,而不仅仅是JavaScript源代码。我已经仔细阅读了许多文档页面,以及许多问题/答案,寻找合适的解决方案,但到目前为止,我无法在浏览器中看到Typescript文件,只能看到JavaScript版本。
我们的项目根模块或webpack任务的“条目”webpackEntry
是一个Javascript文件,它需要JavaScript文件,其中一些是从TypeScript编译的,但有些只是手动创建的。
我配置了一个webpack“prod”任务:
webpack: {
options: {
entry: webpackEntry,
output: {
path: webpackDest,
filename: 'app-min.js',
libraryTarget: "assign",
pathInfo: true
},
externals: grunt.file.readJSON('app-ui/webpack-externals.json'),
progress: true
},
prod: {
devtool: 'source-map',
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.SourceMapDevToolPlugin({
test: /\.tsx?$/,
exclude: 'node_modules',
module: true,
filename: '[file].map',
append: false
})
],
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js','.jsx']
},
module: {
loaders: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.tsx?$/, loader: 'ts-loader', exclude: 'node_modules' }
]
},
ts: {
// configures the ts compiler:
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"jsx": "react",
"experimentalDecorators": true
},
"exclude": [
"node_modules" // add other exclusions for the ts compiler.
]
}
}...
和tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
...但我在Chrome开发工具源中看到的只是JavaScript源文件。我希望能够看到原始的JavaScript源文件,以及原始的Typescript文件,但不能看到已编译的JavaScript文件。
更新: 我按照@ basarat的建议在下面添加了source-map-loader工具。我仍然只在dev-tools窗口中看到JavaScript文件。
相关摘录:
module: {
loaders: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.tsx?$/, loader: 'ts-loader', exclude: 'node_modules' }
],
preLoaders: [
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ test: /\.js$/, loader: "source-map-loader", exclude: ['node_modules', 'ext-*', 'lib', 'tools'] }
]
}, ...
ts-loader中的任何专家都能为此提供帮助吗?在此先感谢您的帮助!
答案 0 :(得分:4)
您需要使用sourcemap加载程序。这包括:https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/quick-start/react-webpack.md
您的webpack配置应该是:
module: {
loaders: [
// All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
{ test: /\.tsx?$/, loader: "ts-loader" }
],
preLoaders: [
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ test: /\.js$/, loader: "source-map-loader" }
]
},
答案 1 :(得分:1)
您可以将devTools值提供给您的webpack配置,如下所示:
module.exports = {
entry:'./app/app',
output:{
filename:'bundle.js',
},
resolve:{
extensions:['','.ts','.js'],
modulesDirectories: ['node_modules']
},
module:{
loaders:[
{
test: /\.ts$/, loader: 'ts-loader'
}
]
},
devtool: "sourcemap" /*<=================*/
}
Microsoft指南位于:https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/tutorials/React%20%26%20Webpack.md