-
Notifications
You must be signed in to change notification settings - Fork 71
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
Importing Vue SFC from lang="ts"
block
#129
Comments
Could you post your tsconfig and rollup config? |
Or a small repo with reproduction :) |
I unfortunately don't have a "small" repo. I am working here, trying to migrate from The import in |
Hello. First of all thank you very much for working on this plugin. It does indeed make much more sense than I can confirm that this issue exists and setup a small demo repository: If you commment out the line It's funny that we encountered this issue around the same time. Maybe it was caused by a recent change? That would explain why 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. |
Reproduced on both repos, not sure if it is the same problem yet, but it is likely. To fix second case we do indeed need to send module resolution back to rollup so that vue plugin can do it's thing. Problem with hooking up rollup's module resolution and typescript is that rollup in later versions returns a Promise from
|
Somewhat related: rollup/rollup#2631 |
Hello ezolenko. Nowadays asynchronous methods in Javascript mostly means methods returning Promises. The new However, in NodeJs, stuff like this is actually possible by yielding the synchronous method on the current thread, then jumping to the async method and jumping back to the synchronous method when the asynchronoues method resolves. See Fibers or wrappers around it like synchronize.js. Thus, the thing with the async method invocation is not much of a problem actually. There might be another problem though. While the plugin context offers a method One approach to solve this might be to add Another approach might be to run only For now I don't see a more beautiful solution yet. Edit: On second thought, the second approach is maybe not that ugly. Instead of |
@danimoh @eddow Workaround for both example repos are disabling error checking with The error is basically typescript complaining that it doesn't know what type *.vue stuff is ( (in the minimal repo, first component needs a reference to the second one, otherwise rollup treeshakes it away from the bundle) |
@danimoh yep, no way to get module source from rollup via context. Most of it can be done on vue plugin side (I opened a case there), but there are still potential pitfalls, like rpt2 will need to have transformed extracted script before transforming a script that imports it. |
I think the approach you describe in the vue issue thread requires typescript to process the files one at a time as it essentially ignores the vue file imports and waits for rollup to feed them back to typescript. Therefore you'd loose type checking across files. As an alternative to letting the vue plugin handle ts, the following might be a valid approach and a sort of better iteration of the ugly hacks I proposed earlier:
Edit: Actually, if we can overwrite |
Vue plugin instance is probably exposed, I don't know if rollup expects plugins to call each other and if something will break (immediately or in the future) in that case. Your second point will work, that's exactly what This approach might work, the main downside being potentially fragile coupling with vue plugin and extra cycles for throwaway work. I wish rollup had a way for plugins to abort transform and declare a dependency to be transformed before current file being retried, then this could be implemented cleanly... |
I think there is actually no throwaway work. Typescript will only compile the code once and also the vue plugin will need to process every file only once if we cache the extracted ts code. This approach does not discard any of it's own results or results of other plugins other than my previous suggestion. Yeah, some architectural change would be required in rollup. Maybe one could open an issue there to implement something like this but it would probably take ages. |
It works for me with this setup, to compile a single vue component: import VuePlugin from 'rollup-plugin-vue'
import typescript from 'rollup-plugin-typescript2'
export default {
plugins: [
typescript({
typescript: require('typescript'),
objectHashIgnoreUnknownHack: true,
}),
VuePlugin(/* VuePluginOptions */),
],
input: 'src/components/HelloWorld.vue',
output: [
{ file: 'dist/HelloWorld.cjs.js', format: 'cjs' },
{ file: 'dist/HelloWorld.esm.js', format: 'esm' },
],
} I'm not sure if this is the best way to create a module out of a Vue SFC with If anyone has any advice, please let me know. |
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 thiswhy? here's an example: <script lang="ts">
import Bar from './Bar.vue';
// ...
</script>
Not much.
Vuetify is typescript, but doesn't use SFCs. It's pure render functions. Buefy uses SFCs and rollup, but no typescript.
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.
If you've come up with a better solution, I'd love to hear it. |
Hi @subdavis, sorry for the question but i am nit that familiar with rollup and its plugins. |
@Iskren1990 It's been like 18 months since I wrote that, and I don't remember a lot of the context. However, a lot has happened in the world of Vue since Oct. 2020. Most notably, Vite happened. There's a library mode for this exact thing. If you're building a component library in 2022, I'd seriously consider using Vite. |
I also switched to vite with library mode for bundling. And vue-tsc on the side for generating typescript type files. |
lang="ts"
block
Thought I'd note some findings here that I stumbled upon in Webpack-land.
The current version of Vue SFC support in I haven't looked at |
While switching from
rollup-plugin-typescript
torollup-plugin-typescript2
in order to produce declaration files, the*.vue
files are not recognized anymore.Trying by just importing
rollup-plugin-typescript
instead ofrollup-plugin-typescript2
bundles without a problem.This could be bound to this issue though I have the last version (of every plugin today indeed).
The text was updated successfully, but these errors were encountered: