Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom jsx document template #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}]
]
}
Expand Down
10 changes: 10 additions & 0 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ interface PluginOptions {
* 指定构建时还需要包含的其他文件
*/
extraFiles?: string[]
/**
* 自定义 Document 模板
*/
customDocumentPath: string
}

export default (ctx: IPluginContext, pluginOpts: PluginOptions) => {
Expand All @@ -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',
Expand Down Expand Up @@ -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`)
Expand Down