From 38f4ce48552a7c46f69633a937f6c9e6482308ee Mon Sep 17 00:00:00 2001 From: Milan Hauth Date: Thu, 24 Sep 2020 18:47:51 +0200 Subject: [PATCH] fix decode, dont fix missing map.sources --- src/compiler/preprocess/index.ts | 14 +++----- test/sourcemaps/index.js | 23 ++++++------- .../samples/sourcemap-names/test.js | 1 + .../samples/sourcemap-sources/_config.js | 33 +++++++++++-------- 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index ac7738f16f..b4e956aab5 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -103,7 +103,8 @@ function get_replacement( let processed_map_shifted; if (processed.map) { const decoded_map = typeof processed.map === "string" ? JSON.parse(processed.map) : processed.map; - decoded_map.mappings = sourcemap_decode(decoded_map.mappings); + if (typeof(decoded_map.mappings) === 'string') + decoded_map.mappings = sourcemap_decode(decoded_map.mappings); const processed_offset = get_location(offset + prefix.length); processed_map_shifted = sourcemap_add_offset(decoded_map, processed_offset); } @@ -132,7 +133,7 @@ export default async function preprocess( // sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1) // so we use sourcemap_list.unshift() to add new maps // https://github.com/ampproject/remapping#multiple-transformations-of-a-file - let sourcemap_list: Array = []; + const sourcemap_list: Array = []; for (const fn of markup) { @@ -215,14 +216,7 @@ export default async function preprocess( // remapper can throw error // `Transformation map ${i} must have exactly one source file.` - sourcemap_list = sourcemap_list - .map(sourcemap => { - if ((sourcemap as any).sources.filter(Boolean).length == 0) - // fix missing source file - (sourcemap as any).sources = [filename]; - return sourcemap; - }); - + // for 0 <= i <= (sourcemap_list.length - 2) const map: ReturnType = sourcemap_list.length == 0 ? null diff --git a/test/sourcemaps/index.js b/test/sourcemaps/index.js index b823abb8ec..7a2bf60982 100644 --- a/test/sourcemaps/index.js +++ b/test/sourcemaps/index.js @@ -22,8 +22,10 @@ describe("sourcemaps", () => { } (solo ? it.only : skip ? it.skip : it)(dir, async () => { + const { test } = require(`./samples/${dir}/test.js`); const inputFile = path.resolve(`${__dirname}/samples/${dir}/input.svelte`); - const outputBase = path.resolve(`${__dirname}/samples/${dir}/_actual`); + const outputName = '_actual'; + const outputBase = path.resolve(`${__dirname}/samples/${dir}/${outputName}`); const input = {}; input.code = fs.readFileSync(inputFile, "utf-8"); @@ -37,11 +39,9 @@ describe("sourcemaps", () => { filename: "input.svelte" }); } catch (error) { - preprocessed = { - error, - code: '', - map: null - }; + preprocessed = { error }; + // run test without js, css + return test({ assert, input, preprocessed }); } const { js, css } = svelte.compile( @@ -49,8 +49,8 @@ describe("sourcemaps", () => { filename: "input.svelte", sourcemap: preprocessed.map, // filenames for sourcemaps - outputFilename: "output.js", - cssOutputFilename: "output.css", + outputFilename: `${outputName}.js`, + cssOutputFilename: `${outputName}.css`, }); js.code = js.code.replace( @@ -64,7 +64,7 @@ describe("sourcemaps", () => { } fs.writeFileSync( `${outputBase}.js`, - `${js.code}\n//# sourceMappingURL=output.js.map` + `${js.code}\n//# sourceMappingURL=${outputName}.js.map` ); fs.writeFileSync( `${outputBase}.js.map`, @@ -73,7 +73,7 @@ describe("sourcemaps", () => { if (css.code) { fs.writeFileSync( `${outputBase}.css`, - `${css.code}\n/*# sourceMappingURL=output.css.map */` + `${css.code}\n/*# sourceMappingURL=${outputName}.css.map */` ); fs.writeFileSync( `${outputBase}.css.map`, @@ -84,8 +84,6 @@ describe("sourcemaps", () => { assert.deepEqual(js.map.sources, ["input.svelte"]); if (css.map) assert.deepEqual(css.map.sources, ["input.svelte"]); - const { test } = require(`./samples/${dir}/test.js`); - preprocessed.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map); preprocessed.locate = getLocator(preprocessed.code); @@ -96,7 +94,6 @@ describe("sourcemaps", () => { css.locate = getLocator(css.code || ""); test({ assert, input, preprocessed, js, css }); - }); }); }); diff --git a/test/sourcemaps/samples/sourcemap-names/test.js b/test/sourcemaps/samples/sourcemap-names/test.js index 21cb4410e6..85f4b1afdb 100644 --- a/test/sourcemaps/samples/sourcemap-names/test.js +++ b/test/sourcemaps/samples/sourcemap-names/test.js @@ -8,6 +8,7 @@ export function test({ assert, input, preprocessed, js, css }) { ['baritone', '--bazitone', 'old_name_1', 'old_name_2'].sort() ); + // TODO move fn test_name to test/sourcemaps/index.js and use in samples/*/test.js function test_name(old_name, new_name, where) { let loc = { character: -1 }; diff --git a/test/sourcemaps/samples/sourcemap-sources/_config.js b/test/sourcemaps/samples/sourcemap-sources/_config.js index df46e1348e..4fb923e1fc 100644 --- a/test/sourcemaps/samples/sourcemap-sources/_config.js +++ b/test/sourcemaps/samples/sourcemap-sources/_config.js @@ -1,21 +1,26 @@ import MagicString from 'magic-string'; +// eslint warning: import/no-named-as-default-member + +//import MagicString, { Bundle } from 'magic-string'; +// TODO why does this break mocha? +// SyntaxError: Cannot use import statement outside a module function add(bundle, filename, source) { - bundle.addSource({ - filename: filename, - content: new MagicString(source) - }); + bundle.addSource({ + filename, + content: new MagicString(source) + }); } function result(bundle, filename) { - return { - code: bundle.toString(), - map: bundle.generateMap({ - file: filename, - includeContent: true, - hires: true - }) - }; + return { + code: bundle.toString(), + map: bundle.generateMap({ + file: filename, + includeContent: true, + hires: true + }) + }; } export default { @@ -28,7 +33,7 @@ export default { add(bundle, 'foo.js', 'var answer = 42;'); add(bundle, 'bar.js', 'console.log(answer);'); - return result(bundle, filename); + return result(bundle, filename); } }, { @@ -39,7 +44,7 @@ export default { add(bundle, 'foo2.js', 'var answer2 = 84;'); add(bundle, 'bar2.js', 'console.log(answer2);'); - return result(bundle, filename); + return result(bundle, filename); } } ]