Environments build API #19714
Replies: 2 comments 7 replies
-
Thanks for the detailed proposal @dario-piotrowicz! I like the idea of making One idea we discussed long ago with @antfu when toying with making We could extend // framework plugin
async buildApp(builder) {
await builder.build('client');
await builder.build('ssr');
await builder.build(...);
} // platform plugin
async buildApp: {
order: 'post',
handler(builder) {
const environmentsToBuild = builder.environments.filter(env =>
configuredEnvironments.has(name === env.name);
);
// Safely call build again on all the environments that needs to be built,
// not a problem to call the ones that have already been built
await Promise.all(environmentsToBuild.map(env => builder.build(env)));
generatePlatformSpecificArtifactsForDeployment();
}
} This assumes that each environment only needs to be built once. But I think that is a safe assumption to make. |
Beta Was this translation helpful? Give feedback.
-
@patak-dev Do you think this change could be considered for the 7.0 milestone? It would make sense to introduce a breaking change like this in a new major, even though the Environment API is experimental. Perhaps it could be enabled with a flag before then to gather feedback. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I’m opening this discussion to propose a new build API for the environment API (for a prior discussion on this topic see: #16358 (comment))
The Problem
The problem in a nutshell is that the current environments build API based on the
buildApp
config is too limited, since it only allows a single plugin to control the build process of the whole application, including all of its environments.This is limiting/problematic for a few reasons:
The proposed solution
We’ve (the Cloudflare team) considered and discussed internally various different alternatives, but we settled on the following as it provides more flexibility and less downsides than any other alternative we considered.
The solution would be to make
buildApp
a hook rather than part of the config. Such a hook would run for each plugin sequentially and it could be defined as a function or an object with order and handler properties, as is common for other hooks.A framework plugin would typically leave order unspecified whereas a platform plugin would typically set order to post. This convention would allow platforms to build additional environments and output configuration files required for deployment while allowing frameworks to get priority on the build process.
An additional
builder.builtEnvironments
property that is aSet
containing the environments that have so far been built would also be made available (so that plugins could change behavior based on whether an environment has already been built or not).Let’s see an example of this API where there are two plugins both being used, the first implementing a framework while the second targeting a platform for the application to be deployed to:
Advantages of this approach
The advantages of this approach are that such API:
Possible additions
It may also be worth considering adding a
builder.clearEnvironment(environment.name)
method to clear the built artifacts of an environments as some frameworks create a server build that is only used for prerendering. This wouldn’t necessarily need to be part of the initial API.Alternatives
For completeness, here's a list of alternative solutions that we’ve considered, alongside explanations describing why we haven’t considered them as optimal as the proposed solution:
Adding a
buildApp.targets
configAdding a targets config option such as:
(reference: #16358 (reply in thread))
This wouldn’t allow multiple plugins to potentially build the same environment and also would require extra coordination between framework and platform plugin
Pre and Post hooks
Adding some pre and post hooks around the existing buildApp config or something to that extent (basically in the same vein as #16358 (comment))
This seems to add unnecessary complexity over the proposed solution without any noticeable gain
Declarative build configs
buildApp
hooks would take and return build configuration objects containing declarative information on which environments Vite needs to build and how, in the same vein as how the config hook works where each plugin is able to update, pass or override the config object in a very flexible manner.This feels too different from the current environments API design, defining the build configuration objects would also be very involved.
buildEnvironment
middleware hookEach plugin could provide a
buildAppEnvironment
middleware hook that would get a promise or set of promises for the current environment builds and would return a promise that will be passed through to the next plugin in the chain, after the last middleware hook the environment could be built.In this way each middleware can wrap the call to the next middleware with pre and post processing, but also await other builds before doing processing, a middleware would also be able to block a build from occurring at all by not calling the next middleware.
This could be an interesting API but it might require a complex mental model and implementation, the proposed solution seems simpler both to use and implement.
Beta Was this translation helpful? Give feedback.
All reactions