简体   繁体   English

Sourcemaps使用typescript和browserify / gulp可怕地破坏了

[英]Sourcemaps horribly broken using typescript and browserify/gulp

I am successfully getting sourcemaps output from my build process which uses browserify with gulp. 我成功地从我的构建过程获取源映射输出,该过程使用了browserify和gulp。 However, the sourcemaps don't work when debugging - breakpoints often jump to a different line when I set them (in Chrome), and it is clear the script is not actually pausing where it says it is. 但是,源代码在调试时不起作用 - 断点在设置时会经常跳转到不同的行(在Chrome中),很明显,脚本实际上并没有暂停它所说的位置。 When I hover over variables to view their values, it shows the wrong thing, and so on. 当我将鼠标悬停在变量上以查看其值时,它会显示错误的内容,依此类推。 I used this tutorial as a basis, so it seems like it should work. 我使用本教程作为基础,所以它看起来应该有效。

The relevant build step in my gulpfile.js is: 我的gulpfile.js的相关构建步骤是:

return browserify({
  basedir: '.',
  debug: mode === 'dev',
  entries: [`${paths.src}/app/${buildConfig.appFolder}/${buildConfig.appSrc}`],
  cache: {},
  packageCache: {},
})
  .plugin(tsify)
  .bundle()
  .pipe(source('app.js'))
  .pipe(gulp.dest(`${paths.dist}/app/${buildConfig.appFolder}`))
  .on('error', err => {
    console.error(err.toString());
    done(err);
  });

And tsconfig.json is: tsconfig.json是:

{
  "compilerOptions": {
    "lib": ["es2018", "dom"],
    "target": "es2015",
    "jsx": "react",
    "moduleResolution": "node",
    "diagnostics": true,
    "types": [
      "react",
      "react-dom",
      "lodash"
    ]
  }
}

Try setting the compiler options: 尝试设置编译器选项:

{
  "compilerOptions": {
     "sourceMap": true,
     "inlineSources": true
  }
}

"sourceMap": true tells typescript to emit source maps. "sourceMap": true告诉"sourceMap": true发出源映射。 "inlineSources": true tells it to embed the typescript source within the source map itself. "inlineSources": true告诉它在源映射本身中嵌入"inlineSources": true源。

If this still doesn't work, you could try using "inlineSourceMap": true (in conjunction with inlineSources ) which causes the sourcemaps to actually be inlined within the emitted javascript (best to make sure production builds disable this to avoid bloating your final bundles) 如果这仍然不起作用,您可以尝试使用"inlineSourceMap": true (与inlineSources结合使用),这会导致源映射实际在发出的javascript中内联(最好确保生成版本禁用此功能以避免膨胀最终的包)

Whether this will work with your exact gulp / browserify setup I'm unsure. 这是否适用于您的确切gulp / browserify设置我不确定。 That tutorial you linked mentions adding: 您链接的教程添加:

var sourcemaps = require('gulp-sourcemaps');

....
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(sourcemaps.write('./'))

Which may be worth a try as well, though this appeared to be in relation to uglify, https://www.typescriptlang.org/docs/handbook/gulp.html#uglify 这也许值得一试,尽管这似乎与uglify有关, https: //www.typescriptlang.org/docs/handbook/gulp.html#uglify

ref: https://www.typescriptlang.org/docs/handbook/compiler-options.html 参考: https//www.typescriptlang.org/docs/handbook/compiler-options.html

Overall I've found sourcemaps with typescript to be frustrating - even with a correct setup I still find occasionally I get poor breakpoint / code stepping behavior, though it has definitely been improving over the years. 总的来说,我发现带有打字稿的源图是令人沮丧的 - 即使设置正确我仍然偶尔会发现我的断点/代码步进行为很差,尽管多年来它一直在改进。

As a general tip I find having third party dependencies split off to a separate bundle, and then blackboxing this in the chrome dev tools to help, as it avoids stepping into library code for promises, observables, etc 作为一般性提示,我发现将第三方依赖关系拆分为一个单独的包,然后在chrome dev工具中对其进行黑盒子帮助,因为它避免了为promises,observable等插入库代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM