Open qlaffont opened 8 months ago
Will take a look!
@nksaraf any update ?
@nksaraf any update ?
Haven't been able to give this one time. It could take me a bit longer. @edivados do you think you could help out on this one?
[!NOTE]
The following is just what I observed. I have not invested a lot of time into this so some things may not be accurate.
- The image in the example is small and vite will inline it so the plugin doesn't pick it up.
beforeBundle: true
seems to work.
In vinxi public files are not copied by vite during build and the plugin seems to have two issues when publicDir
is set to false
.
generateBundle
hook runsWhen publicDir
is set to false the outDir doesn't exist yet and the write here fails.
I don't know much about vite/unplugin so I am not sure if writing to disk here is just wrong or it should not rely on the outDir to exist.
publicDir
to construct paths?When publicDir
is false it receives/uses an empty string and the paths it builds here don't exist.
This one is a Solid-Start issue. In Solid-Start when you pass a plugins array it will be assigned to multiple routers which means the same plugin instance is run multiple times.
This plugin has internal state that doesn't get cleaned up leading to the same assets being processed multiple times concurrently causing issues.
A user can get around this problem by using a function instead of an array to set plugins.
{
plugins: () => [plugin1, plugin2]
}
Okay so public dir needs to be set to true for this plugin to work. Can we set it in userland for when this plugin is used. Or we dedupe all assets server and client and public and that way we get all the assets
@nksaraf Played around a bit more with it and updated my previous message. See point 3.
Can we set it in userland for when this plugin is used.
A user could do that. I did this while testing. Not sure what other issues will pop up. Keeping the public router would mean in this case that unprocessed files are being copied to the output.
Turns out removing the public router has no effect because nitro adds an entry to publicAssets
for every scanDirs
entry so files from the public directory get copied anyway. Nitro adds srcDir to scanDirs so it's not empty.
https://github.com/unjs/nitro/blob/7d3ca94f14aa712eaeb0c4f0b8d81b126f2e913b/src/options.ts#L260-L261
https://github.com/unjs/nitro/blob/7d3ca94f14aa712eaeb0c4f0b8d81b126f2e913b/src/nitro.ts#L70-L78
In my opinion this plugin has two things that should be fixed.
generateBundle
using fs. I doubt plugins are responsible for writing to disk here if they will be written by the bundler later anyway. Changing the source should be enough I would think.publicDir: false
. This is a valid setup where you have no public files but eventually static assets.Or we dedupe all assets server and client and public and that way we get all the assets
Yeah... in Solid-Start there are duplicates between ssr and client router output. If the plugin didn't have the mentioned issues I could also imagine the static router being a vite build too and making something like this eventually possible.
[
{
name: "public",
mode: "static",
dir: "./public",
base: "/",
plugins: [ imagemin ] // no static assets but public files
},
{
name: "client",
plugins: [ imagemin ] // no public files but static assets
}
]
So what I need to do or did I need to create issue (and where) ? 😅
@qlaffont It's time to get creative for a short term solution. 🤣 This is what I could come up with. I am making use of the fact that the plugin seems to process assets but not public files with beforeBundle: true
and later manually use the plugin to process the public files.
Thanks @edivados ! I will try to use it. I will link this issue to the other issue : https://github.com/unplugin/unplugin-imagemin/issues/100 @ErKeLost :wave:
Oh interessting I will have to read through it. Maybe using beforeBundle: true
doesn't cover some things.
The resources loaded by src in beforeBundle cannot be parsed as a module. I haven't perfected this part yet. I'm sorry I don't have any extra time to maintain this package because of my work.
Ok I see. Thank you.
Can we just fork it and patch it for now and release it under @vinxi namespace .. this image optimizations stuff is probably important enough
I think it will be perfect ! We need to see with @ErKeLost
hi
I have time to reply to this question now. Thank you for all your discussions and comments.
Let me answer some of the questions mentioned above.
In vite, we usually use the following ways to introduce image resources.
Import img from'. / a.png'
. // Will be loaded as a module
< img: src= ". / a.png" / >
. // It is not usually loaded as a module.
In css file.
// Will not be loaded as a module
body {.
Background-image: url (". / a.png").
}.
Usually we use these ways to load our image resources.
But in fact, the second and third are not loaded by vite as modules, which leads to normal, for example, if we introduce it in a html now, it will not be built as a module in load or other hooks.
But in the second case, we are usually treated as a module by the plug-in so that vite can load it, such as the vue plug-in.
In the third case, the css file will not be treated as a module.
That's why I need to distinguish between beforeBundle and then replace the whole css with rules after the last rendersource to achieve the effect.
At present, the solid plugin look like does not implement the second way to load the module,(I haven't had time to check the implementation of solid yet.) so the situation of img src is not loaded as a module.
Due to the complexity of the current project caused by beforeBundle and squoosh and sharp svgo, and the confusion of the code, I recently have time to ReFactor the logic of the entire render, and I will also see if other implementations have a way to load all possible ways to load img as a module.
Thank you again for your reply.
I think it will be perfect ! We need to see with 我认为这将是完美的!我们需要看看@ErKeLost
This looks great. I will study whether the current problem can be solved perfectly.
ref : https://github.com/nksaraf/vinxi/issues/44 repro : https://github.com/qlaffont/test-cmp