You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 24, 2024. It is now read-only.
Define how to extract the sourceMappingURL comment
This patch explicitly defines how to extract such comments from
JavaScript, CSS and WebAssembly sources.
It defines multiple ways to do so: either by actually parsing the code,
or by just going through all the lines of the program looking for what
"looks like" a comment. This is so that different implementations can
choose what's best for them, depending on whether they are already
parsing the code or not.
To ensure consist behavior accross implementations that choose different
strategies, the specification enforces additional requirements on
tools that append a `sourceMappingURL` comment to the generated code:
the comment must be placed in such a way that all extraction methods
yield the same result. This is not an unresonable burden, since
if the progeram is syntactically valid, simply adding the comment at the
end of the file only potentially followed by other tool-injected
comments is enough. This requirement is lifted if the input code given
to the tool is already "maliciously crafted", since we would otherwise
require tool to go rewrite that code (for example, splitting strings
that contain something that looks like a comment).
I have left the CSS extraction method as TODO because first I want to
check how do you feel about the JS one. It has the following properties:
- It iterates line by line. Implementations can thus optimize it by
going through each line _in reverse order_, and then scanning through
its characters from the beginning to the end (which is what a regexp
would do).
- It expects multi-line comments to actually be in a single line.
- It returns the last `sourceMappingURL` comment (or well, comment-like)
found in the source.
- It only considers comments after the last piece of code (i.e. it
discards any comment found so far every time it sees some non-comment
non-whitespace characters).
- It has no requirements about what is _before_ a comment. Adding the
comment at the end of the file without first ensuring that there is
a newline before it is valid.
@@ -339,38 +357,12 @@ to have some conventions for the expected use-case of web server-hosted JavaScri
339
357
There are two suggested ways to link source maps to the output. The first requires server
340
358
support in order to add an HTTP header and the second requires an annotation in the source.
341
359
342
-
The HTTP header should supply the source map URL reference as:
343
-
344
-
```
345
-
sourcemap: <url>
346
-
```
347
-
348
-
Note: Previous revisions of this document recommended a header name of `x-sourcemap`. This
349
-
is now deprecated; `sourcemap` is now expected.
350
-
351
-
The generated code should include a line at the end of the source, with the following form:
352
-
353
-
```
354
-
//# sourceMappingURL=<url>
355
-
```
356
-
357
-
Note: The prefix for this annotation was initially `//@` however this conflicts with Internet
358
-
Explorer's Conditional Compilation and was changed to `//#`. Source map generators must only emit `//#`
359
-
while source map consumers must accept both `//@` and `//#`.
360
-
361
-
Note: `//@` is needed for compatibility with some existing legacy source maps.
362
-
363
-
364
-
This recommendation works well for JavaScript, but it is expected that other source files will
365
-
have different conventions. For instance, for CSS `/*# sourceMappingURL=<url> */` is proposed.
366
-
On the WebAssembly side, such a URL is encoded using [[WasmNamesBinaryFormat]], and it's placed as the content of the custom section ([[WasmCustomSection]]) named `sourceMappingURL`.
367
-
368
-
`<url>` is a URL as defined in [[URL]]; in particular,
360
+
Source maps are linked through URLs as defined in [[URL]]; in particular,
369
361
characters outside the set permitted to appear in URIs must be percent-encoded
370
362
and it may be a data URI. Using a data URI along with [=sourcesContent=] allows
371
363
for a completely self-contained source map.
372
364
373
-
<ins>The HTTP `SourceMap` header has precedence over a source annotation, and if both are present,
365
+
<ins>The HTTP `sourcemap` header has precedence over a source annotation, and if both are present,
374
366
the header URL should be used to resolve the source map file.</ins>
375
367
376
368
Regardless of the method used to retrieve the [=Source Mapping URL=] the same
@@ -394,6 +386,181 @@ When the [=Source Mapping URL=] is not absolute, then it is relative to the gene
394
386
- If the generated code is being evaluated as a string with the `eval()` function or
395
387
via `new Function()`, then the [=source origin=] will be the page's origin.
396
388
389
+
### Linking through HTTP headers
390
+
391
+
If a file is served through HTTP(S) with a `sourcemap` header, the value of the header is
392
+
the URL of the linked source map.
393
+
394
+
```
395
+
sourcemap: <url>
396
+
```
397
+
398
+
Note: Previous revisions of this document recommended a header name of `x-sourcemap`. This
399
+
is now deprecated; `sourcemap` is now expected.
400
+
401
+
### Linking through inline annotations
402
+
403
+
The generated code should include a comment, or the equivalent construct depending on its
404
+
language or format, named `sourceMappingURL` and that contains the URL of the source map. This
405
+
specification defines how the comment should look like for JavaScript, CSS, and WebAssembly.
406
+
Other languages should follow a similar convention.
407
+
408
+
For a given language there can be multiple ways of detecting the `sourceMappingURL` comment,
409
+
to allow for different implementations to choose what is less complex for them. The generated
410
+
code <dfn>unambiguously links to a source map</dfn> if the result of all the extraction methods
411
+
is the same.
412
+
413
+
If a tool consumes one or more source files that [=unambiguously links to a source map=] and it
414
+
produces an output file that links to a source map, it must do so [=unambiguously links to a
415
+
source map|unambiguously=].
416
+
417
+
<div class="example">
418
+
The following JavaScript code links to a source map, but it does not do so [=unambiguously links
419
+
to a source map|unambiguously=]:
420
+
421
+
```js
422
+
let a = `
423
+
//# sourceMappingURL=foo.js.map
424
+
//`;
425
+
```
426
+
427
+
Extracing a Source Map URL from it [=extract a Source Map URL from JavaScript through
428
+
parsing|through parsing=] gives null, while [=extract a Source Map URL from JavaScript
429
+
without parsing|without parsing=] gives `foo.js.map`.
430
+
431
+
</div>
432
+
433
+
#### Extraction methods for JavaScript sources
434
+
435
+
To <dfn export>extract a Source Map URL from JavaScript through parsing</dfn> a [=string=] |source|,
436
+
run the following steps:
437
+
438
+
1. Let |tokens| be the [=list=] of [=tokens=]
439
+
obtained by parsing |source| according to [[ECMA-262]].
440
+
1. [=For each=] |token| in |tokens|, in reverse order:
441
+
1. If |token| is not a [=single-line comment=] or a [=multi-line comment=], return null.
442
+
1. Let |comment| be the content of |token|.
443
+
1. If [=match a Source Map URL in a comment|matching a Source Map URL in=]
444
+
|comment| returns a [=string=], return it.
445
+
446
+
To <dfn export>extract a Source Map URL from JavaScript without parsing</dfn> a [=string=] |source|,
447
+
run the following steps:
448
+
449
+
1. Let |lines| be the result of [=strictly split|strictly splitting=] |source| on [=line
450
+
terminator code points|ECMAScript line terminator code points=].
451
+
1. Let |lastURL| be null.
452
+
1. [=For each=] |line| in |lines|:
453
+
1. Let |position| be a [=position variable=] for |line|, initially pointing at the start of |line|.
454
+
1. [=While=] |position| doesn't point past the end of |line|:
455
+
1. [=Collect a sequence of code points=] that are [=white space code points|ECMAScript
456
+
white space code points=] from |line| given |position|.
457
+
458
+
NOTE: The collected code points are not used, but |position| is still updated.
459
+
1. If |position| points past the end of |line|, [=break=].
460
+
1. Let |first| be the [=code point=] of |line| at |position|.
461
+
1. Increment |position| by 1.
462
+
1. If |first| is U+002F (/) and |position| does not point past the end of |line|, then:
463
+
1. Let |second| be the [=code point=] of |line| at |position|.
464
+
1. Increment |position| by 1.
465
+
1. If |second| is U+002F (/), then:
466
+
1. Let |comment| be the [=code point substring=] from |position| to the end of |line|.
467
+
1. If [=match a Source Map URL in a comment|matching a Source Map URL in=]
468
+
|comment| returns a [=string=], set |lastURL| to it.
469
+
1. [=Break=].
470
+
1. Else if |second| is U+002A (*), then:
471
+
1. Let |comment| be the empty [=string=].
472
+
1. While |position| + 1 doesn't point past the end of |line|:
473
+
1. Let |c1| be the [=code point=] of |line| at |position|.
474
+
1. Increment |position| by 1.
475
+
1. Let |c2| be the [=code point=] of |line| at |position|.
476
+
1. If |c1| is U+002A (*) and |c2| is U+002F (/), then:
477
+
1. If [=match a Source Map URL in a comment|matching a Source Map URL in=]
478
+
|comment| returns a [=string=], set |lastURL| to it.
479
+
1. Increment |position| by 1.
480
+
1. Append |c1| to |comment|.
481
+
1. Else, set |lastURL| to null.
482
+
1. Else, set |lastURL| to null.
483
+
484
+
Note: We reset |lastURL| to null whenever we find a non-comment code character.
485
+
1. Return |lastURL|.
486
+
487
+
NOTE: The algorithm above has been designed so that the source lines can be iterated in reverse order,
488
+
returning early after scanning through a line that contains a `sourceMappingURL` comment.
489
+
490
+
<div class="note">
491
+
<span class="marker">Note:</span> The algorithm above is equivalent to the following JavaScript implementation:
492
+
493
+
```js
494
+
const JS_NEWLINE = /^/m;
495
+
496
+
// This RegExp will always match one of the following:
497
+
// - single-line comments
498
+
// - "single-line" multi-line comments
499
+
// - unclosed multi-line comments
500
+
// - just trailing whitespaces
501
+
// - a code character
502
+
// The loop below differentiates between all these cases.
0 commit comments