Gathering detailed insights and metrics for next-compose-plugins
Gathering detailed insights and metrics for next-compose-plugins
Gathering detailed insights and metrics for next-compose-plugins
Gathering detailed insights and metrics for next-compose-plugins
💡next-compose-plugins provides a cleaner API for enabling and configuring plugins for next.js
npm install next-compose-plugins
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
739 Stars
43 Commits
12 Forks
6 Watching
13 Branches
2 Contributors
Updated on 20 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-0.1%
51,397
Compared to previous day
Last week
-1.8%
268,971
Compared to previous week
Last month
8.3%
1,193,520
Compared to previous month
Last year
-34.7%
14,186,257
Compared to previous year
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.
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.
1// next.config.js 2const withPlugins = require('next-compose-plugins'); 3 4module.exports = withPlugins([...plugins], nextConfiguration);
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.
1const withPlugins = require('next-compose-plugins'); 2const sass = require('@zeit/next-sass'); 3 4module.exports = withPlugins([ 5 [sass], 6]);
configuration?: object
Configuration for the plugin.
You can also overwrite specific configuration keys for a phase:
1const withPlugins = require('next-compose-plugins'); 2const { PHASE_PRODUCTION_BUILD } = require('next/constants'); 3const sass = require('@zeit/next-sass'); 4 5module.exports = withPlugins([ 6 [sass, { 7 cssModules: true, 8 cssLoaderOptions: { 9 localIdentName: '[path]___[local]___[hash:base64:5]', 10 }, 11 [PHASE_PRODUCTION_BUILD]: { 12 cssLoaderOptions: { 13 localIdentName: '[hash:base64:8]', 14 }, 15 }, 16 }], 17]);
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.
1const withPlugins = require('next-compose-plugins'); 2const { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } = require('next/constants'); 3const sass = require('@zeit/next-sass'); 4 5module.exports = withPlugins([ 6 [sass, { 7 cssModules: true, 8 cssLoaderOptions: { 9 localIdentName: '[path]___[local]___[hash:base64:5]', 10 }, 11 }, [PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD]], 12]);
You can also negate the phases with a leading !
:
1const withPlugins = require('next-compose-plugins'); 2const { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } = require('next/constants'); 3const sass = require('@zeit/next-sass'); 4 5module.exports = withPlugins([ 6 [sass, { 7 cssModules: true, 8 cssLoaderOptions: { 9 localIdentName: '[path]___[local]___[hash:base64:5]', 10 }, 11 }, ['!', PHASE_DEVELOPMENT_SERVER]], 12]);
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.
1const withPlugins = require('next-compose-plugins'); 2 3const nextConfig = { 4 distDir: 'build', 5 webpack: (config, options) => { 6 7 // modify the `config` here 8 9 return config; 10 }, 11}; 12 13module.exports = withPlugins([ 14 // add plugins here.. 15], nextConfig);
Phases are also supported within the nextConfiguration
object and have the same syntax as in plugin configuration
objects.
1const { PHASE_DEVELOPMENT_SERVER } = require('next/constants'); 2const nextConfig = { 3 distDir: 'build', 4 ['!' + PHASE_DEVELOPMENT_SERVER]: { 5 assetPrefix: 'https://my.cdn.com', 6 }, 7};
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.
1const { withPlugins, optional } = require('next-compose-plugins'); 2const { PHASE_DEVELOPMENT_SERVER } = require('next/constants'); 3 4module.exports = withPlugins([ 5 [optional(() => require('@zeit/next-sass')), { /* optional configuration */ }, [PHASE_DEVELOPMENT_SERVER]], 6]);
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.
1// next.config.js 2const { withPlugins, extend } = require('next-compose-plugins'); 3const baseConfig = require('./base.next.config.js'); 4 5const nextConfig = { /* ... */ }; 6 7module.exports = extend(baseConfig).withPlugins([ 8 [sass, { 9 cssModules: true, 10 }], 11], nextConfig);
1// base.next.config.js 2const withPlugins = require('next-compose-plugins'); 3 4module.exports = withPlugins([ 5 [typescript, { 6 typescriptLoaderOptions: { 7 transpileOnly: false, 8 }, 9 }], 10]);
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.
You can specify in which phases your plugin should get executed within the object you return:
1const { PHASE_DEVELOPMENT_SERVER } = require('next/constants'); 2 3module.exports = (nextConfig = {}) => { 4 return Object.assign({}, nextConfig, { 5 // define in which phases this plugin should get applied. 6 // you can also use multiple phases or negate them. 7 // however, users can still overwrite them in their configuration if they really want to. 8 phases: [PHASE_DEVELOPMENT_SERVER], 9 10 webpack(config, options) { 11 // do something here which only gets applied during development server phase 12 13 if (typeof nextConfig.webpack === 'function') { 14 return nextConfig.webpack(config, options); 15 } 16 17 return config; 18 }, 19 }; 20};
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.
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:
1module.exports = (nextConfig = {}, nextComposePlugins = {}) => { 2 console.log(nextComposePlugins); 3};
Currently, it contains these values:
1{ 2 // this is always true when next-compose-plugins is used 3 // so you can use this as a check when your plugin depends on it 4 nextComposePlugins: boolean, 5 6 // the current phase which gets applied 7 phase: string, 8}
Check out the NextJS Material Dashboard by our partners Creative Tim to see how next-compose-plugins
was used in a real world application.
1// next.config.js 2const withPlugins = require('next-compose-plugins'); 3const images = require('next-images'); 4const sass = require('@zeit/next-sass'); 5const typescript = require('@zeit/next-typescript'); 6 7// next.js configuration 8const nextConfig = { 9 useFileSystemPublicRoutes: false, 10 distDir: 'build', 11}; 12 13module.exports = withPlugins([ 14 15 // add a plugin with specific configuration 16 [sass, { 17 cssModules: true, 18 cssLoaderOptions: { 19 localIdentName: '[local]___[hash:base64:5]', 20 }, 21 }], 22 23 // add a plugin without a configuration 24 images, 25 26 // another plugin with a configuration 27 [typescript, { 28 typescriptLoaderOptions: { 29 transpileOnly: false, 30 }, 31 }], 32 33], nextConfig);
1// next.config.js 2const { withPlugins, optional } = require('next-compose-plugins'); 3const images = require('next-images'); 4const sass = require('@zeit/next-sass'); 5const typescript = require('@zeit/next-typescript'); 6 7const { 8 PHASE_PRODUCTION_BUILD, 9 PHASE_PRODUCTION_SERVER, 10 PHASE_DEVELOPMENT_SERVER, 11 PHASE_EXPORT, 12} = require('next/constants'); 13 14// next.js configuration 15const nextConfig = { 16 useFileSystemPublicRoutes: false, 17 distDir: 'build', 18}; 19 20module.exports = withPlugins([ 21 22 // add a plugin with specific configuration 23 [sass, { 24 cssModules: true, 25 cssLoaderOptions: { 26 localIdentName: '[local]___[hash:base64:5]', 27 }, 28 [PHASE_PRODUCTION_BUILD + PHASE_EXPORT]: { 29 cssLoaderOptions: { 30 localIdentName: '[hash:base64:8]', 31 }, 32 }, 33 }], 34 35 // add a plugin without a configuration 36 images, 37 38 // another plugin with a configuration (applied in all phases except development server) 39 [typescript, { 40 typescriptLoaderOptions: { 41 transpileOnly: false, 42 }, 43 }, ['!', PHASE_DEVELOPMENT_SERVER]], 44 45 // load and apply a plugin only during development server phase 46 [optional(() => require('@some-internal/dev-log')), [PHASE_DEVELOPMENT_SERVER]], 47 48], nextConfig);
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.
1// next.config.js 2const withSass = require('@zeit/next-sass'); 3const withTypescript = require('@zeit/next-typescript'); 4const withImages = require('next-images'); 5const withOffline = require('next-offline'); 6 7module.exports = withSass(withOffline(withTypescript(withImages({ 8 { 9 cssModules: true, 10 cssLoaderOptions: { 11 importLoaders: 1, 12 localIdentName: '[local]___[hash:base64:5]', 13 }, 14 typescriptLoaderOptions: { 15 transpileOnly: false, 16 }, 17 useFileSystemPublicRoutes: false, 18 distDir: 'build', 19 workerName: 'sw.js', 20 imageTypes: ['jpg', 'png'], 21 } 22}))));
See vercel/next-plugins for a list of official and community made plugins for next.js.
MIT © Cyril Wanner
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/25 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
44 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More