Closed jdtjenkins closed 8 months ago
I'm not sure I fully understand your suggestion. I think it's great to keep addVirtualImport
as a low-level primitive, but what you're describing could be another utility/plugin built on top of it! How should such utility be called?
Maybe I misunderstood the issue? I was trying to answer the question "What would it look like if addVirtualImport
accepted an array or object for creating virtual modules". I agree this API is too complicated for a lower level primitive like addVirtualImport
(especially if it included typegen) so it would make more sense if this was a higher level utility.
The basic idea for this API is that it can be annoying to have to compile your modules into strings so being able to create a module from an array or object makes it more user friendly and opens the door for doing stuff like typegen for modules and resolving relative imports with a custom base/root. Maybe something like this would be a better API:
generateVirtualModule({
name: 'my-module',
root: integrationDir,
import: [ './styles.css' ],
export: {
default: JSON.stringify(config),
Button: './components/Button.astro'
},
})
If the higher level nature of this API isn't suited for AIK no worries! Let me know and I can always make this a separate package
Yeah the issue is originally about something way simpler than your proposal and we'll keep it that way! However, I'd love to see an AIK plugin for this! You can either make it 3rd party or a PR, and we can discuss on that one directly! I'm sure Jacob and Luiz will have some valuable feedback as well
Sounds good, sorry for the misunderstanding! I thought that maybe I could help out with this issue since it has the "help wanted" and "good first issue" tags but there is not a lot of information about what the goal of the issue is. Do you mind explaining a bit more about what this issue is about? The title: "array or a series of imports that can all live in one virtual export just like starlight" sounds like it is describing starlight's virtual:starlight/user-css
virtual module that has a series of CSS imports inside a single virtual module like:
import '../styles.css';
import '../styles.css';
import '../styles.css';
import 'virtual:starlight/user-css';
Yeah this is clearly our fault for not writing stuff down! Currently, calling addVirtualImport
creates one vite plugin per virtual import (see https://github.com/florian-lefebvre/astro-integration-kit/blob/main/package/src/utilities/add-virtual-import.ts#L10). What we want is allow it to receive an array instead and do things similar to this https://github.com/withastro/starlight/blob/main/packages/starlight/integrations/virtual-user-config.ts#L11.
I think it could look like this in terms of API:
// utility
addVirtualImports({
updateConfig,
imports: {
"virtual:whatever": "export const ..."
}
})
// plugin
addVirtualImports({
"virtual:whatever": "export const ..."
})
That's a breaking change because that would mean changing both the type signature and the utility name. I'm not concerned about breaking changes tho because we're in an experimental phase, so that just needs to be a minor!
If you have other/better ideas for the API, feel free to just do it and we'll review on the PR!
That makes a lot more sense 😅 I will start exploring this and open a PR when I have something working, thanks!
Awesome thank you! Let me know if you need anything
@BryceRussell That was such a detailed an well thought out explanation! This is completely my fault because I kinda just yeeted this issue out with literally 0 context!
Yes, for this particular thing I was just thinking exactly what @florian-lefebvre said! But mate. That proposal is FUCKING SICK and I love it. Yes. I love it, especially as I'm making themes now, this would be absolutely perfect for me if I'm honest. And I think that kind of high-level abstraction is perfect for an AIK plugin absolutely.
@florian-lefebvre or @jdtjenkins In this example:
// utility
addVirtualImports({
updateConfig,
imports: {
"virtual:whatever": "export const ..."
}
})
There is no name
property, is that on purpose? If so, how should the Vite plugin that resolves these imports be named? I could use the name/key of the first import inside imports
to create the plugin name but that doesn't seem right?
mmmh you're right! I think passing a name makes sense then!
I think the Vite plugin could be the name of the integration, with maybe a count if it is called multiple times.
I'll have a look in the PR.
But @BryceRussell, I loved your idea for a new plugin. It seems like a lovely middle ground between a combination of AIK's addVirtualImport
and addDts
and my own inlineModule
from Inox Tools. Might be a more friendly API for those who don't want to generate everything manually but also don't want the level of magic from Inox Tools.
I think it's worth accepting an object or an array of objects