Skip to content

Commit d46d8f6

Browse files
authored
Update source map docs for 2016 (#613)
1 parent 41b84c9 commit d46d8f6

File tree

1 file changed

+97
-53
lines changed

1 file changed

+97
-53
lines changed

docs/sourcemaps.rst

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,109 @@
33
Source Maps
44
===========
55

6-
In various browsers Sentry supports deminifying JavaScript via source
7-
maps. A source map is a file generated by your minifier which compresses a
8-
mapping of the minified file to the original uncompressed version(s).
6+
Sentry supports un-minifying JavaScript via `Source Maps
7+
<http://blog.getsentry.com/2015/10/29/debuggable-javascript-with-source-maps.html>`_. This lets you
8+
view source code context obtained from stack traces in their original untransformed form, which is
9+
is particularly useful for debugging minified code (e.g. UglifyJS), or transpiled code from a higher-level
10+
language (e.g. TypeScript, ES6).
911

10-
One important thing to note is that even though we support mapping files,
11-
a users browser may not actually be able to collect the required
12-
information for the server to generate a sourcemap.
12+
Generating a Source Map
13+
-----------------------
1314

14-
Sentry requires the following to be able to map tracebacks to their source:
15+
Most modern JavaScript transpilers support source maps. Below are instructions for some common tools.
1516

16-
* A source map header or footer
17-
* A publicly accessible uncompressed version of the source
18-
* A line of context that includes a line number, column number, and filename
17+
UglifyJS
18+
~~~~~~~~
1919

20-
The first two items are the responsibility of you, the end-user, and you
21-
can take care of publishing them as part of your build process. The latter
22-
however, with an individual line of context, is severely crippled in many
23-
browsers.
20+
UglifyJS is a popular tool for minifying your source code for production. It can dramatically
21+
reduce the size of your files by eliminating whitespace, rewriting variable names, removing dead code branches,
22+
and more.
2423

25-
One thing to note is that Sentry will attempt to process the source map
26-
before storing (or grouping) an event. This ensures that if we are able to
27-
deminify the source, we'll be able to more effectively group similar
28-
events over time.
24+
If you are using UglifyJS to minify your source code, the following command will additionally generate a source map
25+
that maps the minified code back to the original source:
2926

30-
Browser Support
31-
---------------
27+
::
3228

33-
In our experiences, the only browser that routinely delivers usable error
34-
reports is **Google Chrome**.
29+
node_modules/uglify-js/bin/uglifyjs {input} \
30+
--source-map-root={relroot}/ \
31+
--source-map-url={name}.map.js \
32+
--source-map={relpath}/{name}.map.js -o {output}
3533

36-
For additional information, see this more up-to-date `wiki page
37-
<https://github.com/ryanseddon/source-map/wiki/Source-maps:-languages,-tools-and-other-info>`_.
3834

39-
Generating a Source Map
40-
-----------------------
35+
Webpack
36+
~~~~~~~
4137

42-
While there are several compression libraries which support source maps,
43-
as of writing our recommendation is to use `UglifyJS
44-
<https://github.com/mishoo/UglifyJS2>`_. That said, many tools such as
45-
`webpack <http://webpack.github.io/>`_ and `browserify
46-
<http://browserify.org/>`_.
38+
Webpack is a powerful build tool that resolves and bundles your JavaScript modules into files fit for running in the
39+
browser. It also supports many different "loaders" which can convert higher-level languages like TypeScript and
40+
ES6/ES2015 into browser-compatible JavaScript.
4741

48-
As an example, we can look at how we used to do things with Sentry (pre-webpack):
42+
Webpack can be configured to output source maps by editing webpack.config.js.
4943

5044
::
5145

52-
node_modules/uglify-js/bin/uglifyjs {input} \
53-
--source-map-root={relroot}/ \
54-
--source-map-url={name}.map.js \
55-
--source-map={relpath}/{name}.map.js -o {output}
46+
module.exports = {
47+
// ... other config above ...
48+
entry: {
49+
"app": "src/app.js"
50+
},
51+
output: {
52+
path: path.join(__dirname, 'dist'),
53+
filename: "[name].js",
54+
sourceMapFilename: "[name].map.js",
55+
}
56+
};
5657

57-
We won't attempt to go into the complexities of source maps, so we
58-
recommend taking a stab at compiling them, and running them against a
59-
validator.
58+
Making Source Maps Available to Sentry
59+
--------------------------------------
6060

61-
Validating a Source Map
62-
-----------------------
61+
Source maps can be either:
6362

64-
We maintain an online validation tool that can be used to test your source
65-
(and sourcemaps) against: `sourcemaps.io <http://sourcemaps.io>`_.
63+
1) Served publicly over HTTP alongside your source files.
64+
65+
2) Uploaded directly to Sentry (**recommended**).
66+
67+
Hosting Source Map Files
68+
~~~~~~~~~~~~~~~~~~~~~~~~
69+
70+
By default, Sentry will look for source map directives in your compiled JavaScript files, which are located
71+
on the last line and have the following format:
72+
73+
.. code-block:: javascript
74+
75+
//# sourceMappingURL=<url>
76+
77+
When Sentry encounters such a directive, it will resolve the source map URL relative the source file in which
78+
it is found, and attempt an HTTP request to fetch it.
79+
80+
So for example if you have a minified JavaScript file located at ``http://example.org/js/app.min.js``. And in that file,
81+
on the last line, the following directive is found:
82+
83+
.. code-block:: javascript
84+
85+
//# sourceMappingURL=app.map.js
86+
87+
Sentry will attempt to fetch ``app.map.js`` from http://example.org/js/app.map.js.
88+
89+
Alternatively, during source map generation you can specify a fully qualified URL where your source maps are located:
90+
91+
.. code-block:: javascript
92+
93+
//# sourceMappingURL=http://example.org/js/app.map.js
94+
95+
While making source maps available to Sentry from your servers is the easiest integration, it is not always advisable:
96+
97+
* Sentry may not always be able to reach your servers.
98+
* If you do not specify versions in your asset URLs, there may be a version mismatch
99+
* The additional latency may mean that source mappings are not available for all errors.
100+
101+
For these reasons, it is recommended to upload source maps to Sentry beforehand (see below).
102+
103+
.. admonition:: Working Behind a Firewall
104+
105+
While the recommended solution is to upload your source artifacts to Sentry, sometimes it’s necessary to allow communication from Sentry’s internal IPs. For more information on Sentry’s public IPs, :ref:`ip-ranges`.
66106

67107
Uploading Source Maps to Sentry
68-
-------------------------------
108+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69109

70110
In many cases your application may sit behind firewalls or you simply
71111
can't expose source code to the public. Sentry provides an abstraction
@@ -105,7 +145,7 @@ sourcemaps point to.
105145
106146
When uploading the file, you'll need to reference it just as it would be referenced
107147
if a browser (or filesystem) had to resolve its path. So for example, if your sourcemap
108-
reference is just a relative path, it's relative to the location of the referencing file.
148+
reference is just a relative path, it's **relative to the location of the referencing file**.
109149

110150
So for example, if you have ``http://example.com/app.min.js``, and the file contains the
111151
reference to ``app.map.js``, the name of the uploaded file should be ``http://example.com/app.map.js``.
@@ -213,7 +253,7 @@ automatically uploaded to the release `2da95dfb052f477380608d59d32b4ab9`
213253
in this case. If you want to use other extensions you can provide it with
214254
the ``--ext`` parameter.
215255

216-
.. admonition:: Validating Sourcemaps
256+
.. admonition:: Validating Sourcemaps with Sentry CLI
217257

218258
Unfortunately it can be quite challenging to ensure that sourcemaps
219259
are actually valid themselves and uploaded correctly. To ensure
@@ -236,18 +276,22 @@ the ``--ext`` parameter.
236276

237277
.. sentry:edition:: hosted
238278
239-
Working Behind a Firewall
240-
-------------------------
241-
242-
While the recommended solution is to upload your source artifacts to
243-
Sentry, sometimes it's necessary to allow communication from Sentry's
244-
internal IPs. For more information on Sentry's public IPs, see :ref:`ip-ranges`.
245-
246279
Troubleshooting
247280
---------------
248281

249282
Source maps can sometimes be tricky to get going. If you're having trouble, try the following tips.
250283

284+
285+
Verify your source maps are built correctly
286+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
287+
288+
We maintain an online validation tool that can be used to test your source
289+
(and sourcemaps) against: `sourcemaps.io <http://sourcemaps.io>`_.
290+
291+
Alternatively, if you are using Sentry CLI to upload source maps to Sentry, you can use the `--validate`
292+
command line option to verify your source maps are correct.
293+
294+
251295
Verify sourceMappingURL is present
252296
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
253297

0 commit comments

Comments
 (0)