- CoffeeScript Application Development Cookbook
- Mike Hatfield
- 321字
- 2021-07-23 19:35:52
Debugging CoffeeScript using source maps
If you have spent any time in the JavaScript community, you would have, no doubt, seen some discussions or rants regarding the weak debugging story for CoffeeScript. In fact, this is often a top argument some give for not using CoffeeScript at all. In this recipe, we will examine how to debug our CoffeeScript application using source maps.
Getting ready
The problem in debugging CoffeeScript stems from the fact that CoffeeScript compiles into JavaScript which is what the browser executes. If an error arises, the line that has caused the error sometimes cannot be traced back to the CoffeeScript source file very easily. Also, the error message is sometimes confusing, making troubleshooting that much more difficult.
Recent developments in the web development community have helped improve the debugging experience for CoffeeScript by making use of a concept known as a source map. In this section, we will demonstrate how to generate and use source maps to help make our CoffeeScript debugging easier.
To use source maps, you need only a base installation of CoffeeScript.
How to do it...
You can generate a source map for your CoffeeScript code using the -m
option on the CoffeeScript command:
coffee -m -c employee.coffee
How it works...
Source maps provide information used by browsers such as Google Chrome that tell the browser how to map a line from the compiled JavaScript code back to its origin in the CoffeeScript file.
Source maps allow you to place breakpoints in your CoffeeScript file and analyze variables and execute functions in your CoffeeScript module. This creates a JavaScript file called employee.js
and a source map called employee.js.map
.
If you look at the last line of the generated employee.js
file, you will see the reference to the source map:
//# sourceMappingURL=employee.js.map
Google Chrome uses this JavaScript comment to load the source map.
The following screenshot demonstrates an active breakpoint and console in Goggle Chrome:

- JSP網(wǎng)絡(luò)編程(學(xué)習(xí)筆記)
- Android Development with Kotlin
- C/C++常用算法手冊(cè)(第3版)
- Python高效開(kāi)發(fā)實(shí)戰(zhàn):Django、Tornado、Flask、Twisted(第2版)
- Bootstrap Essentials
- NoSQL數(shù)據(jù)庫(kù)原理
- Android開(kāi)發(fā)三劍客:UML、模式與測(cè)試
- 智能搜索和推薦系統(tǒng):原理、算法與應(yīng)用
- 快速入門與進(jìn)階:Creo 4·0全實(shí)例精講
- Learning Nessus for Penetration Testing
- Mastering Adobe Captivate 7
- AI自動(dòng)化測(cè)試:技術(shù)原理、平臺(tái)搭建與工程實(shí)踐
- Python 3快速入門與實(shí)戰(zhàn)
- 機(jī)器人ROS開(kāi)發(fā)實(shí)踐
- iOS程序員面試筆試真題與解析