Skip to content

Commit

Permalink
Update readme for version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilwanner committed Apr 15, 2018
1 parent 7f4e113 commit 6b8779c
Showing 1 changed file with 230 additions and 6 deletions.
236 changes: 230 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Provides a cleaner API for enabling and configuring plugins for [next.js](https:
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.

`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.
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](#installation)
- [Usage](#usage)
- [Example](#example)
- [Plugin developers](#plugin-developers)
- [Examples](#examples)
- [See also](#see-also)
- [License](#license)

Expand All @@ -21,6 +22,9 @@ This can also lead to orphaned configuration values when updating or removing pl
npm install --save next-compose-plugins
```

This plugin requires next.js `>= 5.1` because it depends on the phases introduced within this version.
If you are still on `5.0.x`, you can install v1 of this plugin: `npm install --save next-compose-plugins@1`.

## Usage
```javascript
// next.config.js
Expand All @@ -31,15 +35,178 @@ module.exports = withPlugins([...plugins], nextConfiguration);

### `plugins`

Is an array containing all plugins and their configuration.
> See the [examples](#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, you can specify an array where the first element is the plugin and the second its configuration: `[sass, {mySassConfig: 'foobar'}]` (see [example](#example)).
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](#optional-plugins) section if you only want to require a plugin when it is really used.

```javascript
const withPlugins = require('next-compose-plugins');
const sass = require('@zeit/next-sass');

module.exports = withPlugins([
[sass],
]);
```

##### `configuration?: object`

Configuration for the plugin.

You can also overwrite specific configuration keys for a phase:

```javascript
const withPlugins = require('next-compose-plugins');
const { PHASE_PRODUCTION_BUILD } = require('next/constants');
const sass = require('@zeit/next-sass');

module.exports = withPlugins([
[sass, {
cssModules: true,
cssLoaderOptions: {
localIdentName: '[path]___[local]___[hash:base64:5]',
},
[PHASE_PRODUCTION_BUILD]: {
cssLoaderOptions: {
localIdentName: '[hash:base64:8]',
},
},
}],
]);
```

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](https://github.com/zeit/next.js/blob/canary/lib/constants.js).

##### `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](https://github.com/zeit/next.js/blob/canary/lib/constants.js).

```javascript
const withPlugins = require('next-compose-plugins');
const { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } = require('next/constants');
const sass = require('@zeit/next-sass');

module.exports = withPlugins([
[sass, {
cssModules: true,
cssLoaderOptions: {
localIdentName: '[path]___[local]___[hash:base64:5]',
},
}, [PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD]],
]);
```

You can also negate the phases with a leading `!`:

```javascript
const withPlugins = require('next-compose-plugins');
const { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } = require('next/constants');
const sass = require('@zeit/next-sass');

module.exports = withPlugins([
[sass, {
cssModules: true,
cssLoaderOptions: {
localIdentName: '[path]___[local]___[hash:base64:5]',
},
}, ['!', PHASE_DEVELOPMENT_SERVER]],
]);
```

This will apply the plugin in all phases except `PHASE_DEVELOPMENT_SERVER`.

### `nextConfiguration`

Any direct [next.js configuration](https://github.com/zeit/next.js#custom-configuration) can go here, for example: `{distDir: 'dist'}`.

## Example
### Optional plugins

If a plugin should only get loaded when it is used, you can use the `optional` helper function.
This can be useful if the plugin is only in the `devDependencies` and so you only want to apply it during `PHASE_DEVELOPMENT_SERVER`.
If you don't use the `optional` helper in this case, you would get an error.

```javascript
const { withPlugins, optional } = require('next-compose-plugins');
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');

module.exports = withPlugins([
[optional(() => require('@zeit/next-sass')), { /* optional configuration */ }, [PHASE_DEVELOPMENT_SERVER]],
]);
```

## 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:

```javascript
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');

module.exports = (nextConfig = {}) => {
return Object.assign({}, nextConfig, {
// define in which phases this plugin should get applied.
// you can also use multiple phases or negate them.
// however, users can still overwrite them in their configuration if they really want to.
phases: [PHASE_DEVELOPMENT_SERVER],

webpack(config, options) {
// do something here which only gets applied during development server phase

if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options);
}

return config;
},
};
};
```
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](#phases-array) 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:
```javascript
module.exports = (nextConfig = {}, nextComposePlugins = {}) => {
console.log(nextComposePlugins);
};
```
Currently, it contains these values:
```javascript
{
// 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
### Basic example
```javascript
// next.config.js
Expand Down Expand Up @@ -78,7 +245,64 @@ module.exports = withPlugins([
], 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:
### Advanced example
```javascript
// next.config.js
const { withPlugins, optional } = require('next-compose-plugins');
const images = require('next-images');
const sass = require('@zeit/next-sass');
const typescript = require('@zeit/next-typescript');

const {
PHASE_PRODUCTION_BUILD,
PHASE_PRODUCTION_SERVER,
PHASE_DEVELOPMENT_SERVER,
PHASE_EXPORT,
} = require('next/constants');

// next.js configuration
const nextConfig = {
useFileSystemPublicRoutes: false,
distDir: 'build',
};

module.exports = withPlugins([

// add a plugin with specific configuration
[sass, {
cssModules: true,
cssLoaderOptions: {
importLoaders: 1,
localIdentName: '[local]___[hash:base64:5]',
},
[PHASE_PRODUCTION_BUILD + PHASE_EXPORT]: {
cssLoaderOptions: {
importLoaders: 1,
localIdentName: '[hash:base64:8]',
},
},
}],

// add a plugin without a configuration
images,

// another plugin with a configuration (applied in all phases except development server)
[typescript, {
typescriptLoaderOptions: {
transpileOnly: false,
},
}, ['!', PHASE_DEVELOPMENT_SERVER]],

// load and apply a plugin only during development server phase
[optional(() => require('@some-internal/dev-log')), [PHASE_DEVELOPMENT_SERVER]],

], 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.
```javascript
// next.config.js
Expand Down

0 comments on commit 6b8779c

Please sign in to comment.