mirror of https://github.com/sveltejs/svelte
parent
9d17d78729
commit
a5ae6b3b7e
@ -1,37 +1,44 @@
|
|||||||
import MagicString from 'magic-string';
|
import MagicString from 'magic-string';
|
||||||
|
|
||||||
let indent_size = 4;
|
let indent_size = 4;
|
||||||
function get_processor(search, replace) {
|
let comment_multi = true;
|
||||||
return ({ content, filename }) => {
|
function get_processor(tag_name, search, replace) {
|
||||||
let code = content.slice();
|
return {
|
||||||
const ms = new MagicString(code);
|
[tag_name]: ({ content, filename }) => {
|
||||||
|
let code = content.slice();
|
||||||
const idx = ms.original.indexOf(search);
|
const ms = new MagicString(code);
|
||||||
if (idx == -1) throw new Error('search not found in src');
|
|
||||||
ms.overwrite(idx, idx + search.length, replace, { storeName: true });
|
const idx = ms.original.indexOf(search);
|
||||||
|
if (idx == -1) throw new Error('search not found in src');
|
||||||
// change line + column
|
ms.overwrite(idx, idx + search.length, replace, { storeName: true });
|
||||||
const indent = Array.from({ length: indent_size }).join(' ');
|
|
||||||
ms.prependLeft(idx, '\n'+indent);
|
// change line + column
|
||||||
|
const indent = Array.from({ length: indent_size }).join(' ');
|
||||||
const map_opts = { source: filename, hires: true, includeContent: false };
|
ms.prependLeft(idx, '\n'+indent);
|
||||||
const map = ms.generateMap(map_opts);
|
|
||||||
const attach_line = `\n/*# sourceMappingURL=${map.toUrl()} */`;
|
const map_opts = { source: filename, hires: true, includeContent: false };
|
||||||
code = ms.toString() + attach_line;
|
const map = ms.generateMap(map_opts);
|
||||||
|
const attach_line = (tag_name == 'style' || comment_multi)
|
||||||
indent_size += 2;
|
? `\n/*# sourceMappingURL=${map.toUrl()} */`
|
||||||
return { code };
|
: `\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 {
|
export default {
|
||||||
preprocess: [
|
preprocess: [
|
||||||
|
|
||||||
{ script: get_processor('replace_me_script', 'done_replace_script_1') },
|
get_processor('script', 'replace_me_script', 'done_replace_script_1'),
|
||||||
{ script: get_processor('done_replace_script_1', 'done_replace_script_2') },
|
get_processor('script', 'done_replace_script_1', 'done_replace_script_2'),
|
||||||
|
|
||||||
{ style: get_processor('.replace_me_style', '.done_replace_style_1') },
|
get_processor('style', '.replace_me_style', '.done_replace_style_1'),
|
||||||
{ style: get_processor('.done_replace_style_1', '.done_replace_style_2') }
|
get_processor('style', '.done_replace_style_1', '.done_replace_style_2')
|
||||||
|
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in new issue