-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[site/content/notes][xs]: note on md hot reload.
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# How it isimplemented in contentlayer | ||
|
||
## next-contentlayer | ||
|
||
packages/next-contentlayer/src/index.ts | ||
```js | ||
import { type NextPluginOptions, runBeforeWebpackCompile } from './plugin.js' | ||
|
||
export const defaultPluginOptions: NextPluginOptions = {} | ||
|
||
|
||
export const createContentlayerPlugin = | ||
(pluginOptions: NextPluginOptions = defaultPluginOptions) => | ||
(nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => { | ||
return { | ||
...nextConfig, | ||
// ... | ||
webpack(config: webpack.Configuration, options: any) { | ||
config.watchOptions = { | ||
...config.watchOptions, | ||
ignored: ['**/node_modules/!(.contentlayer)/**/*'], | ||
} | ||
|
||
config.plugins!.push(new ContentlayerWebpackPlugin(pluginOptions)) | ||
|
||
// ... | ||
return config | ||
}, | ||
} | ||
} | ||
|
||
// This is what we used to import and use in next.config.js | ||
export const withContentlayer = createContentlayerPlugin(defaultPluginOptions) | ||
|
||
class ContentlayerWebpackPlugin { | ||
constructor(readonly pluginOptions: NextPluginOptions) {} | ||
|
||
apply(compiler: webpack.Compiler) { | ||
compiler.hooks.beforeCompile.tapPromise('ContentlayerWebpackPlugin', async () => { | ||
await runBeforeWebpackCompile({ | ||
pluginOptions: this.pluginOptions, | ||
devServerStartedRef, | ||
// this will be `development` when running `next dev` | ||
mode: compiler.options.mode, | ||
}) | ||
}) | ||
} | ||
} | ||
``` | ||
|
||
packages/next-contentlayer/src/plugin.ts | ||
```js | ||
export const runBeforeWebpackCompile = async ({ | ||
mode, | ||
pluginOptions, | ||
devServerStartedRef, | ||
}: { | ||
mode: WebpackOptionsNormalized['mode'] | ||
pluginOptions: NextPluginOptions | ||
devServerStartedRef: { current: boolean } | ||
}) => { | ||
const isNextDev = mode === 'development' | ||
const isBuild = mode === 'production' | ||
|
||
const { configPath } = pluginOptions | ||
|
||
if (isBuild) { | ||
checkConstraints() | ||
await runContentlayerBuild({ configPath }) | ||
} else if (isNextDev && !devServerStartedRef.current) { | ||
devServerStartedRef.current = true | ||
runContentlayerDev({ configPath }) | ||
} | ||
} | ||
|
||
const runContentlayerDev = async ({ configPath }: NextPluginOptions) => { | ||
if (contentlayerInitialized) return | ||
contentlayerInitialized = true | ||
|
||
await pipe( | ||
core.getConfigWatch({ configPath }), | ||
S.tapSkipFirstRight(() => T.log(`Contentlayer config change detected. Updating type definitions and data...`)), | ||
S.tapRight((config) => (config.source.options.disableImportAliasWarning ? T.unit : T.fork(core.validateTsconfig))), | ||
S.chainSwitchMapEitherRight((config) => core.generateDotpkgStream({ config, verbose: false, isDev: true })), | ||
S.tap(E.fold((error) => T.log(errorToString(error)), core.logGenerateInfo)), | ||
S.runDrain, | ||
runMain, | ||
) | ||
} | ||
|
||
const runMain = core.runMain({ tracingServiceName: 'next-contentlayer', verbose: process.env.CL_DEBUG !== undefined }) | ||
|
||
``` | ||
|