Skip to content

Commit

Permalink
wrap in v4 tags
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Sep 6, 2021
1 parent be4700d commit 1cc4d4a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
22 changes: 20 additions & 2 deletions packages/gatsby/src/commands/build-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getPageData } from "../utils/get-page-data"

import { Span } from "opentracing"
import { IProgram, Stage } from "./types"
import { ROUTES_DIRECTORY } from "../constants"
import { PackageJson } from "../.."
import type { GatsbyWorkerPool } from "../utils/worker/pool"
import { IPageDataWithQueryResult } from "../utils/page-data"
Expand Down Expand Up @@ -125,7 +126,7 @@ const runWebpack = (
} = require(`../utils/dev-ssr/render-dev-html`)
// Make sure we use the latest version during development
if (oldHash !== `` && newHash !== oldHash) {
restartWorker(`${directory}/.cache/_routes/render-page.js`)
restartWorker(`${directory}/${ROUTES_DIRECTORY}}render-page.js`)
}

oldHash = newHash
Expand Down Expand Up @@ -168,7 +169,7 @@ const doBuildRenderer = async (

// render-page.js is hard coded in webpack.config
return {
rendererPath: `${directory}/.cache/_routes/render-page.js`,
rendererPath: `${directory}/${ROUTES_DIRECTORY}render-page.js`,
waitForCompilerClose,
}
}
Expand All @@ -186,6 +187,15 @@ export const buildRenderer = async (
return doBuildRenderer(program, config, stage, parentSpan)
}

// TODO remove after v4 release and update cloud internals
export const deleteRenderer = async (rendererPath: string): Promise<void> => {
try {
await fs.remove(rendererPath)
await fs.remove(`${rendererPath}.map`)
} catch (e) {
// This function will fail on Windows with no further consequences.
}
}
export interface IRenderHtmlResult {
unsafeBuiltinsUsageByPagePath: Record<string, Array<string>>
}
Expand Down Expand Up @@ -433,6 +443,14 @@ export async function buildHTMLPagesAndDeleteStaleArtifacts({
reporter.info(`There are no new or changed html files to build.`)
}

if (_CFLAGS_.GATSBY_MAJOR !== `4` && !program.keepPageRenderer) {
try {
await deleteRenderer(pageRenderer)
} catch (err) {
// pass through
}
}

if (toDelete.length > 0) {
const publicDir = path.join(program.directory, `public`)
const deletePageDataActivityTimer = reporter.activityTimer(
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ROUTES_DIRECTORY =
_CFLAGS_.GATSBY_MAJOR === `4` ? `.cache/_routes/` : `public/`
12 changes: 12 additions & 0 deletions packages/gatsby/src/utils/babel-loader-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ const prepareOptions = (babel, options = {}, resolve = require.resolve) => {
]
const requiredPresets = []

// Stage specific plugins to add
if (
_CFLAGS_.GATSBY_MAJOR !== `4` &&
(stage === `build-html` || stage === `develop-html`)
) {
requiredPlugins.push(
babel.createConfigItem([resolve(`babel-plugin-dynamic-import-node`)], {
type: `plugin`,
})
)
}

if (stage === `develop`) {
requiredPlugins.push(
babel.createConfigItem([resolve(`react-refresh/babel`)], {
Expand Down
8 changes: 5 additions & 3 deletions packages/gatsby/src/utils/dev-ssr/render-dev-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import nodePath from "path"
import report from "gatsby-cli/lib/reporter"
import { isCI } from "gatsby-core-utils"
import { Stats } from "webpack"
import { ROUTES_DIRECTORY } from "../../constants"
import { startListener } from "../../bootstrap/requires-writer"
import { findPageByPath } from "../find-page-by-path"
import { getPageData as getPageDataExperimental } from "../get-page-data"
Expand Down Expand Up @@ -112,10 +113,11 @@ const ensurePathComponentInSSRBundle = async (
report.panic(`page not found`, page)
}

// Now check if it's written to .cache/_routes/render-page.js
// Now check if it's written to the correct path
const htmlComponentRendererPath = nodePath.join(
directory,
`.cache/_routes/render-page.js`
ROUTES_DIRECTORY,
`render-page.js`
)

// This search takes 1-10ms
Expand Down Expand Up @@ -232,7 +234,7 @@ export const renderDevHTML = ({
})
}

// Wait for .cache/_routes/render-page.js to update w/ the page component.
// Wait for html-renderer to update w/ the page component.
const found = await ensurePathComponentInSSRBundle(pageObj, directory)

// If we can't find the page, just force set isClientOnlyPage
Expand Down
5 changes: 3 additions & 2 deletions packages/gatsby/src/utils/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
writeVirtualLoadingIndicatorModule,
} from "./loading-indicator"
import { renderDevHTML } from "./dev-ssr/render-dev-html"
import { ROUTES_DIRECTORY } from "../constants"

type ActivityTracker = any // TODO: Replace this with proper type once reporter is typed

Expand Down Expand Up @@ -517,7 +518,7 @@ module.exports = {
page: pathObj,
skipSsr: req.query[`skip-ssr`] || false,
store,
htmlComponentRendererPath: `${program.directory}/.cache/_routes/render-page.js`,
htmlComponentRendererPath: `${program.directory}/${ROUTES_DIRECTORY}render-page.js`,
directory: program.directory,
})
res.status(200).send(renderResponse)
Expand Down Expand Up @@ -586,7 +587,7 @@ module.exports = {
skipSsr: true,
store,
error: message,
htmlComponentRendererPath: `${program.directory}/.cache/_routes/render-page.js`,
htmlComponentRendererPath: `${program.directory}/${ROUTES_DIRECTORY}render-page.js`,
directory: program.directory,
})

Expand Down

0 comments on commit 1cc4d4a

Please sign in to comment.