From 4156e94b60db9dd603add0289f4d6a06d3be7421 Mon Sep 17 00:00:00 2001 From: Johannes Schickling Date: Mon, 28 Mar 2022 15:22:10 +0200 Subject: [PATCH] next-contentlayer: BREAKING Change withContentlayer API and introduce createContentlayerPlugin #140 --- packages/next-contentlayer/src/index-cjs.ts | 39 +++++++++++++++++++-- packages/next-contentlayer/src/index.ts | 36 ++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/packages/next-contentlayer/src/index-cjs.ts b/packages/next-contentlayer/src/index-cjs.ts index 1bd993d9..f79b21da 100644 --- a/packages/next-contentlayer/src/index-cjs.ts +++ b/packages/next-contentlayer/src/index-cjs.ts @@ -2,11 +2,29 @@ import type { NextConfig } from 'next' export type { NextConfig } -type PluginOptions = {} +export type PluginOptions = {} let devServerStarted = false -module.exports.withContentlayer = +const defaultPluginOptions: PluginOptions = {} +module.exports.defaultPluginOptions = defaultPluginOptions + +/** + * This function allows you to provide custom plugin options (currently there are none however). + * + * @example + * ```js + * // next.config.mjs + * import { createContentlayerPlugin } from 'next-contentlayer' + * + * const withContentlayer = createContentlayerPlugin() + * + * export default withContentlayer({ + * // My Next.js config + * }) + * ``` + */ +module.exports.createContentlayerPlugin = (_pluginOptions: PluginOptions = {}) => (nextConfig: Partial = {}): Partial => { // could be either `next dev` or just `next` @@ -59,3 +77,20 @@ module.exports.withContentlayer = }, } } + +/** + * Next.js plugin for Contentlayer with default options. + * + * If you want to provide custom plugin options, please use {@link createContentlayerPlugin} instead. + * + * @example + * ```js + * // next.config.mjs + * import { withContentlayer } from 'next-contentlayer' + * + * export default withContentlayer({ + * // My Next.js config + * }) + * ``` + */ +module.exports.withContentlayer = module.exports.createContentlayerPlugin(defaultPluginOptions) diff --git a/packages/next-contentlayer/src/index.ts b/packages/next-contentlayer/src/index.ts index 534f346d..b90c750d 100644 --- a/packages/next-contentlayer/src/index.ts +++ b/packages/next-contentlayer/src/index.ts @@ -8,7 +8,24 @@ export type PluginOptions = {} let devServerStarted = false -export const withContentlayer = +export const defaultPluginOptions: PluginOptions = {} + +/** + * This function allows you to provide custom plugin options (currently there are none however). + * + * @example + * ```js + * // next.config.mjs + * import { createContentlayerPlugin } from 'next-contentlayer' + * + * const withContentlayer = createContentlayerPlugin() + * + * export default withContentlayer({ + * // My Next.js config + * }) + * ``` + */ +export const createContentlayerPlugin = (_pluginOptions: PluginOptions = {}) => (nextConfig: Partial = {}): Partial => { // could be either `next dev` or just `next` @@ -59,3 +76,20 @@ export const withContentlayer = }, } } + +/** + * Next.js plugin for Contentlayer with default options. + * + * If you want to provide custom plugin options, please use {@link createContentlayerPlugin} instead. + * + * @example + * ```js + * // next.config.mjs + * import { withContentlayer } from 'next-contentlayer' + * + * export default withContentlayer({ + * // My Next.js config + * }) + * ``` + */ +export const withContentlayer = createContentlayerPlugin(defaultPluginOptions)