Skip to content
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

add source map support for preprocessors #5015

Closed

Conversation

halfnelson
Copy link
Contributor

@halfnelson halfnelson commented Jun 14, 2020

What
Adds support for the handling of source maps generated by preprocessors.

Why
Continuing the ongoing effort for a solid Typescript story for Svelte, The Typescript pre-processor and IDE support are good enough for prime time. The main letdown is the debug story. Svelte component source maps ignore the preprocessing that takes place making it impossible to do meaningful debugging via the web dev tools.

How
We use https://github.com/ampproject/remapping a small and synchronous source map combiner (Based on feedback on #1863)

Some manual source map merging is done in the context of Script and Style preprocessor results using a small set of utility functions developed for that purpose.

Sample level tests have been written to prove the flow.

Next Steps
The bundler plugins/loaders will need to be made aware of the sourcemap compiler option and pass in any preprocessor results.

@antony
Copy link
Member

antony commented Jul 11, 2020

@halfnelson does this PR supersede #1863 ?

@halfnelson
Copy link
Contributor Author

@antony yep, it attempts to meet Rich's criteria "it'd be great if we could avoid using source-map"

@halfnelson
Copy link
Contributor Author

The downside is it isn't as clean as RedHatters awesome contribution, so happy for suggestions. I tend to stop at the "it works now" stage

@benmccann
Copy link
Member

@halfnelson thanks for this! really excited about it. One thing I noticed from a quick peek at this PR is that a lot of the new code uses spaces. Could you convert it to tabs to match the existing code?

@@ -91,7 +92,8 @@
"source-map-support": "^0.5.13",
"tiny-glob": "^0.2.6",
"tslib": "^1.10.0",
"typescript": "^3.5.3"
"typescript": "^3.5.3",
"sourcemap-codec": "^1.4.8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this up a few lines so that it's in alphabetical order?

@@ -324,6 +325,35 @@ export default class Component {
js.map.sourcesContent = [
this.source
];

if (compile_options.sourceMap) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably call this option sourcemap (all lowercase) in order to match the rollup option name. I think it'd be easier to remember what to set if they're the same

let out: GeneratedStringWithMap;
let last_end = 0;
for (const { offset, length, replacement } of replacements)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably be on the previous line for consistency with the existing code

if (this.generated.length == 0) return other;
if (other.generated.length == 0) return this;

//combine sources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a mix here with there sometimes being a space after // and sometimes not. I'd probably always include a space for consistency

@milahu
Copy link
Contributor

milahu commented Sep 5, 2020

@halfnelson said

I tend to stop at the "it works now" stage

thanks for the patch! lets get this done ....

shamelessly rebased to current master:
https://github.com/milahu/svelte/tree/feature/preprocessor-sourcemaps
(took a while until i found git pull --rebase)

conflict was: new version allows self-closing <script/> and <style/> tags

/<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi,
/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi,

made a mistake on rebase:
failed to use the new <style> regex, fixed in commit 'prettify code 3'
assuming the commits are squashed

applied all suggestions by @benmccann

further prettified the code (except tests), added comments, .... see commit messages

all tests pass

should i make a new PR?

@milahu
Copy link
Contributor

milahu commented Sep 11, 2020

push - what can i do to get this merged?

@milahu
Copy link
Contributor

milahu commented Sep 18, 2020

dear maintainers - @benmccann @Conduitry etc

what are you waiting for?
the request author @halfnelson has obviously lost interest in the issue

i fixed all suggestions by @benmccann in my fork
should i make a new PR?

@antony
Copy link
Member

antony commented Sep 18, 2020

@milahu we're not waiting for anything. We're extremely busy, using the little free time we have at the moment to work on Sapper.

Please open a new PR, mention this one and we will review it in due course.

Copy link
Contributor

@milahu milahu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +118 to +125
//shift the first line of the second mapping by the number of columns in the last line of the first
const end = get_end_location(this.generated);
const col_offset = end.column + 1;
const first_line = other_mappings.length == 0 ? [] : other_mappings[0].map(seg => {
const new_seg = seg.slice() as MappingSegment;
new_seg[0] = seg[0] + col_offset;
return new_seg;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micro optimization:
we can skip column-shifting if last line of first part is empty
= first_source_str.slice(-1) == '\n'

what value has end.column in that case?
i guess its not -1 but rather line_length - 1
so the old column-shifting would be wrong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
end.column is zero if last line is empty (pass)
added micro optimization - skip colum shifting if col_offset is zero

return map;
}

async function replace_async(str: string, re: RegExp, func: (...any) => Promise<GeneratedStringWithMap>): Promise<GeneratedStringWithMap> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fn replace_async was moved into fn preprocess to use the global variable filename.
should we keep this? or change the fn signature of replace_async
to reduce line diffs of patch and to get a pure fn (clean interface)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done
moved fn replace_async out of fn preprocess, added filename and get_location to fn signature

@halfnelson
Copy link
Contributor Author

This lives on in #5428

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants