Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-core-utils): Move page-data & HTML utils to package #32861

Merged
merged 16 commits into from
Aug 23, 2021
Merged
17 changes: 13 additions & 4 deletions e2e-tests/development-runtime/cypress/plugins/block-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const fs = require(`fs-extra`)
const path = require(`path`)
const glob = require(`glob`)
const { fixedPagePath } = require(`gatsby-core-utils`)

const siteDir = path.join(__dirname, `..`, `..`)
const srcDir = path.join(siteDir, `src`)
Expand Down Expand Up @@ -32,13 +33,21 @@ const restoreAsset = hiddenPath => {
}

const getPageDataPath = pagePath => {
const fixedPagePath = pagePath === `/` ? `index` : pagePath
return path.join(publicDir, `page-data`, fixedPagePath, `page-data.json`)
return path.join(
publicDir,
`page-data`,
fixedPagePath(pagePath),
`page-data.json`
)
}

const getHiddenPageDataPath = pagePath => {
const fixedPagePath = pagePath === `/` ? `index` : pagePath
return path.join(publicDir, `page-data`, fixedPagePath, `_page-data.json`)
return path.join(
publicDir,
`page-data`,
fixedPagePath(pagePath),
`_page-data.json`
)
}

const blockPageData = pagePath =>
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/development-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"cross-env": "^5.2.0",
"cypress": "6.1.0",
"fs-extra": "^7.0.1",
"gatsby-core-utils": "^2.12.0",
"gatsby-cypress": "^0.1.7",
"is-ci": "^2.0.0",
"prettier": "2.0.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require(`fs-extra`)
const path = require(`path`)

const { fixedPagePath } = require(`gatsby-core-utils`)
const publicDir = path.join(__dirname, `..`, `..`, `public`)

function getAssetManifest() {
Expand All @@ -9,8 +9,7 @@ function getAssetManifest() {
}

function getPageDataPath(pagePath) {
const fixedPagePath = pagePath === `/` ? `index` : pagePath
return path.posix.join(`page-data`, fixedPagePath, `page-data.json`)
return path.posix.join(`page-data`, fixedPagePath(pagePath), `page-data.json`)
}

const filterAssets = (assetsForPath, filter) => {
Expand Down
3 changes: 2 additions & 1 deletion e2e-tests/production-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"gatsby-plugin-sass": "^4.1.0-next.2",
"gatsby-plugin-sharp": "^3.0.0-next.5",
"gatsby-plugin-stylus": "^3.1.0-next.2",
"gatsby-source-filesystem": "^3.3.0",
"gatsby-seo": "^0.1.0",
"gatsby-source-filesystem": "^3.3.0",
"glob": "^7.1.3",
"react": "^16.9.0",
"react-dom": "^16.9.0",
Expand Down Expand Up @@ -51,6 +51,7 @@
"devDependencies": {
"cross-env": "^5.2.0",
"fs-extra": "^7.0.1",
"gatsby-core-utils": "^2.12.0",
"is-ci": "^2.0.0",
"prettier": "2.0.4",
"start-server-and-test": "^1.7.1"
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
"tailwindcss": "1"
},
"devDependencies": {
"cheerio": "^1.0.0-rc.9",
"cross-env": "^5.0.2",
"fs-extra": "^9.0.0",
"gatsby-core-utils": "^2.12.0",
"jest": "^24.0.0",
"jest-diff": "^24.0.0",
"jest-serializer-path": "^0.1.15",
"npm-run-all": "4.1.5",
"start-server-and-test": "^1.11.3",
"node-fetch": "^2.6.1",
"npm-run-all": "4.1.5",
"prettier": "^2.3.1",
"cheerio": "^1.0.0-rc.9",
"start-server-and-test": "^1.11.3",
"strip-ansi": "^6.0.0"
},
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/ssr/test-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// - start the develop server
// - run this script
async function run() {
const { getPageHtmlFilePath } = require(`gatsby/dist/utils/page-html`)
const { generateHtmlPath } = require(`gatsby-core-utils`)
const { join } = require(`path`)
const fs = require(`fs-extra`)
const fetch = require(`node-fetch`)
Expand Down Expand Up @@ -38,7 +38,7 @@ async function run() {
const builtHtml = format(
filterHtml(
fs.readFileSync(
getPageHtmlFilePath(join(process.cwd(), `public`), path),
generateHtmlPath(join(process.cwd(), `public`), path),
`utf-8`
)
)
Expand Down
26 changes: 26 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/page-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os from "os"
import { generatePageDataPath } from "../page-data"

describe(`page-data`, () => {
it(`returns correct page data path`, () => {
if (os.platform() !== `win32`) {
expect(generatePageDataPath(`public`, `/path/1`)).toBe(
`public/page-data/path/1/page-data.json`
)

expect(generatePageDataPath(`public`, `/`)).toBe(
`public/page-data/index/page-data.json`
)
}

if (os.platform() === `win32`) {
expect(generatePageDataPath(`public`, `/path/1`)).toBe(
`public\\page-data\\path\\1\\page-data.json`
)

expect(generatePageDataPath(`public`, `/`)).toBe(
`public\\page-data\\index\\page-data.json`
)
}
})
})
26 changes: 26 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/page-html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os from "os"
import { generateHtmlPath } from "../page-html"

describe(`page-html`, () => {
it(`returns correct html path`, () => {
if (os.platform() !== `win32`) {
expect(generateHtmlPath(`public`, `/path/1`)).toBe(
`public/path/1/index.html`
)

expect(generateHtmlPath(`public`, `/path/1/index.html`)).toBe(
`public/path/1/index.html`
)
}

if (os.platform() === `win32`) {
expect(generateHtmlPath(`public`, `/path/1`)).toBe(
`public\\path\\1\\index.html`
)

expect(generateHtmlPath(`public`, `/path/1/index.html`)).toBe(
`public\\path\\1\\index.html`
)
}
})
})
2 changes: 2 additions & 0 deletions packages/gatsby-core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export { isTruthy } from "./is-truthy"
export { getMatchPath } from "./match-path"
export * from "./service-lock"
export * from "./site-metadata"
export * from "./page-data"
export * from "./page-html"
17 changes: 17 additions & 0 deletions packages/gatsby-core-utils/src/page-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from "path"

export function fixedPagePath(pagePath: string): string {
return pagePath === `/` ? `index` : pagePath
}

export function generatePageDataPath(
publicDir: string,
pagePath: string
): string {
return path.join(
publicDir,
`page-data`,
fixedPagePath(pagePath),
`page-data.json`
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import path from "path"
const checkForHtmlSuffix = (pagePath: string): boolean =>
!/\.(html?)$/i.test(pagePath)

// copied from https://github.com/markdalgleish/static-site-generator-webpack-plugin/blob/master/index.js#L161
export const getPageHtmlFilePath = (
dir: string,
outputPath: string
): string => {
export function generateHtmlPath(dir: string, outputPath: string): string {
let outputFileName = outputPath.replace(/^(\/|\\)/, ``) // Remove leading slashes for webpack-dev-server

if (checkForHtmlSuffix(outputPath)) {
Expand All @@ -18,11 +14,11 @@ export const getPageHtmlFilePath = (
return path.join(dir, outputFileName)
}

export const remove = async (
export async function remove(
{ publicDir }: { publicDir: string },
pagePath: string
): Promise<void> => {
const filePath = getPageHtmlFilePath(publicDir, pagePath)
): Promise<void> {
const filePath = generateHtmlPath(publicDir, pagePath)
if (fs.existsSync(filePath)) {
return await fs.remove(filePath)
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-gatsby-cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@babel/runtime": "^7.14.8",
"date-fns": "^2.22.1",
"fs-extra": "^8.1.0",
"gatsby-core-utils": "^2.13.0-next.0",
"gatsby-telemetry": "^2.13.0-next.0",
"kebab-hash": "^0.1.2",
"lodash": "^4.17.21",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from "lodash"
import { createWriteStream, existsSync } from "fs-extra"
import { parse, posix } from "path"
import kebabHash from "kebab-hash"
import { fixedPagePath } from "gatsby-core-utils"
import { IMMUTABLE_CACHING_HEADER } from "./constants"

import {
Expand Down Expand Up @@ -56,8 +57,7 @@ function pathChunkName(path) {
}

function getPageDataPath(path) {
const fixedPagePath = path === `/` ? `index` : path
return posix.join(`page-data`, fixedPagePath, `page-data.json`)
return posix.join(`page-data`, fixedPagePath(path), `page-data.json`)
}

function getScriptPath(file, manifest) {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@babel/runtime": "^7.14.8",
"fs-extra": "^8.1.0",
"gatsby-core-utils": "^2.13.0-next.0",
"kebab-hash": "^0.1.2",
"lodash": "^4.17.21",
"webpack-assets-manifest": "^5.0.6"
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-netlify/src/build-headers-program.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from "lodash"
import { writeFile, existsSync } from "fs-extra"
import { parse, posix } from "path"
import kebabHash from "kebab-hash"
import { fixedPagePath } from "gatsby-core-utils"
import { HEADER_COMMENT, IMMUTABLE_CACHING_HEADER } from "./constants"

import {
Expand Down Expand Up @@ -55,8 +56,7 @@ function pathChunkName(path) {
}

function getPageDataPath(path) {
const fixedPagePath = path === `/` ? `index` : path
return posix.join(`page-data`, fixedPagePath, `page-data.json`)
return posix.join(`page-data`, fixedPagePath(path), `page-data.json`)
}

function getScriptPath(file, manifest) {
Expand Down
12 changes: 5 additions & 7 deletions packages/gatsby/src/commands/build-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ import fs from "fs-extra"
import path from "path"
import { platform } from "os"
import reporter from "gatsby-cli/lib/reporter"

import {
remove as removePageHtmlFile,
getPageHtmlFilePath,
} from "../utils/page-html"
import { removePageData, fixedPagePath } from "../utils/page-data"
generateHtmlPath,
fixedPagePath,
} from "gatsby-core-utils"
import { removePageData } from "../utils/page-data"
import { store } from "../redux"
import { IGatsbyState } from "../redux/types"

const checkFolderIsEmpty = (path: string): boolean =>
fs.existsSync(path) && !fs.readdirSync(path).length

const checkAndRemoveEmptyDir = (publicDir: string, pagePath: string): void => {
const pageHtmlDirectory = path.dirname(
getPageHtmlFilePath(publicDir, pagePath)
)
const pageHtmlDirectory = path.dirname(generateHtmlPath(publicDir, pagePath))
const pageDataDirectory = path.join(
publicDir,
`page-data`,
Expand Down
7 changes: 2 additions & 5 deletions packages/gatsby/src/utils/__tests__/get-page-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { fixedPagePath } from "gatsby-core-utils"
import { store } from "../../redux"
import { getPageData, RETRY_INTERVAL } from "../get-page-data"
import {
fixedPagePath,
flush as flushPageData,
savePageQueryResult,
} from "../page-data"
import { flush as flushPageData, savePageQueryResult } from "../page-data"
import {
IGatsbyPage,
IGatsbyPlugin,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/develop-preload-headers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Response } from "express"
import * as path from "path"

import { fixedPagePath } from "gatsby-core-utils"
import { findPageByPath } from "./find-page-by-path"
import { fixedPagePath, readPageData } from "./page-data"
import { readPageData } from "./page-data"
import { store } from "../redux"

/**
Expand Down
Loading