💡next-compose-plugins provides a cleaner API for enabling and configuring plugins for next.js
Installations
npm install next-compose-plugins
Developer
cyrilwanner
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
12.16.3
NPM Version
6.14.4
Statistics
739 Stars
43 Commits
12 Forks
6 Watching
13 Branches
2 Contributors
Updated on 20 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
89,101,397
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
:bulb: 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
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};
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.
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]);
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.
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]);
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:
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.
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:
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}
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
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);
Advanced example
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);
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.
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 also
See vercel/next-plugins for a list of official and community made plugins for next.js.
License
MIT © Cyril Wanner
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Reason
44 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2
/10
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