Skip to content

Commit

Permalink
don't write new (a)sync-requires.js if components didn't change (#4759)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored and KyleAMathews committed Mar 29, 2018
1 parent 20252dc commit 44797c3
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/gatsby/src/internal-plugins/query-runner/pages-writer.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
const _ = require(`lodash`)
const fs = require(`fs-extra`)
const crypto = require(`crypto`)

const { store, emitter } = require(`../../redux/`)

import { joinPath } from "../../utils/path"

const getLayoutById = layouts => id => layouts.find(l => l.id === id)

let lastHash = null

// Write out pages information.
const writePages = async () => {
bootstrapFinished = true
let { program, jsonDataPaths, pages, layouts } = store.getState()

const pagesComponentDependencies = {}

// Write out pages.json
const pagesData = pages.reduce(
(mem, { path, matchPath, componentChunkName, layout, jsonName }) => {
const layoutOjb = getLayoutById(layouts)(layout)

const pageComponentsChunkNames = {
componentChunkName,
layoutComponentChunkName: layoutOjb && layoutOjb.componentChunkName,
}

if (program._[0] === `develop`) {
pagesComponentDependencies[path] = pageComponentsChunkNames
}

return [
...mem,
{
componentChunkName,
...pageComponentsChunkNames,
layout: layoutOjb ? layoutOjb.machineId : layout,
layoutComponentChunkName: layoutOjb && layoutOjb.componentChunkName,
jsonName,
path,
matchPath,
Expand All @@ -30,6 +45,18 @@ const writePages = async () => {
[]
)

const newHash = crypto
.createHash(`md5`)
.update(JSON.stringify(pagesComponentDependencies))
.digest(`hex`)

if (newHash === lastHash) {
// components didn't change - no need to rewrite pages.json
return Promise.resolve()
}

lastHash = newHash

// Get list of components, layouts, and json files.
let components = []
let json = []
Expand Down

0 comments on commit 44797c3

Please sign in to comment.