dario-piotrowicz / vite-environment-6.0.0-alpha-experimentations

1 stars 1 forks source link

Refactor `xEnvironmentProvider` to `xEnvironmentPlugin` #4

Closed dario-piotrowicz closed 5 months ago

dario-piotrowicz commented 6 months ago

The environment providers exported by the respective packages:

Are a bit too limiting, since their consumers can only use them in their config hook, for example: https://github.com/dario-piotrowicz/vite-environment-6.0.0-alpha-experimentations/blob/bb63a66604448fca5cb27afa712f18dd18b2723f/examples/dummy-framework/frameworkPlugin.ts#L19-L26

This prevents the environments to integrate with the vite pipeline (without the consumer manually adding plugins, etc...).

(for example if the workerd environment needed to always use some specific hook, say resolveId, that wouldn't be possible with the workerdEnvironmentProvider alone and the consumer would also have to separately register an extra plugins that configures the resolveId hook)

So replacing the above mentioned xEnvironmentProviders with xEnvironmentPlugins would enable us to have the plugins both great the environments and also integrate them with the underlying vite pipeline.

( usage example:

 plugins: [
  workerdEnvironmentPlugin('my-workerd-env-A'), // <- sets up a new workerd environment named 'my-workerd-env-A'
  workerdEnvironmentPlugin('my-workerd-env-B'), // <- sets up a new workerd environment named 'my-workerd-env-B'
  nodeVMEnvironmentPlugin('node-vm'), // <- sets up a new node-vm environment named 'node-vm'
 ],

)


source discord message

dario-piotrowicz commented 5 months ago

Chatted with Igor about this and we agreed on what needs to be done, we actually need to implement factories of plugins that then instantiate the environment.

What I mean is that the current user facing config for the workerd remix example is:

plugins: [
  remix({
    ssrEnvironment: await workerdEnvironmentProvider({
      config: './remix-wrangler.toml',
    }),
  }),
  tsconfigPaths(),
],

and in order to still be able for the end user to specify the config to use (or any other possible workerd option) we need something like:

plugins: [
  remix({
    ssrEnvironment: await workerdEnvironmentPluginFactory({
      config: './remix-wrangler.toml',
    }),
  }),
  tsconfigPaths(),
],

the workerdEnvironmentPluginFactory call will then generate a plugin that the Remix plugin then can use to instantiate the actual environment (basically the result of the workerdEnvironmentPluginFactory call is the workerdEnvironmentPlugin plugin I mentioned in my previous message 👆).

This allows us to move to the plugins structure suggested by Patak while still allowing the user to provide their own workerd options.

[!WARNING] workerdEnvironmentPluginFactory is NOT ok as the final user facing name, I've just used it in the example above since it is clearer to understand in this context, the final name should be something like just workerd, workerdEnv, ... or something simple like that