forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move query/pages-writer to bootstrap/requires-writer (gatsbyjs#13729)
And delete data.json
- Loading branch information
Showing
7 changed files
with
177 additions
and
318 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/gatsby/src/bootstrap/__tests__/requires-writer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { joinPath } = require(`../../utils/path`) | ||
const requiresWriter = require(`../requires-writer`) | ||
|
||
const now = Date.now() | ||
|
||
const newMockState = () => { | ||
const pages = new Map() | ||
pages.set(`path1`, { | ||
component: `component1`, | ||
componentChunkName: `chunkName1`, | ||
matchPath: `matchPath1`, | ||
path: `/path1`, | ||
}) | ||
pages.set(`path2`, { | ||
component: `component2`, | ||
componentChunkName: `chunkName2`, | ||
path: `/path2`, | ||
}) | ||
const program = { directory: `/dir` } | ||
return { pages, program } | ||
} | ||
|
||
jest.mock(`fs-extra`, () => { | ||
return { | ||
writeFile: () => Promise.resolve(), | ||
move: () => {}, | ||
} | ||
}) | ||
|
||
const mockFsExtra = require(`fs-extra`) | ||
|
||
describe(`requires-writer`, () => { | ||
beforeEach(() => { | ||
global.Date.now = () => now | ||
requiresWriter.resetLastHash() | ||
}) | ||
describe(`writeAll`, () => { | ||
it(`writes requires files`, async () => { | ||
const spy = jest.spyOn(mockFsExtra, `writeFile`) | ||
await requiresWriter.writeAll(newMockState()) | ||
expect(spy).toBeCalledWith( | ||
joinPath(`/dir`, `.cache`, `match-paths.json.${now}`), | ||
JSON.stringify([{ path: `/path1`, matchPath: `matchPath1` }], null, 4) | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const _ = require(`lodash`) | ||
const fs = require(`fs-extra`) | ||
const crypto = require(`crypto`) | ||
const { store, emitter } = require(`../redux/`) | ||
import { joinPath } from "../utils/path" | ||
|
||
let lastHash = null | ||
|
||
const resetLastHash = () => { | ||
lastHash = null | ||
} | ||
|
||
const pickComponentFields = page => | ||
_.pick(page, [`component`, `componentChunkName`]) | ||
|
||
const getComponents = pages => | ||
_(pages) | ||
.map(pickComponentFields) | ||
.uniqBy(c => c.componentChunkName) | ||
.value() | ||
|
||
const pickMatchPathFields = page => _.pick(page, [`path`, `matchPath`]) | ||
|
||
const getMatchPaths = pages => | ||
pages.filter(page => page.matchPath).map(pickMatchPathFields) | ||
|
||
const createHash = (matchPaths, components) => | ||
crypto | ||
.createHash(`md5`) | ||
.update(JSON.stringify({ matchPaths, components })) | ||
.digest(`hex`) | ||
|
||
// Write out pages information. | ||
const writeAll = async state => { | ||
const { program } = state | ||
const pages = [...state.pages.values()] | ||
const matchPaths = getMatchPaths(pages) | ||
const components = getComponents(pages) | ||
|
||
const newHash = createHash(matchPaths, components) | ||
|
||
if (newHash === lastHash) { | ||
// Nothing changed. No need to rewrite files | ||
return Promise.resolve() | ||
} | ||
|
||
lastHash = newHash | ||
|
||
// Create file with sync requires of components/json files. | ||
let syncRequires = `const { hot } = require("react-hot-loader/root") | ||
// prefer default export if available | ||
const preferDefault = m => m && m.default || m | ||
\n\n` | ||
syncRequires += `exports.components = {\n${components | ||
.map( | ||
c => | ||
` "${c.componentChunkName}": hot(preferDefault(require("${joinPath( | ||
c.component | ||
)}")))` | ||
) | ||
.join(`,\n`)} | ||
}\n\n` | ||
|
||
// Create file with async requires of components/json files. | ||
let asyncRequires = `// prefer default export if available | ||
const preferDefault = m => m && m.default || m | ||
\n` | ||
asyncRequires += `exports.components = {\n${components | ||
.map( | ||
c => | ||
` "${c.componentChunkName}": () => import("${joinPath( | ||
c.component | ||
)}" /* webpackChunkName: "${c.componentChunkName}" */)` | ||
) | ||
.join(`,\n`)} | ||
}\n\n` | ||
|
||
const writeAndMove = (file, data) => { | ||
const destination = joinPath(program.directory, `.cache`, file) | ||
const tmp = `${destination}.${Date.now()}` | ||
return fs | ||
.writeFile(tmp, data) | ||
.then(() => fs.move(tmp, destination, { overwrite: true })) | ||
} | ||
|
||
const result = await Promise.all([ | ||
writeAndMove(`sync-requires.js`, syncRequires), | ||
writeAndMove(`async-requires.js`, asyncRequires), | ||
writeAndMove(`match-paths.json`, JSON.stringify(matchPaths, null, 4)), | ||
]) | ||
|
||
return result | ||
} | ||
|
||
const debouncedWriteAll = _.debounce(() => writeAll(store.getState()), 500, { | ||
leading: true, | ||
}) | ||
|
||
/** | ||
* Start listening to CREATE/DELETE_PAGE events so we can rewrite | ||
* files as required | ||
*/ | ||
const startListener = () => { | ||
emitter.on(`CREATE_PAGE`, () => { | ||
debouncedWriteAll() | ||
}) | ||
|
||
emitter.on(`CREATE_PAGE_END`, () => { | ||
debouncedWriteAll() | ||
}) | ||
|
||
emitter.on(`DELETE_PAGE`, () => { | ||
debouncedWriteAll() | ||
}) | ||
|
||
emitter.on(`DELETE_PAGE_BY_PATH`, () => { | ||
debouncedWriteAll() | ||
}) | ||
} | ||
|
||
module.exports = { | ||
writeAll, | ||
resetLastHash, | ||
startListener, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.