-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
add: parse attached sourcemap from preprocessor #5854
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0b2b77c
add: parse attached sourcemap from preprocessor
milahu 9d17d78
fix
milahu a5ae6b3
attached sourcemap: support single line comments in script
milahu dcaeb76
attached sourcemap: use regex
milahu 4a1450b
remove attached sourcemap path
milahu a29e2d0
fix: warnings, lint
milahu 2fe7c0e
fix warning, in doubt ignore attached sourcemap
milahu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
||
] | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.