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

feat: allow apply condition to be a function #4857

Merged
merged 2 commits into from Sep 10, 2021
Merged

feat: allow apply condition to be a function #4857

merged 2 commits into from Sep 10, 2021

Conversation

andylizi
Copy link
Contributor

@andylizi andylizi commented Sep 6, 2021

Description

apply can now be a function. Please see #4536 (review) for the original motivation.

function myPlugin() {
  return {
    name: 'client-build-only',
    apply(config, { command }) {
      return command === 'build' && !config.build.ssr
    }
  }
}

Additional context

Unfortunately we won't have ResolvedConfig ready when we check for apply and that's not easy to change (we don't want to call config hook on a plugin if it's ultimately not going to apply, for example). I eventually decided that UserConfig and ConfigEnv should probably be enough for most use cases, and those can always fallback to if (...) return pattern if necessary.

It felt a bit awkward trying to write documentation for this (not a native English speaker), any wording suggestions are welcome :D


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

brillout
brillout previously approved these changes Sep 6, 2021
Copy link
Contributor

@brillout brillout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it 😍

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Since this is a feature to the config interface, we will leave it to the meeting on Friday and asking Evan for input.

docs/guide/using-plugins.md Outdated Show resolved Hide resolved
docs/guide/api-plugin.md Show resolved Hide resolved
@andylizi
Copy link
Contributor Author

andylizi commented Sep 6, 2021

Done!

By the way, should I update the implementation in #4536 in this PR while we are at it?

@antfu

This comment has been minimized.

@patak-dev
Copy link
Member

I think it is better to do another PR to update the implementation later

@patak-dev
Copy link
Member

haha, just saw @antfu comment, @andylizi looks like this is your call then 😉

@patak-dev patak-dev merged commit f19282f into vitejs:main Sep 10, 2021
@patak-dev
Copy link
Member

Thanks for the PR @andylizi, it would be great to be able to clean up plugin-legacy 👏🏼

@andylizi andylizi deleted the apply-function branch September 11, 2021 03:34
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants