-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Feature/sourcemap preprocessor support #5584
Feature/sourcemap preprocessor support #5584
Conversation
Co-authored-by: Milan Hauth <[email protected]>
Co-authored-by: Milan Hauth <[email protected]>
Co-authored-by: Ben McCann <[email protected]>
abf858a
to
7138271
Compare
@benmccann @dummdidumm I took a stab at a refactor of https://github.com/halfnelson/svelte/blob/1217c5b24a3499db2ed302436050a82a4419b2f6/src/compiler/preprocess/index.ts |
nice one! line 132 processed_tags.sort((a,b) => a.tag.offset > b.tag.offset ? 1 : (a.tag.offset == b.tag.offset ? 0 : -1)); isnt that just processed_tags.sort((a,b) => a.tag.offset - b.tag.offset); to avoid temporary variables, the methods from_source, from_processed and concat could be merged const leading_content = StringWithSourcemap.from_source(filename, source.slice(last_end, tag.offset) + open_tag, locator(last_end));
const processed_content = StringWithSourcemap.from_processed(processed.code, processed.map, locator(tag.content_offset));
const trailing_content = StringWithSourcemap.from_source(filename, close_tag, locator(tag.content_offset + tag.content.length - 1));
out.concat(leading_content).concat(processed_content).concat(trailing_content); becomes this out.add_source(filename, source.slice(last_end, tag.offset) + open_tag, locator(last_end));
out.add_processed(processed, locator(tag.content_offset));
out.add_source(filename, close_tag, locator(tag.content_offset + tag.content.length - 1)); (etc)
target.applyPreprocessorOutput(await fn({
content: target.source,
filename: target.filename
})); could be less ugly, if we rename
|
Good point :)
I guess this is a personal preference. I don't mind the extra variables here as I feel that they help make the core action more readable
Strongly agree. I will change it.
I don't mind being explicit when crossing a responsibility/interface boundary.
ok
Good point, that will allow JS to collect the target object. Thanks for solid review! |
updated and ninja edited the original link |
yeah, it's a good change, but I think it probably is better to separate into two PRs to keep the review manageable |
Tidy code for consistency
At long last, this has been released in 3.30.0 🎉 Thank you to the several people involved in the literal years since the original PR! |
sweet : ) if someone wants to optimize this even more: currently, the libraries code-red and remapping this encode + decode steps are unnecessary and expensive the technical side of to problem is solved (patches are made) to optimize the merging of sourcemaps, see |
Any improvements would be great! I'm happy to help review those and get them in Btw, @dummdidumm reported the line numbers might be off by one in his tests. I haven't gotten time to really test much yet. I have a PR to add the feature to |
a possible external cause: svelte/test/sourcemaps/index.ts Lines 98 to 103 in 505eba8
possible test bug: |
test bug the function magic_string_preprocessor_result sample solution export function preprocessor_result (
filename: string,
src: MagicString,
map_options: any
) {
map_options = Object.assign({
// default options
source: filename,
hires: true,
includeContent: false
}, map_options || {});
const get_map = map_options.skipEncode ? src.generateDecodedMap : src.generateMap;
delete map_options.skipEncode;
return {
code: src.toString(),
map: get_map(map_options)
};
}
// sample use
return preprocessor_result(filename, src, { skipEncode: true }); (also, by convention in object-oriented-programming, the object always comes as first parameter, but thats "only" aesthetics) (to make tests faster, result() should return decoded mappings by default, and only encode mappings when |
Co-authored-by: Milan Hauth <[email protected]> Co-authored-by: Ben McCann <[email protected]>
It is now compatible with both version before and after 3.30.0 sveltejs/svelte#5584
What
Adds support for the handling of source maps generated by preprocessors.
This is a rebase of parts of #5428 (by @milahu) to make it easier to review.
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 (e.g. sveltejs/rollup-plugin-svelte#140)
Apply the developer mode warnings and encode/decode optimizations from #5428