web-infra-dev / rsbuild

The Rspack-based build tool. It's fast, out-of-the-box and extensible.
https://rsbuild.dev/
MIT License
1.75k stars 137 forks source link

[Feature]: Support using `ProvidePlugin` with configuration #3875

Open colinaaa opened 4 hours ago

colinaaa commented 4 hours ago

What problem does this feature solve?

Just like source.define uses DefinePlugin, source.provide uses ProvidePlugin.

This is useful for plugin to override other plugin's provides.

Real world use case

We have 2 plugins.

Currently, the only way to override the Foo in pluginB is to use bundlerChain:

// pluginA
api.modifyBundlerChain(chain => {
  chain.plugin('provide.foo').use(ProvidePlugin, [{ Foo: 'path/to/foo' }])
  chain.plugin('provide.bar').use(ProvidePlugin, [{ Foo: 'path/to/foo' }])
})

// pluginB
api.modifyBundlerChain(chain => {
  chain.plugin('provide.foo').use(ProvidePlugin, [{ Foo: 'path/to/overrided-foo' }])
})

What does the proposed API look like?

A new configuration source.provide is added.

export default {
  source: {
    provide: {
      Foo: [require.resolve('foo'), 'default'],
      Bar: require.resolve('bar'),
    },
  },
}
chenjiahan commented 3 hours ago

Rsbuild added support for source.define because it is a popular feature and almost every application uses it.

But for ProvidePlugin, I think only a few applications or tools use this plugin, so I prefer to keep it as an opt-in plugin rather than wrap it as an Rsbuild configuration option.