Skip to content

Commit

Permalink
fix: ensure stable pages entry order across builds
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 16, 2021
1 parent f320418 commit 929bcf5
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/node/config.ts
Expand Up @@ -16,6 +16,7 @@ import {
} from './shared'
import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias'
import { MarkdownOptions } from './markdown/markdown'
import { createHash } from 'crypto'

export { resolveSiteDataByRoute } from './shared'

Expand Down Expand Up @@ -87,15 +88,32 @@ export async function resolveConfig(
? userThemeDir
: DEFAULT_THEME_PATH

// Important: globby/fast-glob doesn't guarantee order of the returned files.
// We must sort the pages so the input list to rollup is stable across
// builds - otherwise different input order could result in different exports
// order in shared chunks which in turns invalidates the hash of every chunk!
// JavaScript built-in sort() is mandated to be stable as of ES2019 and
// supported in Node 12+, which is required by Vite.
const pages = (
await globby(['**.md'], {
cwd: srcDir,
ignore: ['**/node_modules', ...(userConfig.srcExclude || [])]
})
).sort()

const hash = createHash('sha256')
.update(pages.join(','))
.digest('hex')
.slice(0, 8)

console.log(hash)

const config: SiteConfig = {
root,
srcDir,
site,
themeDir,
pages: await globby(['**.md'], {
cwd: srcDir,
ignore: ['**/node_modules', ...(userConfig.srcExclude || [])]
}),
pages,
configPath: resolve(root, 'config.js'),
outDir: resolve(root, 'dist'),
tempDir: path.resolve(APP_PATH, 'temp'),
Expand Down

0 comments on commit 929bcf5

Please sign in to comment.