首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用webpack ts-loader时,Typescript源代码不显示

使用webpack ts-loader时,Typescript源代码不显示
EN

Stack Overflow用户
提问于 2016-03-31 07:57:22
回答 2查看 4K关注 0票数 6

我正在使用webpack和ts-loader从一项繁琐的任务中转换文字,并生成源代码以供调试产品使用。我的团队希望能够看到原始的Typescript源代码,而不仅仅是JavaScript源代码。我仔细阅读了许多页面的文档和许多问题/答案,以寻找正确的解决方案,但到目前为止,我在浏览器中看不到Typescript文件,只能看到JavaScript版本。

我们的项目根模块,或“入口”的webpack任务,webpackEntry,是一个Javascript文件,它需要JavaScript文件,其中一些是从TypeScript编译,但一些只是手动创建。

我有一个webpack "prod“任务,配置如下:

代码语言:javascript
复制
        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:

代码语言:javascript
复制
{
   "compilerOptions": {
        "target": "es5",
        "sourceMap": true
    },
    "exclude": [
        "node_modules"
      ]
}

..。但我在Chrome Dev工具源码中看到的只有JavaScript源文件。我希望能够看到原始的JavaScript源文件,以及原始的Typescript文件,但不能看到转换后的JavaScript文件。

更新:我遵循了@basarat的建议,添加了source-map-loader工具。在开发工具窗口中,我仍然只能看到JavaScript文件。

相关摘录:

代码语言: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方面的专家能帮上忙?提前感谢您的帮助!

EN

回答 2

Stack Overflow用户

发布于 2016-03-31 08:04:38

您需要使用sourcemap加载器。这里介绍了这一点:https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/quick-start/react-webpack.md

您的webpack配置应该是这样的:

代码语言:javascript
复制
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" }
    ]
},
票数 4
EN

Stack Overflow用户

发布于 2017-01-22 07:33:44

您可以将sourcemap的devTools值提供给您的webpack配置,如下所示:

代码语言:javascript
复制
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" /*<=================*/
}

微软的指南位于此处:https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/tutorials/React%20%26%20Webpack.md

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36321862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档