Projekt

Obecné

Profil

Stáhnout (8.35 KB) Statistiky
| Větev: | Revize:
1
# Source Map Support
2
[![Build Status](https://travis-ci.org/evanw/node-source-map-support.svg?branch=master)](https://travis-ci.org/evanw/node-source-map-support)
3

    
4
This module provides source map support for stack traces in node via the [V8 stack trace API](https://github.com/v8/v8/wiki/Stack-Trace-API). It uses the [source-map](https://github.com/mozilla/source-map) module to replace the paths and line numbers of source-mapped files with their original paths and line numbers. The output mimics node's stack trace format with the goal of making every compile-to-JS language more of a first-class citizen. Source maps are completely general (not specific to any one language) so you can use source maps with multiple compile-to-JS languages in the same node process.
5

    
6
## Installation and Usage
7

    
8
#### Node support
9

    
10
```
11
$ npm install source-map-support
12
```
13

    
14
Source maps can be generated using libraries such as [source-map-index-generator](https://github.com/twolfson/source-map-index-generator). Once you have a valid source map, insert the following line at the top of your compiled code:
15

    
16
```js
17
require('source-map-support').install();
18
```
19

    
20
And place a source mapping comment somewhere in the file (usually done automatically or with an option by your transpiler):
21

    
22
```
23
//# sourceMappingURL=path/to/source.map
24
```
25

    
26
If multiple sourceMappingURL comments exist in one file, the last sourceMappingURL comment will be
27
respected (e.g. if a file mentions the comment in code, or went through multiple transpilers).
28
The path should either be absolute or relative to the compiled file.
29

    
30
It is also possible to to install the source map support directly by
31
requiring the `register` module which can be handy with ES6:
32

    
33
```js
34
import 'source-map-support/register'
35

    
36
// Instead of:
37
import sourceMapSupport from 'source-map-support'
38
sourceMapSupport.install()
39
```
40
Note: if you're using babel-register, it includes source-map-support already.
41

    
42
It is also very useful with Mocha:
43

    
44
```
45
$ mocha --require source-map-support/register tests/
46
```
47

    
48
#### Browser support
49

    
50
This library also works in Chrome. While the DevTools console already supports source maps, the V8 engine doesn't and `Error.prototype.stack` will be incorrect without this library. Everything will just work if you deploy your source files using [browserify](http://browserify.org/). Just make sure to pass the `--debug` flag to the browserify command so your source maps are included in the bundled code.
51

    
52
This library also works if you use another build process or just include the source files directly. In this case, include the file `browser-source-map-support.js` in your page and call `sourceMapSupport.install()`. It contains the whole library already bundled for the browser using browserify.
53

    
54
```html
55
<script src="browser-source-map-support.js"></script>
56
<script>sourceMapSupport.install();</script>
57
```
58

    
59
This library also works if you use AMD (Asynchronous Module Definition), which is used in tools like [RequireJS](http://requirejs.org/). Just list `browser-source-map-support` as a dependency:
60

    
61
```html
62
<script>
63
  define(['browser-source-map-support'], function(sourceMapSupport) {
64
    sourceMapSupport.install();
65
  });
66
</script>
67
```
68

    
69
## Options
70

    
71
This module installs two things: a change to the `stack` property on `Error` objects and a handler for uncaught exceptions that mimics node's default exception handler (the handler can be seen in the demos below). You may want to disable the handler if you have your own uncaught exception handler. This can be done by passing an argument to the installer:
72

    
73
```js
74
require('source-map-support').install({
75
  handleUncaughtExceptions: false
76
});
77
```
78

    
79
This module loads source maps from the filesystem by default. You can provide alternate loading behavior through a callback as shown below. For example, [Meteor](https://github.com/meteor) keeps all source maps cached in memory to avoid disk access.
80

    
81
```js
82
require('source-map-support').install({
83
  retrieveSourceMap: function(source) {
84
    if (source === 'compiled.js') {
85
      return {
86
        url: 'original.js',
87
        map: fs.readFileSync('compiled.js.map', 'utf8')
88
      };
89
    }
90
    return null;
91
  }
92
});
93
```
94

    
95
The module will by default assume a browser environment if XMLHttpRequest and window are defined. If either of these do not exist it will instead assume a node environment. 
96
In some rare cases, e.g. when running a browser emulation and where both variables are also set, you can explictly specify the environment to be either 'browser' or 'node'. 
97

    
98
```js
99
require('source-map-support').install({
100
  environment: 'node'
101
});
102
```
103

    
104
To support files with inline source maps, the `hookRequire` options can be specified, which will monitor all source files for inline source maps.
105

    
106

    
107
```js
108
require('source-map-support').install({
109
  hookRequire: true
110
});
111
```
112

    
113
This monkey patches the `require` module loading chain, so is not enabled by default and is not recommended for any sort of production usage.
114

    
115
## Demos
116

    
117
#### Basic Demo
118

    
119
original.js:
120

    
121
```js
122
throw new Error('test'); // This is the original code
123
```
124

    
125
compiled.js:
126

    
127
```js
128
require('source-map-support').install();
129

    
130
throw new Error('test'); // This is the compiled code
131
// The next line defines the sourceMapping.
132
//# sourceMappingURL=compiled.js.map
133
```
134

    
135
compiled.js.map:
136

    
137
```json
138
{
139
  "version": 3,
140
  "file": "compiled.js",
141
  "sources": ["original.js"],
142
  "names": [],
143
  "mappings": ";;AAAA,MAAM,IAAI"
144
}
145
```
146

    
147
Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js):
148

    
149
```
150
$ node compiled.js
151

    
152
original.js:1
153
throw new Error('test'); // This is the original code
154
      ^
155
Error: test
156
    at Object.<anonymous> (original.js:1:7)
157
    at Module._compile (module.js:456:26)
158
    at Object.Module._extensions..js (module.js:474:10)
159
    at Module.load (module.js:356:32)
160
    at Function.Module._load (module.js:312:12)
161
    at Function.Module.runMain (module.js:497:10)
162
    at startup (node.js:119:16)
163
    at node.js:901:3
164
```
165

    
166
#### TypeScript Demo
167

    
168
demo.ts:
169

    
170
```typescript
171
declare function require(name: string);
172
require('source-map-support').install();
173
class Foo {
174
  constructor() { this.bar(); }
175
  bar() { throw new Error('this is a demo'); }
176
}
177
new Foo();
178
```
179

    
180
Compile and run the file using the TypeScript compiler from the terminal:
181

    
182
```
183
$ npm install source-map-support typescript
184
$ node_modules/typescript/bin/tsc -sourcemap demo.ts
185
$ node demo.js
186

    
187
demo.ts:5
188
  bar() { throw new Error('this is a demo'); }
189
                ^
190
Error: this is a demo
191
    at Foo.bar (demo.ts:5:17)
192
    at new Foo (demo.ts:4:24)
193
    at Object.<anonymous> (demo.ts:7:1)
194
    at Module._compile (module.js:456:26)
195
    at Object.Module._extensions..js (module.js:474:10)
196
    at Module.load (module.js:356:32)
197
    at Function.Module._load (module.js:312:12)
198
    at Function.Module.runMain (module.js:497:10)
199
    at startup (node.js:119:16)
200
    at node.js:901:3
201
```
202
    
203
#### CoffeeScript Demo
204

    
205
demo.coffee:
206

    
207
```coffee
208
require('source-map-support').install()
209
foo = ->
210
  bar = -> throw new Error 'this is a demo'
211
  bar()
212
foo()
213
```
214

    
215
Compile and run the file using the CoffeeScript compiler from the terminal:
216

    
217
```sh
218
$ npm install source-map-support coffee-script
219
$ node_modules/coffee-script/bin/coffee --map --compile demo.coffee
220
$ node demo.js
221

    
222
demo.coffee:3
223
  bar = -> throw new Error 'this is a demo'
224
                     ^
225
Error: this is a demo
226
    at bar (demo.coffee:3:22)
227
    at foo (demo.coffee:4:3)
228
    at Object.<anonymous> (demo.coffee:5:1)
229
    at Object.<anonymous> (demo.coffee:1:1)
230
    at Module._compile (module.js:456:26)
231
    at Object.Module._extensions..js (module.js:474:10)
232
    at Module.load (module.js:356:32)
233
    at Function.Module._load (module.js:312:12)
234
    at Function.Module.runMain (module.js:497:10)
235
    at startup (node.js:119:16)
236
```
237

    
238
## Tests
239

    
240
This repo contains both automated tests for node and manual tests for the browser. The automated tests can be run using mocha (type `mocha` in the root directory). To run the manual tests:
241

    
242
* Build the tests using `build.js`
243
* Launch the HTTP server (`npm run serve-tests`) and visit
244
  * http://127.0.0.1:1336/amd-test
245
  * http://127.0.0.1:1336/browser-test
246
  * http://127.0.0.1:1336/browserify-test - **Currently not working** due to a bug with browserify (see [pull request #66](https://github.com/evanw/node-source-map-support/pull/66) for details).
247
* For `header-test`, run `server.js` inside that directory and visit http://127.0.0.1:1337/
248

    
249
## License
250

    
251
This code is available under the [MIT license](http://opensource.org/licenses/MIT).
(2-2/6)