question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing Vue SFC from `lang="ts"` block

See original GitHub issue

While switching from rollup-plugin-typescript to rollup-plugin-typescript2 in order to produce declaration files, the *.vue files are not recognized anymore.

[!] (rpt2 plugin) Error: someFolder/index.ts(2,53): semantic error TS2307 Cannot find module './component.vue'.
src\index.ts
Error: someFolder/index.ts(2,53): semantic error TS2307 Cannot find module './component.vue'.

Trying by just importing rollup-plugin-typescript instead of rollup-plugin-typescript2 bundles without a problem.

This could be bound to this issue though I have the last version (of every plugin today indeed).

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
subdaviscommented, Apr 24, 2022

It works for me with this setup, to compile a single vue component:

Right, but that’s not a real use case. That’s hello world. For anyone having trouble understanding the problem, here’s what I’ve gleaned.

rollup literally can’t do this

why? here’s an example:

<script lang="ts">
import Bar from './Bar.vue';
// ...
</script>
  1. vue plugin passes script over to typescript plugin
  2. typescript plugin encounters a .vue file, but has no way of knowing what to do with it because rollup doesn’t provide a mechanism for plugins to defer to other plugins on imports like webpack. Regular JS can defer to plugins, but code already being processed by a plugin cannot.
  3. I actually don’t understand why this is different than lang=scss or lang=ts, but it is I guess.

Well, what can I do?

Not much.

But vuetify! buefy!

Vuetify is typescript, but doesn’t use SFCs. It’s pure render functions.

Buefy uses SFCs and rollup, but no typescript.

Really though, there’s nothing I can do?

You aren’t going to like it. For every Vue file you need to import from a typescript file, you’ll have to create a regular javascript file intermediary.

import Bar from './Bar.vue';

export default Bar;

Then and only then will you be able to build your typescript SFC component lib with rollup.

geez, that sucks

If you’ve come up with a better solution, I’d love to hear it.

2reactions
danimohcommented, Jan 13, 2019

Hello.

First of all thank you very much for working on this plugin. It does indeed make much more sense than rollup-plugin-typescript.

I can confirm that this issue exists and setup a small demo repository: https://github.com/danimoh/rollup-plugin-typescript2-vue-demo

If you commment out the line import AnotherComponent from './AnotherComponent.vue'; it does compile, but unfortunately not with this line enabled.

It’s funny that we encountered this issue around the same time. Maybe it was caused by a recent change? A guess from my side with very limited knowledge about rollup, rollup plugins and typescript would be: Is it possible that typescript itself is trying to import AnotherComponent instead of rollup or rollup-plugin-vue handling that import first?

That would explain why rollup-plugin-typescript does not have this issue as it compiles on a per file basis with transpileModule.

In this case, the following might be interesting: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#customizing-module-resolution

Any work on this issue is very much appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SFC Syntax Specification - Vue.js
This has lower priority than explicitly registered/imported components. Pre-Processors #. Blocks can declare pre-processor languages using the lang attribute.
Read more >
Vue.js plugin, two script blocks, SFC, TypeScript ... - YouTrack
Vue.js plugin, two script blocks, SFC, TypeScript and Composition API ... FIRST BLOCK without setup attribute --> <script lang="ts"> import ...
Read more >
How To Use TypeScript with Vue Single File Components
In this tutorial, you will use @vue/cli to generate a new Vue.js 2.0 application with TypeScript and build a Single-File Component (SFC).
Read more >
How to use the kind of TypeScript in a Single File Component ...
But when trying to compile the next component it shows me error. <script lang="ts"> import Vue from ...
Read more >
Add TypeScript Support to Vue 2 Project | Ji ZHANG's Blog
vue -loader will extract <script lang="ts"> blocks from SFC (Single-File Components) and they also get compiled. The resolve and appendTsSuffixTo ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found