From 0b2b77cb1912845e7d2c2e5837d29d1f03dd5858 Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Sun, 3 Jan 2021 19:45:02 +0100 Subject: [PATCH 1/7] add: parse attached sourcemap from preprocessor --- src/compiler/preprocess/index.ts | 9 +++- src/compiler/utils/string_with_sourcemap.ts | 25 +++++++++++ test/sourcemaps/index.ts | 9 ++-- .../samples/attached-sourcemap/_config.js | 37 +++++++++++++++ .../samples/attached-sourcemap/input.svelte | 11 +++++ .../samples/attached-sourcemap/test.js | 45 +++++++++++++++++++ 6 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 test/sourcemaps/samples/attached-sourcemap/_config.js create mode 100644 test/sourcemaps/samples/attached-sourcemap/input.svelte create mode 100644 test/sourcemaps/samples/attached-sourcemap/test.js diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 264fbbdbcd42..660a327e2d3b 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -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; @@ -179,6 +184,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); + // Convert the preprocessed code and its sourcemap to a StringWithSourcemap let decoded_map: DecodedSourceMap; if (processed.map) { diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index 1766a0db069a..4a442b964a94 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -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; @@ -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; @@ -288,3 +290,26 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc return result_map as SourceMap; } + +// find attached sourcemap in processed.code +// TODO? handle multiple attached maps, combine with processed.map +export function parse_attached_sourcemap(processed: Processed): void { + const magic_prefix = '\n/*# sourceMappingURL=data:application/json;'; + const cut_index = processed.code.lastIndexOf('\n'); + const last_line = processed.code.slice(cut_index); + if (magic_prefix != last_line.slice(0, 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 + } + // remove last line + processed.code = processed.code.slice(0, cut_index); + // slice to -2 --> remove trailing '*/' + const b64map = last_line.slice(last_line.indexOf('base64,')+7, -2).trim(); + processed.map = b64dec(b64map); +} diff --git a/test/sourcemaps/index.ts b/test/sourcemaps/index.ts index 4122c3a41969..f00c2ced338b 100644 --- a/test/sourcemaps/index.ts +++ b/test/sourcemaps/index.ts @@ -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 }) }; const preprocessed = await svelte.preprocess( @@ -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' ); } diff --git a/test/sourcemaps/samples/attached-sourcemap/_config.js b/test/sourcemaps/samples/attached-sourcemap/_config.js new file mode 100644 index 000000000000..588ab7515cfa --- /dev/null +++ b/test/sourcemaps/samples/attached-sourcemap/_config.js @@ -0,0 +1,37 @@ +import MagicString from 'magic-string'; + +let indent_size = 4; +function get_processor(search, replace) { + return ({ 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 = `\n/*# sourceMappingURL=${map.toUrl()} */`; + code = ms.toString() + attach_line; + + indent_size += 2; + return { code }; + }; +} + +export default { + preprocess: [ + + { script: get_processor('replace_me_script', 'done_replace_script_1') }, + { script: get_processor('done_replace_script_1', 'done_replace_script_2') }, + + { style: get_processor('.replace_me_style', '.done_replace_style_1') }, + { style: get_processor('.done_replace_style_1', '.done_replace_style_2') } + + ] +}; diff --git a/test/sourcemaps/samples/attached-sourcemap/input.svelte b/test/sourcemaps/samples/attached-sourcemap/input.svelte new file mode 100644 index 000000000000..21a47a72a9c9 --- /dev/null +++ b/test/sourcemaps/samples/attached-sourcemap/input.svelte @@ -0,0 +1,11 @@ + + +

{done_replace_script_2}

diff --git a/test/sourcemaps/samples/attached-sourcemap/test.js b/test/sourcemaps/samples/attached-sourcemap/test.js new file mode 100644 index 000000000000..fde219080823 --- /dev/null +++ b/test/sourcemaps/samples/attached-sourcemap/test.js @@ -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 seconds occurence in output.js + 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); + // first occurence in input.svelte + 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' + ); +} From 9d17d7872979b9dc3be030d46c5ba020a0503b71 Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Mon, 4 Jan 2021 20:01:50 +0100 Subject: [PATCH 2/7] fix --- src/compiler/utils/string_with_sourcemap.ts | 3 +-- test/sourcemaps/samples/attached-sourcemap/test.js | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index 4a442b964a94..dc182a6321b2 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -291,8 +291,7 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc return result_map as SourceMap; } -// find attached sourcemap in processed.code -// TODO? handle multiple attached maps, combine with processed.map +// parse attached sourcemap in processed.code export function parse_attached_sourcemap(processed: Processed): void { const magic_prefix = '\n/*# sourceMappingURL=data:application/json;'; const cut_index = processed.code.lastIndexOf('\n'); diff --git a/test/sourcemaps/samples/attached-sourcemap/test.js b/test/sourcemaps/samples/attached-sourcemap/test.js index fde219080823..c5b4d7989dce 100644 --- a/test/sourcemaps/samples/attached-sourcemap/test.js +++ b/test/sourcemaps/samples/attached-sourcemap/test.js @@ -7,7 +7,8 @@ export function test({ input, css, js }) { let out_obj, loc_output, actual, loc_input, expected; out_obj = js; - // we need the seconds occurence in output.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); @@ -22,7 +23,6 @@ export function test({ input, css, js }) { out_obj = css; loc_output = out_obj.locate_1('.done_replace_style_2'); actual = out_obj.mapConsumer.originalPositionFor(loc_output); - // first occurence in input.svelte loc_input = input.locate_1('.replace_me_style'); expected = { source: 'input.svelte', From a5ae6b3b7e57fc68ebb408ec0cde105436685051 Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Mon, 4 Jan 2021 22:13:55 +0100 Subject: [PATCH 3/7] attached sourcemap: support single line comments in script --- src/compiler/preprocess/index.ts | 7 ++- src/compiler/utils/string_with_sourcemap.ts | 18 +++--- test/sourcemaps/index.ts | 2 +- .../samples/attached-sourcemap/_config.js | 55 +++++++++++-------- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 660a327e2d3b..e0847898d546 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -175,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 @@ -184,7 +185,7 @@ function get_replacement( const suffix_with_map = StringWithSourcemap.from_source( file_basename, suffix, get_location(offset + prefix.length + original.length)); - parse_attached_sourcemap(processed); + parse_attached_sourcemap(processed, tag_name); // Convert the preprocessed code and its sourcemap to a StringWithSourcemap let decoded_map: DecodedSourceMap; @@ -289,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}>`, ``); + return get_replacement(file_basename, offset, get_location, content, processed, `<${tag_name}${attributes}>`, ``, tag_name); } ); source = res.string; diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index dc182a6321b2..b0507aa2f847 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -292,13 +292,14 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc } // parse attached sourcemap in processed.code -export function parse_attached_sourcemap(processed: Processed): void { - const magic_prefix = '\n/*# sourceMappingURL=data:application/json;'; +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'); const last_line = processed.code.slice(cut_index); - if (magic_prefix != last_line.slice(0, magic_prefix.length)) { - return; // attachment not found - } + 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. '+ @@ -306,9 +307,8 @@ export function parse_attached_sourcemap(processed: Processed): void { 'processed.code:\n'+ processed.code.slice(0, 100)+' [....]'; // help to find preprocessor } - // remove last line - processed.code = processed.code.slice(0, cut_index); - // slice to -2 --> remove trailing '*/' - const b64map = last_line.slice(last_line.indexOf('base64,')+7, -2).trim(); + 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); } diff --git a/test/sourcemaps/index.ts b/test/sourcemaps/index.ts index f00c2ced338b..903629c06b1b 100644 --- a/test/sourcemaps/index.ts +++ b/test/sourcemaps/index.ts @@ -42,7 +42,7 @@ describe('sourcemaps', () => { filename: 'input.svelte' } ); - + const { js, css } = svelte.compile( preprocessed.code, { filename: 'input.svelte', diff --git a/test/sourcemaps/samples/attached-sourcemap/_config.js b/test/sourcemaps/samples/attached-sourcemap/_config.js index 588ab7515cfa..6d9786f6b068 100644 --- a/test/sourcemaps/samples/attached-sourcemap/_config.js +++ b/test/sourcemaps/samples/attached-sourcemap/_config.js @@ -1,37 +1,44 @@ import MagicString from 'magic-string'; let indent_size = 4; -function get_processor(search, replace) { - return ({ 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 = `\n/*# sourceMappingURL=${map.toUrl()} */`; - code = ms.toString() + attach_line; - - indent_size += 2; - return { code }; +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: [ - { script: get_processor('replace_me_script', 'done_replace_script_1') }, - { script: get_processor('done_replace_script_1', 'done_replace_script_2') }, + get_processor('script', 'replace_me_script', 'done_replace_script_1'), + get_processor('script', 'done_replace_script_1', 'done_replace_script_2'), - { style: get_processor('.replace_me_style', '.done_replace_style_1') }, - { style: get_processor('.done_replace_style_1', '.done_replace_style_2') } + get_processor('style', '.replace_me_style', '.done_replace_style_1'), + get_processor('style', '.done_replace_style_1', '.done_replace_style_2') ] }; From dcaeb768e4f996c7267404cceab760252bc35ae8 Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Wed, 6 Jan 2021 20:02:05 +0100 Subject: [PATCH 4/7] attached sourcemap: use regex --- src/compiler/utils/string_with_sourcemap.ts | 37 +++++++++++---------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index b0507aa2f847..d87868f939ec 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -293,22 +293,23 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc // 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'); - 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); + const r_in = '[#@]\\s*sourceMappingURL\\s*=\\s*data:' + + '(?:application|text)/json;(?:charset[:=]\\S+?;)?base64,(\\S*)'; + const regex = (tag_name == 'script') + ? new RegExp('(?://'+r_in+')|(?:/\\*'+r_in+'\\s*\\*/)$') + : new RegExp('/\\*'+r_in+'\\s*\\*/$'); + processed.code = processed.code.replace(regex, (_, match1, match2) => { + 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 + } + const map64 = (tag_name == 'script') + ? (match1 || match2) + : match1; + processed.map = b64dec(map64); + return ''; // remove from processed.code + }); } From 4a1450b46f1e8e74aad5dca0122c1a50a2db11a7 Mon Sep 17 00:00:00 2001 From: milahu Date: Mon, 18 Jan 2021 20:10:32 +0100 Subject: [PATCH 5/7] remove attached sourcemap path --- src/compiler/utils/string_with_sourcemap.ts | 36 ++++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index d87868f939ec..52c69b7b1082 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -293,23 +293,35 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc // parse attached sourcemap in processed.code export function parse_attached_sourcemap(processed: Processed, tag_name: 'script' | 'style'): void { - const r_in = '[#@]\\s*sourceMappingURL\\s*=\\s*data:' + - '(?:application|text)/json;(?:charset[:=]\\S+?;)?base64,(\\S*)'; + const r_in = '[#@]\\s*sourceMappingURL\\s*=\\s*(\\S*)'; const regex = (tag_name == 'script') ? new RegExp('(?://'+r_in+')|(?:/\\*'+r_in+'\\s*\\*/)$') : new RegExp('/\\*'+r_in+'\\s*\\*/$'); + function throw_error(message) { + throw 'error: ' + message + '\nprocessed.code:\n' + + (processed.code.length < 100 ? processed.code : processed.code.slice(0, 100)+' [...]'); // help to find preprocessor + } processed.code = processed.code.replace(regex, (_, match1, match2) => { - 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 + const map_url = (tag_name == 'script') ? (match1 || match2) : match1; + const map_data = (map_url.match(/data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(\S*)/) || [])[1]; + if (map_data) { + // sourceMappingURL is data URL + if (processed.map) { + throw_error('not implemented. '+ + 'found sourcemap in both processed.code and processed.map. '+ + 'please pass only one sourcemap.'); + } + processed.map = b64dec(map_data); // use attached sourcemap + return ''; // remove from processed.code + } + // sourceMappingURL is file path or URL + if (!processed.map) { + // TODO show warning? silently ignore? + throw_error('value error. processed.map is empty, '+ + 'but processed.code has attached sourcemap file '+map_url+'. '+ + 'please make your preprocessor return a sourcemap.'); } - const map64 = (tag_name == 'script') - ? (match1 || match2) - : match1; - processed.map = b64dec(map64); + // ignore sourcemap file path return ''; // remove from processed.code }); } From a29e2d0394f58c701a0d4e348af8cbb715e41d73 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 19 Jan 2021 09:32:52 +0100 Subject: [PATCH 6/7] fix: warnings, lint --- src/compiler/utils/string_with_sourcemap.ts | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index 52c69b7b1082..6cf14db5fc9f 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -297,9 +297,10 @@ export function parse_attached_sourcemap(processed: Processed, tag_name: 'script const regex = (tag_name == 'script') ? new RegExp('(?://'+r_in+')|(?:/\\*'+r_in+'\\s*\\*/)$') : new RegExp('/\\*'+r_in+'\\s*\\*/$'); - function throw_error(message) { - throw 'error: ' + message + '\nprocessed.code:\n' + - (processed.code.length < 100 ? processed.code : processed.code.slice(0, 100)+' [...]'); // help to find preprocessor + function log_warning(message) { + // code_start: help to find preprocessor + const code_start = processed.code.length < 100 ? processed.code : (processed.code.slice(0, 100) + ' [...]'); + console.warn(`warning: ${message}. processed.code = ${JSON.stringify(code_start)}`); } processed.code = processed.code.replace(regex, (_, match1, match2) => { const map_url = (tag_name == 'script') ? (match1 || match2) : match1; @@ -307,21 +308,20 @@ export function parse_attached_sourcemap(processed: Processed, tag_name: 'script if (map_data) { // sourceMappingURL is data URL if (processed.map) { - throw_error('not implemented. '+ - 'found sourcemap in both processed.code and processed.map. '+ - 'please pass only one sourcemap.'); + log_warning('not implemented. ' + + 'found sourcemap in both processed.code and processed.map. ' + + 'please make your preprocessor return only one sourcemap.'); } processed.map = b64dec(map_data); // use attached sourcemap return ''; // remove from processed.code } - // sourceMappingURL is file path or URL + // sourceMappingURL is path or URL if (!processed.map) { - // TODO show warning? silently ignore? - throw_error('value error. processed.map is empty, '+ - 'but processed.code has attached sourcemap file '+map_url+'. '+ + log_warning('value error. processed.map is empty, ' + + `but processed.code has attached sourcemap path ${JSON.stringify(map_url)}. ` + 'please make your preprocessor return a sourcemap.'); } - // ignore sourcemap file path + // ignore sourcemap path return ''; // remove from processed.code }); } From 2fe7c0e9a9d5b79b6c87f754899f8643f22d94f3 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 19 Jan 2021 18:33:40 +0100 Subject: [PATCH 7/7] fix warning, in doubt ignore attached sourcemap --- src/compiler/utils/string_with_sourcemap.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index 6cf14db5fc9f..3002e10d849d 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -308,18 +308,19 @@ export function parse_attached_sourcemap(processed: Processed, tag_name: 'script if (map_data) { // sourceMappingURL is data URL if (processed.map) { - log_warning('not implemented. ' + - 'found sourcemap in both processed.code and processed.map. ' + - 'please make your preprocessor return only one sourcemap.'); + log_warning('Not implemented. ' + + 'Found sourcemap in both processed.code and processed.map. ' + + 'Please update your preprocessor to return only one sourcemap.'); + // ignore attached sourcemap + return ''; } processed.map = b64dec(map_data); // use attached sourcemap return ''; // remove from processed.code } // sourceMappingURL is path or URL if (!processed.map) { - log_warning('value error. processed.map is empty, ' + - `but processed.code has attached sourcemap path ${JSON.stringify(map_url)}. ` + - 'please make your preprocessor return a sourcemap.'); + log_warning(`Found sourcemap path ${JSON.stringify(map_url)} in processed.code, but no sourcemap data. ` + + 'Please update your preprocessor to return sourcemap data directly.'); } // ignore sourcemap path return ''; // remove from processed.code