简体   繁体   English

如何为coffeescript启用源映射?

[英]how can I enable a source map for coffeescript?

I recently discovered the existence of source maps in chrome via source debugging in the haxe language . 我最近通过haxe语言中的源代码调试发现了chrome中源代码映射的存在。 It allows to debug generated javascript inside the chrome browser while seeing the bug reason in the original source code. 它允许在Chrome浏览器中调试生成的javascript,同时查看原始源代码中的错误原因。

Has anyone written a source map generator for coffeescript / Is coffeescript source mappable ? 有没有人写过coffeescript /是coffeescript源可映射的源地图生成器?

It would help debug the javascript generated by coffeescript. 它将有助于调试coffeescript生成的javascript。

Coffeescript 1.6 has native support for source maps. Coffeescript 1.6原生支持源地图。

Use the "--map" or "-m" option to enable it. 使用“--map”或“-m”选项启用它。 Or if you use the npm compiler, you will have to add the sourceMap: true option. 或者,如果使用npm编译器,则必须添加sourceMap: true选项。

npm install -g coffee-script

Should install coffee-script as a global module. 应该将coffee-script安装为全局模块。 Check version > 1.6 by typing 通过键入检查版本> 1.6

coffee -v

If you need help you can use. 如果您需要帮助,可以使用。 Use it to see meaning of options used below 用它来查看下面使用的选项的含义

coffee -h

For regular compilation use 用于常规编译使用

coffee -mo script/ -cw src/

This should auto-generate maps files. 这应该自动生成地图文件。 I leave this running in terminal as I code, it compiles every time I save. 当我编码时,我将它留在终端中运行,每次保存时它都会编译。

KNOWN BUG: 已知的BUG:

The current coffee-script compiler does not seem to handle different /src and /script directories. 当前的coffee-script编译器似乎不处理不同的/ src和/ script目录。 In map file you find that sources = {filename} rather than {relative file path}. 在映射文件中,您会发现sources = {filename}而不是{relative file path}。

SOLUTION: 解:

  1. Keep your .coffee files in same directory as .js 将.coffee文件保存在与.js相同的目录中
  2. Modify source directive manually in .map file. 在.map文件中手动修改源指令。 This will get overwritten again on next save 这将在下次保存时再次被覆盖

This has long been an active issue on the CoffeeScript project (indeed, it predates the source map standard). 这一直是CoffeeScript项目的一个活跃问题(事实上,它早于源地图标准)。 However, no (complete) CoffeeScript source map generator exists yet. 但是,还没有(完整的)CoffeeScript源地图生成器存在。 For discussion, see https://github.com/jashkenas/coffee-script/issues/558 有关讨论,请参阅https://github.com/jashkenas/coffee-script/issues/558

Source map support is also one of the goals of the "CoffeeScript Redux" compiler that was recently funded on Kickstarter (see http://www.kickstarter.com/projects/michaelficarra/make-a-better-coffeescript-compiler ). 源地图支持也是最近由Kickstarter资助的“CoffeeScript Redux”编译器的目标之一(请参阅http://www.kickstarter.com/projects/michaelficarra/make-a-better-coffeescript-compiler )。 That project has just begun; 该项目刚刚开始; you can watch it at https://github.com/michaelficarra/CoffeeScriptRedux 你可以在https://github.com/michaelficarra/CoffeeScriptRedux上观看

Ps, if you're on vim, use: Ps,如果您使用的是vim,请使用:

au BufWritePost *.coffee silent make -m

which compiles with source map on file save. 编译源文件保存文件。 I've found it extremely handy when I want some random buffer to start compiling coffee for me. 当我想要一些随机缓冲区开始为我编制咖啡时,我发现它非常方便。

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

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