diff --git a/packages/nextjs/README.md b/packages/nextjs/README.md index 259f424..e3fe20a 100644 --- a/packages/nextjs/README.md +++ b/packages/nextjs/README.md @@ -223,7 +223,11 @@ const config = { // 作为示例,比如你本地编写的 postcss 插件目录 extraFiles: [ 'postcss-plugins/**' - ] + ], + // 自定义 Document 模板,默认为插件内置的模板 + // 如果你需要更改内置模板上的脚本或样式,可以使用该选项 + // 参见:https://www.nextjs.cn/docs/advanced-features/custom-document + customDocumentPath: 'src/custom-document.jsx' }] ] } diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts index 5f2bdb8..563640e 100644 --- a/packages/nextjs/src/index.ts +++ b/packages/nextjs/src/index.ts @@ -48,6 +48,10 @@ interface PluginOptions { * 指定构建时还需要包含的其他文件 */ extraFiles?: string[] + /** + * 自定义 Document 模板 + */ + customDocumentPath: string } export default (ctx: IPluginContext, pluginOpts: PluginOptions) => { @@ -57,6 +61,7 @@ export default (ctx: IPluginContext, pluginOpts: PluginOptions) => { const runNextjs = pluginOpts.runNextjs ?? true const browser = pluginOpts.browser ?? true const extraFiles = pluginOpts.extraFiles ?? [] + const customDocumentPath = pluginOpts.customDocumentPath ?? '' ctx.registerCommand({ name: 'start', @@ -260,6 +265,11 @@ export default (ctx: IPluginContext, pluginOpts: PluginOptions) => { })) .pipe(rename('_app.jsx')) .pipe(dest(path.join(outputPath, 'pages'))), + customDocumentPath ? + src(path.join(appPath, customDocumentPath)).pipe(rename((pathInfo) => { + pathInfo.basename = '_document'; + return pathInfo; + })).pipe(dest(path.join(outputPath, 'pages'))) : src(`${templateDir}/pages/_document.jsx`).pipe(dest(path.join(outputPath, 'pages'))), src(`${appPath}/config/**`).pipe(dest(path.join(outputPath, 'config'))), src(`${templateDir}/next.config.ejs`)