-
Notifications
You must be signed in to change notification settings - Fork 150
WIP: Improve TypeScript imports preprocessing (#318) #342
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
Conversation
}): string { | ||
if (!markup) return content; | ||
|
||
const { vars } = compile(stripTags(markup), { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the Svelte compiler will throw errors on unknown properties, this needs to be surrounded with a try-catch for backwards-compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm correct, it will depend on the release of svelte
. If the 3 PRs are merged and released in the same time, there will be no problem since the markup
will not be passed in previous versions. Thus, it will not try to compile it.
Am I correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, yes that is correct. Should be documented in the code then just to be sure for future readers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should inform svelte
maintainers that the 3 PRs should be released together.
Do you agree?
If it's not possible, using a try-catch will solve the issue
Thanks for taking a stab at this! |
Thank you for your feedbacks.
I was considering using something like
I agree, I will add this with an appropriate test.
I need to make some research on this.. Do you know a svelte maintainer who could help? |
- inject vars from markup to typescript - do not use importTransform when markup is available - inject component script to context=module script - refactor modules/markup to reuse common functions - test injection behavior
@milahu could know more |
cfa861e
to
e5a73db
Compare
Hi @dummdidumm, I added a test for I noticed that we use common functions with |
- introduce two deps: magic-string, sorcery - produce a source map on each step of the process - store all source maps in a chain - refactor transformer to increase readability
435a6d5
to
8c97341
Compare
After some studies and tests, I managed to handle the source map transformation. ProcessHere is a little summary of the process:
Note: For backward compatibility, this behavior is disabled if no Note 2: This process is only applied if the RefactoringThis implementation introduced a lot of complexity in the transformer. DrawbacksThis implementation introduces two new dependencies:
Fun fact: these libraries are written by Rich Harris. Can we reuse these dependencies?
Maybe Additional notesI was not sure of a good way to test this source map transformation. I ended writting a simple test to check that source map is produced and I used the great You can find a screenshot of the result below. QuestionsDo you have any idea to better test this? Also, do you agree with the refactoring? Screenshot |
Now that the needed PRs are merged (new Svelte release pending), we can continue working on this. Some thoughts
I'm on vacation the next 3 weeks so I can't look into this in the near future. |
@SomaticIT I'll try to pick this up. The proposed solution is very smart, from what I see it only needs some minor adjustments.
|
@dummdidumm, thank you. I'm currently on vacation. I will be back next week. I would be happy to help.
I also had an idea that could lead to better performance : maybe we could make svelte pass the What do you think? |
Ah yes you're right, the injected code is always below the real code, and it's stripped away. So this should work then. I tested this out on a project and ran into one issue: When there's no code after transpilation, because the code is type-defs only, the mappings are an empty string, which breaks Another thing is: When using the TypeScript transpiler, it's now necessary to also install
I'm not sure if we can do this, this relies on the people having some kind of tree shaking as part of their build, which is not always the case. If we make this new behavior opt-in, people could manage that and say "on production builds I know this will work because of treeshaking" and turn if off. |
filename, | ||
sourceMapChain, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the sorcery error that happens on empty code/maps. A test case should be added to handle this.
if (!code) { | |
return { code, diagnostics }; | |
} | |
I've been a bit AFK these last two months, so I'll need to get my bearings regarding this whole scenario again.
We are already "educating" people to install what they're going to need for their respective workflows. However, I'm also all up for simplicity and, as it seems, most people are using/will use Svelte with Typescript at some point. I'd like to keep only the "main tools" as "peer dependencies" (ts, postcss, stylus, sass, etc) if possible, although What's keeping us from having the
Can you link the PR here? |
@kaisermann this can be closed now |
/!\ This is a work-in-progress. DO NOT MERGE!
Introduction
I open this PR to init the work and the collaboration on a resolution for #318.
Note: This implementation requires an unreleased version of svelte to work.
Note 2: You will find below a way to setup a test environment to test this implementation on your projects.
Background
Thanks to @dummdidumm advices, I added 3 features on
svelte
, some have been merged, some are waiting for review:These features enable 2 required things:
svelte
pass the full source markup to preprocessorssvelte.compile
to get a full report of used variables in markupDescription
This PR makes two improvements.
First, it takes the markup passed by
svelte
and make it available to transformers.This markup is optional here to be backward compatible with previous versions of
svelte
.Then, it uses the full markup, strips the
script
andstyle
tags and pass it tosvelte.compile
to get a report of all used variables in the template. This list of variables is appended to the TypeScript code to allow the TypeScript compiler to correctly handle imports. Thanks to this, we can remove the customimportTransformer
whenmarkup
is available.TODO
This is a work in progress.
First of all, it needs above PRs to be merged and released on
svelte
side.Then, it misses some cases:
context="module"
scriptsFor this last task, I will need the help of the community 😉.
I also have very few experience on sourcemap manipulation so if someone can help on this...
Setup test environment
If you want to test working on this PR, you will need to do the following:
If you want to test this feature in your project (I will need your feedbacks):
Performances impact
On my computer, I benchmarked the preprocessing of this svelte file:
test/fixtures/TypeScriptImports.svelte
.Before this PR: ~30ms
After this PR: ~55ms
Before submitting the PR, please make sure you do the following
npm run lint
!)Tests
npm test
oryarn test