Skip to content
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

[Feature]: Plugin apply in plugin #3253

Closed
Justinidlerz opened this issue Aug 20, 2024 · 4 comments · Fixed by #3348
Closed

[Feature]: Plugin apply in plugin #3253

Justinidlerz opened this issue Aug 20, 2024 · 4 comments · Fixed by #3348

Comments

@Justinidlerz
Copy link

Justinidlerz commented Aug 20, 2024

What problem does this feature solve?

As a infrastructure developer, we provides many plugins for team member to assemble for match the business cases.

Such as:

  • pluginContentSecretly -> do some secretly thinks like: remove invalid charsets / remove domains ...
  • pluginMultiBundle -> call modifyRsbuildConfig to add preset environments

Wanna to add the pluginContentSecretly into one of environment sets of the pluginMultiBundle.

Actually I can extract implements of the pluginContentSecretly and partially call the same API method set in pluginMultiBundle, But it's ugly.

The pluginContentSecretly content see like:

const pluginContentSecretly = () => {
  return {
    name: 'plugin-content-secretly',
    setup: (api) => {
      api.modifyRspackChain(chain => {
         chain.plugin('remove-invalid-content-plugin').use(Plugin, [[ ... ]])

        // .... many many other rules in this plugin
      })
    }
  }
}

The pluginMultiBundle content see like:

const pluginMultiBundle = () => {
  return {
    name: 'plugin-multi-bundle',
    setup: (api) => {
      api.modifyRsbuildConfig(config => {
         config.environments = {
           normal: {
             output: {
               target: 'web',
             }
           },
           safety: {
             output: {
               target: 'web',
             }
             plugins: [pluginContentSecretly()], // <- it's not work
             tools: { // <- now only can implement as this
               rspack: {
                 plugins: [
                   new Plugin({ .... })
                 ]
              }
            }
           }
         }
      })
    }
  }
}

What does the proposed API look like?

We can apply the plugins in another plugin, I think the plugin lifecycle well be modify. Should discuss about such how about the setting override, plugin order, etc..

const pluginContentSecretly = () => {
  return {
    name: 'plugin-content-secretly',
    setup: (api) => {
      api.modifyRspackChain(chain => {
         chain.plugin('remove-invalid-content-plugin').use(Plugin, [[ ... ]])
      })
    }
  }
}

const pluginMultiBundle = () => {
  return {
    name: 'plugin-multi-bundle',
    setup: (api) => {
      api.modifyRsbuildConfig(config => {
         config.environments = {
           normal: {
             output: {
               target: 'web',
             }
           },
           safety: {
             output: {
               target: 'web',
             }
             plugins: [pluginContentSecretly()],
           }
         }
      })
    }
  }
}
@chenjiahan
Copy link
Member

Recursively registering plugins may not be a good idea, here is an reference: vitejs/vite#2484

@chenjiahan
Copy link
Member

chenjiahan commented Aug 20, 2024

I think Rsbuild can support nested plugins config, so users can return an array of plugins in a plugin function:

function myPlugin() {
  return [
    fooPlugin()
    barPlugin()
  ];
}

export default {
  plugins: [myPlugin()]
}

@Justinidlerz
Copy link
Author

I think Rsbuild can support nested plugins config, so users can return an array of plugins in a plugin function:

function myPlugin() {
  return [
    fooPlugin()
    barPlugin()
  ];
}

export default {
  plugins: [myPlugin()]
}

Okay, This cannot solve this problem, but seems it's helpful for preset to select plugin sets.

@Justinidlerz
Copy link
Author

Recursively registering plugins may not be a good idea, here is an reference: vitejs/vite#2484

Sound good, I think what if we expand full Rspack configuration can solve it same as the issue of vite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants