-
-
Notifications
You must be signed in to change notification settings - Fork 506
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
feat(plugin-vite): better logic #3583
feat(plugin-vite): better logic #3583
Conversation
This is not a mandatory PR to be merged, it's only for discussion. |
7fc7a19
to
23aca91
Compare
I still fail to understand why the default behavior you want is to copy everything and ignore specific things rather than the other way around. Please, try to help me understand why this is preferable when only a few modules actually need to be copied (or perhaps none at all, like in my project). Vite's default behavior is to bundle everything together and tree shake. What is the point of using vite if you are going to ignore half of what it does? Edit: if the answer is to avoid breaking changes, I would argue that we should make this breaking change, as it was done incorrectly to begin with. It would be better to right this wrong sooner rather than later, and save countless people in the future from bundling their app incorrectly. |
Regarding this implementation, I think I have two comments:
|
The first misconception is that not all If we exclude all |
Sorry, I keep saying "everything" when I mean "all dependencies". Dev dependencies will not be copied, of course.
Are you sure about this? My project is not configured to be an ES module and yet tree shaking works perfectly fine.
What exactly is the difference between a user knowing that they should ignore their dependencies and a user knowing they should specifically copy their native modules? Either way, a user has to know that they need extra configuration. Since native code is rarer, i do not think a default rule favoring it is preferrable, personally. Side-note: I will admit that I was previously unaware that are legitimate projects that put all their dependencies in devDependencies. There's apparently a line of thinking that all dependencies for a non-library are dev dependencies since the user does not need to install them and they are only needed for the build. I had not personally encountered this and based on my (admittedly limited) nodejs experience this is different from the norm. However, that seems to be more common in vite land? So while I might disagree with it, it is probably what vite users expect. I'm mostly just concerned about this due to the mismatch between the webpack plugin and the vite plugin. Maybe someone starting with the vite plugin would know to put all their dependencies into devDependencies because that's what vite people do, whereas a webpack user migrating to vite wouldn't. In that case, I would suggest a migration guide to help mitigate what I ran into, (which was wasting hours trying to figure out why it was bundling code i didn't want). At the very least, proper practice for ensuring your dependencies get processed properly should be documented. Anyway, I don't wanna be one of those guys who just relentlessly argues on the internet about something. So, I will not make any more comments on this topic as a whole except for making replies. I think whatever you decide to do is fine, and if it does not work for me and my project I can always just continue to patch the plugin. |
a517c24
to
34dc5a7
Compare
The main problem are things like |
And it's less of a PITA to manually blacklist? |
I usually rely on EDIT: It would perhaps be useful if |
This comment was marked as off-topic.
This comment was marked as off-topic.
366fdb8
to
6774799
Compare
2c00b69
to
660b192
Compare
660b192
to
9c6ca78
Compare
@caoxiemeihao is there a way we could test the changes our for you? just excited for the updates and curious when it might be out? thanks! and thank you for your work on this @caoxiemeihao and advocacy @joeyballentine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PTAL @electron/forgers |
const userConfig = (await loadConfigFromFile(configEnv, buildConfig.config))?.config ?? {}; | ||
switch (target) { | ||
case 'main': | ||
return mergeConfig(getMainViteConfig(configEnv as ConfigEnv<'build'>), userConfig); | ||
case 'preload': | ||
return mergeConfig(getPreloadViteConfig(configEnv as ConfigEnv<'build'>), userConfig); | ||
case 'renderer': | ||
return mergeConfig(getRendererViteConfig(configEnv as ConfigEnv<'renderer'>), userConfig); | ||
default: | ||
throw new Error(`Unknown target: ${target}, expected 'main', 'preload' or 'renderer'`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get a bypass? I started with 7.4.0
and have modified the vite.base.config.mjs
. Mainly to provide a root
if the config gets loaded by vitest
and for vite-tsconfig-paths
. Without a bypass this would be a breaking change.
EDIT: Has it been considered to provide the vite extensions and default configurations as a library? That would allow me to pluck things like root
from the default config and I can call mergeConfig
afterwards myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vite.base.config.mjs
in your project will have higher priority than @electron-forge/plugin-vite
, and your configuration will still be valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is only true for things which I explicitly configure, e.g. if I use a different way to implement hot-restart, I'd have to override the plugin applied by default to do nothing… which also looks rather awkward. Therefore it would be nice, if I could just import the pieces I want from this plugin and apply them by myself.
await fs.writeJson(path.resolve(buildPath, 'package.json'), pj, { | ||
spaces: 2, | ||
}); | ||
|
||
// Copy the dependencies in package.json | ||
for (const dep of flatDependencies) { | ||
await fs.copy(dep.src, path.resolve(buildPath, dep.dest)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I reading this correctly and this removes the functionality to copy dependencies listed in package.json
? Is this supposed to land in a forge v8 package or will this breaking change land in a minor version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be released as a minor version, because Vite does not build some Node.js modules reliably.
However, most people still want Vite to handle the Node.js modules in dependencies
, which can effectively reduce the size of the App build.
In any case, the current change is the choice of most people. If other problems are encountered during the upgrade process, we may need some community-provided Vite plugins to handle them instead of forge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case, the current change is the choice of most people.
And how has this been measured?
If other problems are encountered during the upgrade process, we may need some community-provided Vite plugins to handle them instead of forge.
Do you mean a vite plugin to include the native binaries in the bundle?
In any case why is it necessary to break existing configuration now? AFAICT it would be possible to add an option to toggle this behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, people who have used @electron-forge/plugin-webpack
plugin hope that vite-plugin can have the same design as webpack-plugin, so that they can easily migrate from Webpack to Vite. But in fact, Vite and Webpack are not compatible in many places, and we can only work hard to make it more like Webpack. At least the current PR behavior is consistent with Webpack.
We may need to remove some hack behaviors from Forge, and they will be done by Vite plugin. For example, the copy behavior of dependencies
looks very stupid in the eyes of Webpack users, but Vite often cannot solve such problems, especially C/C++ native packages.
Of course, I personally think that the current version of Vite plugin is the result of weighing many aspects, but it is not easy to be accepted by Webpack plugin users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For users, the upgrade will bring some additional problems, which is not only a problem faced by Forge users, but more accurately, it should be a problem that Vite needs to face. I will try my best to provide some additional plugins to help users solve these problems, and also to provide Vite ecosystem with more Webpack-like capabilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case why is it necessary to break existing configuration now? AFAICT it would be possible to add an option to toggle this behavior.
Adding an option to let the user decide whether to copy dependencies
would be necessary, as this PR is clearly a departure from previous behavior.
Please let me know if anyone agrees to add this option?
Updated the PR from a |
Up until v7.2.0,
@electron-forge/plugin-vite
was designed to minimize user boilerplate, with all required Vite configurations already integrated into the plugin itself. While this approach provided a good starting experience for users, it also had a few downsides:In #3468 (released in Forge v7.3.0), I shifted a lot of the configuration logic from
@electron-forge/plugin-vite
to@electron-forge/template-vite
, which enabled useful features such as hot restart, hot reload, and improved ESM support (which is important for frameworks such as Svelte) to be implemented with slight tweaks to the template code.However, the changes introduced in v7.3.0 broke builds for existing users of
@electron-forge/plugin-vite
(see #3506). Based on community feedback brought by users of both v7.2.0 and v7.3.0, I am trying to release a new version that will have the advantages of both iterations of the plugin and not introduce breaking changes between updates.What changes did this PR make? First of all, let’s make it clear that the underlying Vite configs of v7.2.0 and v7.3.0 are almost identical, except that they are located in different places. So, if we keep the v7.3.0 config as-is and move it to
@electron-forge/plugin-vite
, we can solve the problem we are facing now. This is exactly what this PR does.In addition, since v7.2.0 does not have Hot Restart and Hot Reload features,
this PR provides a
{ target?: 'main' | The 'preload' }
option is used to enable them, but it is not required.The main purpose of this PR is to solve the two issues currently encountered without breaking changes.
<7.2.0
and>=7.3.0
of Forge. (Undocumented breaking change for Vite plugin in v7.3.0 #3506)dependencies
to node_modules of built App. (fix(plugin-vite): Don't copy node_modules on build #3579)