fix decode, dont fix missing map.sources

pull/5428/head
Milan Hauth 5 years ago
parent cf600f7a9f
commit 38f4ce4855

@ -103,7 +103,8 @@ function get_replacement(
let processed_map_shifted; let processed_map_shifted;
if (processed.map) { if (processed.map) {
const decoded_map = typeof processed.map === "string" ? JSON.parse(processed.map) : 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); const processed_offset = get_location(offset + prefix.length);
processed_map_shifted = sourcemap_add_offset(decoded_map, processed_offset); 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) // 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 // so we use sourcemap_list.unshift() to add new maps
// https://github.com/ampproject/remapping#multiple-transformations-of-a-file // https://github.com/ampproject/remapping#multiple-transformations-of-a-file
let sourcemap_list: Array<Processed['map']> = []; const sourcemap_list: Array<Processed['map']> = [];
for (const fn of markup) { for (const fn of markup) {
@ -215,14 +216,7 @@ export default async function preprocess(
// remapper can throw error // remapper can throw error
// `Transformation map ${i} must have exactly one source file.` // `Transformation map ${i} must have exactly one source file.`
sourcemap_list = sourcemap_list // for 0 <= i <= (sourcemap_list.length - 2)
.map(sourcemap => {
if ((sourcemap as any).sources.filter(Boolean).length == 0)
// fix missing source file
(sourcemap as any).sources = [filename];
return sourcemap;
});
const map: ReturnType<typeof remapper> = const map: ReturnType<typeof remapper> =
sourcemap_list.length == 0 sourcemap_list.length == 0
? null ? null

@ -22,8 +22,10 @@ describe("sourcemaps", () => {
} }
(solo ? it.only : skip ? it.skip : it)(dir, async () => { (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 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 = {}; const input = {};
input.code = fs.readFileSync(inputFile, "utf-8"); input.code = fs.readFileSync(inputFile, "utf-8");
@ -37,11 +39,9 @@ describe("sourcemaps", () => {
filename: "input.svelte" filename: "input.svelte"
}); });
} catch (error) { } catch (error) {
preprocessed = { preprocessed = { error };
error, // run test without js, css
code: '', return test({ assert, input, preprocessed });
map: null
};
} }
const { js, css } = svelte.compile( const { js, css } = svelte.compile(
@ -49,8 +49,8 @@ describe("sourcemaps", () => {
filename: "input.svelte", filename: "input.svelte",
sourcemap: preprocessed.map, sourcemap: preprocessed.map,
// filenames for sourcemaps // filenames for sourcemaps
outputFilename: "output.js", outputFilename: `${outputName}.js`,
cssOutputFilename: "output.css", cssOutputFilename: `${outputName}.css`,
}); });
js.code = js.code.replace( js.code = js.code.replace(
@ -64,7 +64,7 @@ describe("sourcemaps", () => {
} }
fs.writeFileSync( fs.writeFileSync(
`${outputBase}.js`, `${outputBase}.js`,
`${js.code}\n//# sourceMappingURL=output.js.map` `${js.code}\n//# sourceMappingURL=${outputName}.js.map`
); );
fs.writeFileSync( fs.writeFileSync(
`${outputBase}.js.map`, `${outputBase}.js.map`,
@ -73,7 +73,7 @@ describe("sourcemaps", () => {
if (css.code) { if (css.code) {
fs.writeFileSync( fs.writeFileSync(
`${outputBase}.css`, `${outputBase}.css`,
`${css.code}\n/*# sourceMappingURL=output.css.map */` `${css.code}\n/*# sourceMappingURL=${outputName}.css.map */`
); );
fs.writeFileSync( fs.writeFileSync(
`${outputBase}.css.map`, `${outputBase}.css.map`,
@ -84,8 +84,6 @@ describe("sourcemaps", () => {
assert.deepEqual(js.map.sources, ["input.svelte"]); assert.deepEqual(js.map.sources, ["input.svelte"]);
if (css.map) assert.deepEqual(css.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.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map);
preprocessed.locate = getLocator(preprocessed.code); preprocessed.locate = getLocator(preprocessed.code);
@ -96,7 +94,6 @@ describe("sourcemaps", () => {
css.locate = getLocator(css.code || ""); css.locate = getLocator(css.code || "");
test({ assert, input, preprocessed, js, css }); test({ assert, input, preprocessed, js, css });
}); });
}); });
}); });

@ -8,6 +8,7 @@ export function test({ assert, input, preprocessed, js, css }) {
['baritone', '--bazitone', 'old_name_1', 'old_name_2'].sort() ['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) { function test_name(old_name, new_name, where) {
let loc = { character: -1 }; let loc = { character: -1 };

@ -1,21 +1,26 @@
import MagicString from 'magic-string'; 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) { function add(bundle, filename, source) {
bundle.addSource({ bundle.addSource({
filename: filename, filename,
content: new MagicString(source) content: new MagicString(source)
}); });
} }
function result(bundle, filename) { function result(bundle, filename) {
return { return {
code: bundle.toString(), code: bundle.toString(),
map: bundle.generateMap({ map: bundle.generateMap({
file: filename, file: filename,
includeContent: true, includeContent: true,
hires: true hires: true
}) })
}; };
} }
export default { export default {
@ -28,7 +33,7 @@ export default {
add(bundle, 'foo.js', 'var answer = 42;'); add(bundle, 'foo.js', 'var answer = 42;');
add(bundle, 'bar.js', 'console.log(answer);'); 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, 'foo2.js', 'var answer2 = 84;');
add(bundle, 'bar2.js', 'console.log(answer2);'); add(bundle, 'bar2.js', 'console.log(answer2);');
return result(bundle, filename); return result(bundle, filename);
} }
} }
] ]

Loading…
Cancel
Save