Skip to content

Commit

Permalink
chore(requires-writer): use virtual modules instead of fs
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 2, 2020
1 parent 673dc2b commit 1425b55
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/__tests__/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jest.mock(`gatsby/package.json`, () => {
})

jest.mock(
`../sync-requires`,
`$virtual/sync-requires`,
() => {
return {
components: {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/cache-dir/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import emitter from "./emitter"
import { apiRunner, apiRunnerAsync } from "./api-runner-browser"
import { setLoader, publicLoader } from "./loader"
import DevLoader from "./dev-loader"
import syncRequires from "./sync-requires"
import syncRequires from "$virtual/sync-requires"
// Generated during bootstrap
import matchPaths from "./match-paths.json"
import matchPaths from "$virtual/match-paths.json"

window.___emitter = emitter

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "./navigation"
import emitter from "./emitter"
import PageRenderer from "./page-renderer"
import asyncRequires from "./async-requires"
import asyncRequires from "$virtual/async-requires"
import {
setLoader,
ProdLoader,
Expand All @@ -22,7 +22,7 @@ import EnsureResources from "./ensure-resources"
import stripPrefix from "./strip-prefix"

// Generated during bootstrap
import matchPaths from "./match-paths.json"
import matchPaths from "$virtual/match-paths.json"

const loader = new ProdLoader(asyncRequires, matchPaths)
setLoader(loader)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {

const { RouteAnnouncerProps } = require(`./route-announcer-props`)
const apiRunner = require(`./api-runner-ssr`)
const syncRequires = require(`./sync-requires`)
const syncRequires = require(`$virtual/sync-requires`)
const { version: gatsbyVersion } = require(`gatsby/package.json`)

const stats = JSON.parse(
Expand Down
28 changes: 23 additions & 5 deletions packages/gatsby/src/bootstrap/requires-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { match } from "@reach/router/lib/utils"
import { joinPath } from "gatsby-core-utils"
import { store, emitter } from "../redux/"
import { IGatsbyState, IGatsbyPage } from "../redux/types"
import { writeModule } from "../utils/gatsby-webpack-virtual-modules"
import { markWebpackStatusAsPending } from "../utils/webpack-status"

interface IGatsbyPageComponent {
Expand Down Expand Up @@ -212,7 +213,7 @@ const preferDefault = m => m && m.default || m
.map((c: IGatsbyPageComponent): string => {
// we need a relative import path to keep contenthash the same if directory changes
const relativeComponentPath = path.relative(
path.join(program.directory, `.cache`),
path.join(program.directory, `node_modules`, `$virtual`),
c.component
)
Expand All @@ -223,7 +224,16 @@ const preferDefault = m => m && m.default || m
.join(`,\n`)}
}\n\n`

const writeAndMove = (file: string, data: string): Promise<void> => {
const writeAndMove = (
virtualFilePath: string,
file: string,
data: string
): Promise<void> => {
writeModule(virtualFilePath, data)

// files in .cache are not used anymore as part of webpack builds, but
// still can be used by other tools (for example `gatsby serve` reads
// `match-paths.json` to setup routing)
const destination = joinPath(program.directory, `.cache`, file)
const tmp = `${destination}.${Date.now()}`
return fs
Expand All @@ -232,9 +242,17 @@ const preferDefault = m => m && m.default || m
}

await Promise.all([
writeAndMove(`sync-requires.js`, syncRequires),
writeAndMove(`async-requires.js`, asyncRequires),
writeAndMove(`match-paths.json`, JSON.stringify(matchPaths, null, 4)),
writeAndMove(`$virtual/sync-requires.js`, `sync-requires.js`, syncRequires),
writeAndMove(
`$virtual/async-requires.js`,
`async-requires.js`,
asyncRequires
),
writeAndMove(
`$virtual/match-paths.json`,
`match-paths.json`,
JSON.stringify(matchPaths, null, 4)
),
])

return true
Expand Down

0 comments on commit 1425b55

Please sign in to comment.