Skip to content

Add support for TypeScript in script block #7

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

Closed
wants to merge 4 commits into from

Conversation

dwightjack
Copy link
Contributor

Hi,

I've tested the plugin on a project using TypeScript in the script block (<script lang="ts">) and esbuild was complaining about TS specific syntax.

This PR adds support for TS by reading the attrs.lang attribute from the parsed SFC descriptors and sets the correct esbuild loader (js or ts).

I had to use the parse function from @vue/component-compiler-utils because at the moment the language information is not exposed by @vue/component-compiler (see vuejs/vue-component-compiler#113). The downside is that each module is parsed twice 😞 .

Another change that I made was using a module to strip all comments from the script source because the build wasn:' working with code like this:

<script lang="ts">
import Vue from 'vue';
/**
 * ```html
 * <script>
 * export default Vue.extend({})
 * <\/script> 
 * ```
 */
export default Vue.extend({
  props: {
  }
  ...
</script>

Basically, the compiler was detecting two default exports. 🤔

@dwightjack
Copy link
Contributor Author

@apeschar by the way: thanks for cleaning up and fixing my code 🙇

@apeschar
Copy link
Owner

apeschar commented Oct 6, 2021

I don't like the fact that the component is parsed twice. Would it be an idea to first submit a PR to the https://github.com/vuejs/vue-component-compiler so that this info is exposed, and then use it here?

@dwightjack
Copy link
Contributor Author

I don't like the fact that the component is parsed twice. Would it be an idea to first submit a PR to the https://github.com/vuejs/vue-component-compiler so that this info is exposed, and then use it here?

Ok, I will take a look into it. 👍

@dwightjack
Copy link
Contributor Author

@apeschar sorry for the delay. Just an update to inform you that I have submitted the PR to the vue-component-compiler: vuejs/vue-component-compiler#122 😃

@apeschar
Copy link
Owner

apeschar commented Jan 4, 2022

Thanks for your efforts. It doesn't seem like there's much activity on the vuejs/vue-component-compiler#122 PR, unfortunately.

I've taken a different approach by defining our own versions of compileToDescriptor and compileToDescriporAsync to add the lang property to the scripts. This should achieve the same result without having to parse twice.

I've also integrated your suggestion to strip comments from scripts.

I will now close your PR. You should be able to upgrade to esbuild-vue 1.1.0 and use TypeScript.

@apeschar apeschar closed this Jan 4, 2022
@dwightjack
Copy link
Contributor Author

@apeschar sadly, you're right. I'm going to keep that PR open just in case.

I've taken a different approach by defining our own versions of compileToDescriptor and compileToDescriporAsync to add the lang property to the scripts. This should achieve the same result without having to parse twice.

That's great news! Thanks! 💯

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.

2 participants