Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: parse attached sourcemap from preprocessor #5854

Merged
merged 7 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
import { decode as decode_mappings } from 'sourcemap-codec';
import { getLocator } from 'locate-character';
import { StringWithSourcemap, sourcemap_add_offset, combine_sourcemaps } from '../utils/string_with_sourcemap';
import {
StringWithSourcemap,
sourcemap_add_offset,
combine_sourcemaps,
parse_attached_sourcemap
} from '../utils/string_with_sourcemap';

export interface Processed {
code: string;
Expand Down Expand Up @@ -170,7 +175,8 @@ function get_replacement(
original: string,
processed: Processed,
prefix: string,
suffix: string
suffix: string,
tag_name: 'script' | 'style'
): StringWithSourcemap {

// Convert the unchanged prefix and suffix to StringWithSourcemap
Expand All @@ -179,6 +185,8 @@ function get_replacement(
const suffix_with_map = StringWithSourcemap.from_source(
file_basename, suffix, get_location(offset + prefix.length + original.length));

parse_attached_sourcemap(processed, tag_name);

// Convert the preprocessed code and its sourcemap to a StringWithSourcemap
let decoded_map: DecodedSourceMap;
if (processed.map) {
Expand Down Expand Up @@ -282,7 +290,7 @@ export default async function preprocess(
if (!processed || !processed.map && processed.code === content) {
return no_change();
}
return get_replacement(file_basename, offset, get_location, content, processed, `<${tag_name}${attributes}>`, `</${tag_name}>`);
return get_replacement(file_basename, offset, get_location, content, processed, `<${tag_name}${attributes}>`, `</${tag_name}>`, tag_name);
}
);
source = res.string;
Expand Down
24 changes: 24 additions & 0 deletions src/compiler/utils/string_with_sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DecodedSourceMap, RawSourceMap, SourceMapLoader } from '@ampproject/remapping/dist/types/types';
import remapping from '@ampproject/remapping';
import { SourceMap } from 'magic-string';
import { Processed } from '../preprocess';

type SourceLocation = {
line: number;
Expand Down Expand Up @@ -255,6 +256,7 @@ export function combine_sourcemaps(

// browser vs node.js
const b64enc = typeof btoa == 'function' ? btoa : b => Buffer.from(b).toString('base64');
const b64dec = typeof atob == 'function' ? atob : a => Buffer.from(a, 'base64').toString();

export function apply_preprocessor_sourcemap(filename: string, svelte_map: SourceMap, preprocessor_map_input: string | DecodedSourceMap | RawSourceMap): SourceMap {
if (!svelte_map || !preprocessor_map_input) return svelte_map;
Expand Down Expand Up @@ -288,3 +290,25 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc

return result_map as SourceMap;
}

// parse attached sourcemap in processed.code
export function parse_attached_sourcemap(processed: Processed, tag_name: 'script' | 'style'): void {
const magic_prefix = '# sourceMappingURL=data:application/json;';
const cut_index = processed.code.lastIndexOf('\n');
benmccann marked this conversation as resolved.
Show resolved Hide resolved
const last_line = processed.code.slice(cut_index);
const line_start = last_line.slice(1, 3); // last_line[0] == '\n'
if ((line_start != '/*' && (tag_name == 'script' && line_start != '//')) ||
magic_prefix != last_line.slice(3, 3 + magic_prefix.length)
) return; // attachment not found
if (processed.map) {
throw 'not implemented. '+
'found sourcemap in both processed.code and processed.map. '+
'please pass only one sourcemap.\n'+
'processed.code:\n'+
processed.code.slice(0, 100)+' [....]'; // help to find preprocessor
}
processed.code = processed.code.slice(0, cut_index); // remove last line
const slice_to = (line_start == '/*') ? -2 : undefined;
const b64map = last_line.slice(last_line.indexOf('base64,')+7, slice_to).trim();
processed.map = b64dec(b64map);
}
11 changes: 7 additions & 4 deletions test/sourcemaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('sourcemaps', () => {
const inputCode = fs.readFileSync(inputFile, 'utf-8');
const input = {
code: inputCode,
locate: getLocator(inputCode)
locate: getLocator(inputCode),
locate_1: getLocator(inputCode, { offsetLine: 1 })
milahu marked this conversation as resolved.
Show resolved Hide resolved
};

const preprocessed = await svelte.preprocess(
Expand All @@ -41,7 +42,7 @@ describe('sourcemaps', () => {
filename: 'input.svelte'
}
);

const { js, css } = svelte.compile(
preprocessed.code, {
filename: 'input.svelte',
Expand Down Expand Up @@ -86,12 +87,14 @@ describe('sourcemaps', () => {

assert.deepEqual(
js.map.sources.slice().sort(),
(config.js_map_sources || ['input.svelte']).sort()
(config.js_map_sources || ['input.svelte']).sort(),
'js.map.sources is wrong'
);
if (css.map) {
assert.deepEqual(
css.map.sources.slice().sort(),
(config.css_map_sources || ['input.svelte']).sort()
(config.css_map_sources || ['input.svelte']).sort(),
'css.map.sources is wrong'
);
}

Expand Down
44 changes: 44 additions & 0 deletions test/sourcemaps/samples/attached-sourcemap/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import MagicString from 'magic-string';

let indent_size = 4;
let comment_multi = true;
function get_processor(tag_name, search, replace) {
return {
[tag_name]: ({ content, filename }) => {
let code = content.slice();
const ms = new MagicString(code);

const idx = ms.original.indexOf(search);
if (idx == -1) throw new Error('search not found in src');
ms.overwrite(idx, idx + search.length, replace, { storeName: true });

// change line + column
const indent = Array.from({ length: indent_size }).join(' ');
ms.prependLeft(idx, '\n'+indent);

const map_opts = { source: filename, hires: true, includeContent: false };
const map = ms.generateMap(map_opts);
const attach_line = (tag_name == 'style' || comment_multi)
? `\n/*# sourceMappingURL=${map.toUrl()} */`
: `\n//# sourceMappingURL=${map.toUrl()}` // only in script
;
code = ms.toString() + attach_line;

indent_size += 2;
if (tag_name == 'script') comment_multi = !comment_multi;
return { code };
}
};
}

export default {
preprocess: [

get_processor('script', 'replace_me_script', 'done_replace_script_1'),
get_processor('script', 'done_replace_script_1', 'done_replace_script_2'),

get_processor('style', '.replace_me_style', '.done_replace_style_1'),
get_processor('style', '.done_replace_style_1', '.done_replace_style_2')

]
};
11 changes: 11 additions & 0 deletions test/sourcemaps/samples/attached-sourcemap/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<style>
.replace_me_style {
color: red;
}
</style>
<script>
let
replace_me_script = 'hello'
;
</script>
<h1 class="done_replace_style_2">{done_replace_script_2}</h1>
45 changes: 45 additions & 0 deletions test/sourcemaps/samples/attached-sourcemap/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as assert from 'assert';

const get_line_column = obj => ({ line: obj.line, column: obj.column });

export function test({ input, css, js }) {

let out_obj, loc_output, actual, loc_input, expected;

out_obj = js;
// we need the second occurence of 'done_replace_script_2' in output.js
// the first occurence is mapped back to markup '{done_replace_script_2}'
loc_output = out_obj.locate_1('done_replace_script_2');
loc_output = out_obj.locate_1('done_replace_script_2', loc_output.character + 1);
actual = out_obj.mapConsumer.originalPositionFor(loc_output);
loc_input = input.locate_1('replace_me_script');
expected = {
source: 'input.svelte',
name: 'replace_me_script',
...get_line_column(loc_input)
};
assert.deepEqual(actual, expected);

out_obj = css;
loc_output = out_obj.locate_1('.done_replace_style_2');
actual = out_obj.mapConsumer.originalPositionFor(loc_output);
loc_input = input.locate_1('.replace_me_style');
expected = {
source: 'input.svelte',
name: '.replace_me_style',
...get_line_column(loc_input)
};
assert.deepEqual(actual, expected);

assert.equal(
js.code.indexOf('\n/*# sourceMappingURL=data:application/json;base64,'),
-1,
'magic-comment attachments were NOT removed'
);

assert.equal(
css.code.indexOf('\n/*# sourceMappingURL=data:application/json;base64,'),
-1,
'magic-comment attachments were NOT removed'
);
}