Skip to content

Commit 3daf045

Browse files
cijiugechucsr632
andauthored
feat: expose an option for both cli and configFile to determine whether to enable minifyHtml (#109)
* feat: expose an option for both cli and config * fix: cli flag * improve vite-pages ssr options * make minifyHtml default to true * fix: make `--minifyHtml=false` to be treated as boolean false instead of string "false" Co-authored-by: csr632 <[email protected]>
1 parent fc4bd3f commit 3daf045

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

packages/react-pages/src/node/cli.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import fs from 'fs-extra'
33
import minimist from 'minimist'
44
import path from 'node:path'
55
import { resolveConfig } from 'vite'
6+
import type { InlineConfig } from 'vite'
67
import { PKG_ROOT } from './constants'
78
import { ssrBuild } from './static-site-generation'
9+
import type { staticSiteGenerationConfig } from './types'
810

911
const argv: any = minimist(process.argv.slice(2))
1012

@@ -17,34 +19,46 @@ console.log(
1719
)
1820
// console.log(chalk.cyan(`vite v${require('vite/package.json').version}`))
1921

20-
// cli usage: vite-pages ssr [root] [vite config like --outDir or --configFile]
22+
// cli usage: vite-pages ssr [root] [--minifyHtml] [vite options like: --configFile, --base, --logLevel, --mode, --build.outDir, etc.]
2123
const [command, root] = argv._
2224
if (root) {
2325
argv.root = root
2426
}
2527

28+
// make `--minifyHtml=false` to be treated as boolean false instead of string "false"
29+
Object.entries(argv).forEach(([key, value]) => {
30+
if (value === 'false') argv[key] = false
31+
})
32+
33+
// console.log('@@argv', argv)
2634
;(async () => {
2735
if (!command || command === 'ssr') {
36+
const toBeResovledConfig: InlineConfig = {
37+
...argv,
38+
}
39+
2840
// user can pass in vite config like --outDir or --configFile
2941
const viteConfig = await resolveConfig(
30-
argv,
42+
toBeResovledConfig,
3143
'build',
3244
'production',
3345
'production'
3446
)
3547
const thisPlugin = viteConfig.plugins.find((plugin) => {
3648
return plugin.name === 'vite-plugin-react-pages'
3749
})
38-
// @ts-expect-error
39-
const ssrConfig = thisPlugin?.vitePagesStaticSiteGeneration
50+
//@ts-expect-error
51+
const ssrConfig = thisPlugin?.vitePagesStaticSiteGeneration as
52+
| staticSiteGenerationConfig
53+
| undefined
4054

41-
await ssrBuild(viteConfig, ssrConfig, argv).catch((err: any) => {
55+
await ssrBuild(viteConfig, argv, ssrConfig).catch((err: any) => {
4256
console.error(chalk.red(`ssr error:\n`), err)
4357
process.exit(1)
4458
})
4559
} else {
4660
console.error(
47-
`[vite-pages] Invalid command. CLI usage: vite-pages ssr [root] [vite config like --outDir or --configFile]`
61+
`[vite-pages] Invalid command. CLI usage: vite-pages ssr [root] [--minifyHtml] [vite options like: --configFile, --base, --logLevel, --mode, --build.outDir, etc.]`
4862
)
4963
}
5064
})()

packages/react-pages/src/node/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from 'path'
22
import type { PluggableList } from 'unified'
33
import type { Plugin, IndexHtmlTransformContext } from 'vite'
44
import type { OutputPlugin } from 'rollup'
5+
import type { staticSiteGenerationConfig } from './types'
56

67
import {
78
DefaultPageStrategy,
@@ -52,7 +53,7 @@ export interface PluginConfig {
5253
pagesDir?: string
5354
pageStrategy?: PageStrategy
5455
useHashRouter?: boolean
55-
staticSiteGeneration?: {}
56+
staticSiteGeneration?: staticSiteGenerationConfig
5657
}
5758

5859
function pluginFactory(opts: PluginConfig = {}): Plugin {

packages/react-pages/src/node/static-site-generation/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { pathToFileURL } from 'node:url'
88

99
import { CLIENT_PATH } from '../constants'
1010
import type { SSRPlugin } from '../../../clientTypes'
11+
import type { staticSiteGenerationConfig } from '../types'
1112

1213
const minifyOptions = {
1314
keepClosingSlash: true,
@@ -19,8 +20,8 @@ const minifyOptions = {
1920

2021
export async function ssrBuild(
2122
viteConfig: ResolvedConfig,
22-
ssrConfig: any,
23-
argv: any
23+
argv: any,
24+
ssrConfig?: staticSiteGenerationConfig
2425
) {
2526
// ssr build should not use hash router
2627
// if (viteOptions?.define?.['__HASH_ROUTER__'])
@@ -222,9 +223,13 @@ ${CSSInjectPoint}`
222223
`<script type="module" src="${basePath}${entryChunk.fileName}"></script>`
223224
)
224225

225-
const minifiedHtml = await minify(html, minifyOptions)
226+
const minifyHtml = argv?.minifyHtml ?? ssrConfig?.minifyHtml ?? true
227+
if (minifyHtml) {
228+
const minifiedHtml = await minify(html, minifyOptions)
229+
return minifiedHtml
230+
}
226231

227-
return minifiedHtml
232+
return html
228233
}
229234
}
230235

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface staticSiteGenerationConfig {
2+
/**
3+
* @default true
4+
*/
5+
readonly minifyHtml?: boolean
6+
}

0 commit comments

Comments
 (0)