💡 next-compose-plugins
Provides a cleaner API for enabling and configuring plugins for next.js because the default way next.js suggests to enable and configure plugins can get unclear and confusing when you have many plugins.
It is often unclear which plugins are enabled or which configuration belongs to which plugin because they are nested and share one configuration object. This can also lead to orphaned configuration values when updating or removing plugins.
While next-compose-plugins
tries to eliminate this case by providing an alternative API for enabling and configuring plugins where each plugin has their own configuration object, it also adds more features like phase specific plugins and configuration.
Table of contents
Installation
npm install --save next-compose-plugins
Building a Dashboard or Admin UI? Or do you want to see a usage example of next-compose-plugins
in a real world project? Check out the NextJS Material Dashboard by our partners Creative Tim to get you started.
Usage
// next.config.jsconst withPlugins = ; moduleexports = ;
plugins
See the examples for more use-cases.
It is an array containing all plugins and their configuration. If a plugin does not need additional configuration, you can simply add the imported plugin. If it does need configuration or you only want to run it in a specific phase, you can specify an array:
[plugin: function, configuration?: object, phases?: array]
plugin: function
Imported plugin. See the optional plugins section if you only want to require a plugin when it is really used.
const withPlugins = ;const sass = ; moduleexports = ;
configuration?: object
Configuration for the plugin.
You can also overwrite specific configuration keys for a phase:
const withPlugins = ;const PHASE_PRODUCTION_BUILD = ;const sass = ; moduleexports = ;
This will overwrite the cssLoaderOptions
with the new localIdentName
specified, but only during production build.
You can also combine multiple phases ([PHASE_PRODUCTION_BUILD + PHASE_PRODUCTION_SERVER]: {}
) or exclude a phase (['!' + PHASE_PRODUCTION_BUILD]: {}
which will overwrite the config in all phases except PRODUCTION_BUILD
).
You can use all phases next.js provides.
phases?: array
If the plugin should only be applied in specific phases, you can specify them here. You can use all phases next.js provides.
const withPlugins = ;const PHASE_DEVELOPMENT_SERVER PHASE_PRODUCTION_BUILD = ;const sass = ; moduleexports = ;
You can also negate the phases with a leading !
:
const withPlugins = ;const PHASE_DEVELOPMENT_SERVER PHASE_PRODUCTION_BUILD = ;const sass = ; moduleexports = ;
This will apply the plugin in all phases except PHASE_DEVELOPMENT_SERVER
.
nextConfiguration
Any direct next.js configuration can go here, for example: {distDir: 'dist'}
.
You can also customize the webpack configuration of next.js within this object.
const withPlugins = ; const nextConfig = distDir: 'build' { // modify the `config` here return config; }; moduleexports = ;
Phases are also supported within the nextConfiguration
object and have the same syntax as in plugin configuration
objects.
const PHASE_DEVELOPMENT_SERVER = ;const nextConfig = distDir: 'build' '!' + PHASE_DEVELOPMENT_SERVER: assetPrefix: 'https://my.cdn.com' ;
Optional plugins
If a plugin should only get loaded when it is used, you can use the optional
helper function.
This can especially be useful if the plugin is only in the devDependencies
and so may not be available in all phases.
If you don't use the optional
helper in this case, you would get an error.
const withPlugins optional = ;const PHASE_DEVELOPMENT_SERVER = ; moduleexports = ;
Extend another config file
It sometimes makes sense to split a next.config.js
file into multiple files, for example when you have more than just one next.js project in one repository.
You can then define the base config in one file and add project specific plugins/settings in the config file or the project.
To easily archive this, you can use the extend
helper in the next.config.js
file of your project.
// next.config.jsconst withPlugins extend = ;const baseConfig = ; const nextConfig = /* ... */ ; moduleexports = ;
// base.next.config.jsconst withPlugins = ; moduleexports = ;
Plugin developers
This plugin has a few extra functionality which you can use as a plugin developer.
However, if you use them, you should mention somewhere in your readme or install instructions that it needs next-compose-plugins
to have all features available and so it won't confuse your users if something is not working as described out-of-the-box because they don't use this compose plugin yet.
Phases
You can specify in which phases your plugin should get executed within the object you return:
const PHASE_DEVELOPMENT_SERVER = ; module { return Object
These phases are handled as a default configuration and users can overwrite the phases in their next.config.js
file if they want to.
See phases configuration for all available options.
Additional information
When a plugin gets loaded with next-compose-plugins
, some additional information on which you can depend is available.
It gets passed in as the second argument to your plugin function:
module { console;};
Currently, it contains these values:
// this is always true when next-compose-plugins is used // so you can use this as a check when your plugin depends on it nextComposePlugins: boolean // the current phase which gets applied phase: string
Examples
Real world example
Check out the NextJS Material Dashboard by our partners Creative Tim to see how next-compose-plugins
was used in a real world application.
Basic example
// next.config.jsconst withPlugins = ;const images = ;const sass = ;const typescript = ; // next.js configurationconst nextConfig = useFileSystemPublicRoutes: false distDir: 'build'; moduleexports = ;
Advanced example
// next.config.jsconst withPlugins optional = ;const images = ;const sass = ;const typescript = ; const PHASE_PRODUCTION_BUILD PHASE_PRODUCTION_SERVER PHASE_DEVELOPMENT_SERVER PHASE_EXPORT = ; // next.js configurationconst nextConfig = useFileSystemPublicRoutes: false distDir: 'build'; moduleexports = ;
Comparison
As a comparison, it would look like this without this plugin where it is not really clear which configuration belongs to which plugin and what are all the enabled plugins. Many features mentioned above will also not be possible or requires you to have a lot more custom code in your config file.
// next.config.jsconst withSass = ;const withTypescript = ;const withImages = ;const withOffline = ; moduleexports = ;
See also
See vercel/next-plugins for a list of official and community made plugins for next.js.
License
MIT © Cyril Wanner