cyrilwanner / next-compose-plugins

💡next-compose-plugins provides a cleaner API for enabling and configuring plugins for next.js
MIT License
736 stars 12 forks source link

Support next 5.1 phases #2

Closed cyrilwanner closed 6 years ago

cyrilwanner commented 6 years ago

Next 5.1 introduced phases in the next.config.js file so specific plugins can only get applied in a specific phase. We should also add support for it.

I'm not sure about the api yet, maybe something like this:

// next.config.js

module.exports = withPlugins([

  // only add next-offline on production build & production server
  [offline, {
    /* offline config */
  }, [PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_SERVER]],

  // add next-typescript to all phases (default if none is specified)
  [typescript, {
    /* typescript config */
  }],

  // add next-sass in every phase except on the development server (when first argument is '!' -> blacklisting phases)
  [sass, {
    /* sass config */
  }, ['!', PHASE_DEVELOPMENT_SERVER]],

], nextConfiguration);

Please comment if you have inputs or another idea :)

prateekrastogi commented 6 years ago

Wouldn't it be better if plugin can itself declare the phase in which it needs to be loaded without putting additional burden on plugin users? May be if some plugins need to loaded in user context specific way then we can extend and put extra phases, but i guess most won't

cyrilwanner commented 6 years ago

Good idea, I'll make this optionally available for plugin developers. But I think I'll still also make this available in the next.config.js file if a plugin doesn't add support for it so users can still decide (or overwrite) the behavior. It's a nice-to-have feature as Next.js officially added support for phases.

prateekrastogi commented 6 years ago

yeah, especially given that there are no other ways to separate dev plugins from prod plugins causing even dev plugins to included in main dependencies of package.json. At the run-time, composition should be intelligent enough to ignore missing dev plugins deps, which might be used through conditional import.

cyrilwanner commented 6 years ago

Hi @prateekrastogi, version 2 of next-compose-plugins just got released 🎉 It includes many improvements like phase specific plugins and/or configuration, additional information/possibilities for plugin developers and optional plugins (e.g. when they are only in the devDependencies). I would suggest you can check out the updated readme.

For the optional (dev) dependencies, you can take a look at the optional plugins section. Would this resolve the problems you had described? Or do you still have a use-case which would not be solved now?

Thanks again for your inputs and feel free to create new issues when you have more ideas or a problem with the newly introduced functionalities :)

prateekrastogi commented 6 years ago

@cyrilwanner Tried using the updated plugin in my projects and its working perfectly. Just a little clarification regarding optional plugin that some optional plugins may be dev dependencies, but do not necessarily need to be run in PHASE_DEVELOPMENT_SERVER . They may run in some other phases. For example, next-bundle-analyzer needs to be run in PHASE_PRODUCTION_BUILD, but will generally be preferred to included as dev dependency. So, may be removing "and so you only want to apply it during PHASE_DEVELOPMENT_SERVER" line from readme will help avoid confusion.

cyrilwanner commented 6 years ago

@prateekrastogi thank you for testing the updated plugin! You are right, that makes total sense, I'll remove this line from the readme.