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

Issuefix #39151

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
42 changes: 18 additions & 24 deletions packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
const path = require(`path`)
const fs = require('fs-extra')
const path = require('path')
const { createContentDigest } = require('gatsby-core-utils')
const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`)

jest.mock(`asciidoctor`, () => () => {
return {
load: jest.fn(() => {
return {
hasRevisionInfo: jest.fn(),
getAuthor: jest.fn(),
getAttributes: jest.fn(() => {
return {}
}),
getAttribute: jest.fn(),
convert: jest.fn(() => `html generated`),
getDocumentTitle: jest.fn(() => {
return {
getCombined: jest.fn(() => `title`),
hasSubtitle: jest.fn(() => true),
getSubtitle: jest.fn(() => `subtitle`),
getMain: jest.fn(() => `main`),
}
}),
}
}),
}
})
jest.mock(`asciidoctor`, () => () => ({
load: jest.fn(() => ({
hasRevisionInfo: jest.fn(),
getAuthor: jest.fn(),
getAttributes: jest.fn(() => ({})),
getAttribute: jest.fn(),
convert: jest.fn(() => `html generated`),
getDocumentTitle: jest.fn(() => ({
getCombined: jest.fn(() => `title`),
hasSubtitle: jest.fn(() => true),
getSubtitle: jest.fn(() => `subtitle`),
getMain: jest.fn(() => `main`),
})),
})),
}))

describe(`gatsby-transformer-asciidoc`, () => {
let node
Expand Down
28 changes: 23 additions & 5 deletions packages/gatsby/src/bootstrap/resolve-module-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { testImportError } from "../utils/test-import-error"
import { resolveModule, ModuleResolver } from "../utils/module-resolver"
import { maybeAddFileProtocol, resolveJSFilepath } from "./resolve-js-file-path"
import { preferDefault } from "./prefer-default"
import { match } from "@reach/router"

const staticallyAnalyzeExports = (
modulePath: string,
Expand All @@ -23,7 +24,7 @@ const staticallyAnalyzeExports = (
}
const code = fs.readFileSync(absPath, `utf8`) // get file contents

let ast
let ast: t.File
try {
ast = babelParseToAst(code, absPath)
} catch (err) {
Expand Down Expand Up @@ -58,7 +59,7 @@ const staticallyAnalyzeExports = (
isES6 = true
},

ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
ExportNamedDeclaration: function ExportNamedDeclaration(astPath: t.NodePath<t.ExportNamedDeclaration>) {
const declaration = astPath.node.declaration

// get foo from `export const foo = bar`
Expand All @@ -82,7 +83,7 @@ const staticallyAnalyzeExports = (

// get foo from `export { foo } from 'bar'`
// get foo from `export { foo }`
ExportSpecifier: function ExportSpecifier(astPath) {
ExportSpecifier: function ExportSpecifier(astPath: t.NodePath<t.ExportSpecifier>) {
isES6 = true
const exp = astPath?.node?.exported
if (!exp) {
Expand All @@ -100,7 +101,7 @@ const staticallyAnalyzeExports = (
// export default function() {}
// export default function foo() {}
// const foo = () => {}; export default foo
ExportDefaultDeclaration: function ExportDefaultDeclaration(astPath) {
ExportDefaultDeclaration: function ExportDefaultDeclaration(astPath: t.NodePath<t.ExportDefaultDeclaration>) {
const declaration = astPath.node.declaration
if (
!t.isIdentifier(declaration) &&
Expand All @@ -122,7 +123,7 @@ const staticallyAnalyzeExports = (
exportNames.push(exportName)
},

AssignmentExpression: function AssignmentExpression(astPath) {
AssignmentExpression: function AssignmentExpression(astPath: t.NodePath<t.AssignmentExpression>) {
const nodeLeft = astPath.node.left

if (!t.isMemberExpression(nodeLeft)) {
Expand Down Expand Up @@ -238,3 +239,20 @@ export async function resolveModuleExports(

return []
}
function matchPathParams(path: string, matchPath?: string) {
// Try original path
let result = match(matchPath || path, { path })

// Production SSR with encoded URL
if (!result && path.includes('%')) {
try {
const decoded = decodeURIComponent(path)
result = match(matchPath || decoded, { path: decoded })
} catch {
// Fallback to original on decode fail
}
}

// Never return null in SSR to prevent TypeError
return result?.params || {}
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/find-page-by-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IGatsbyPage, IGatsbyState } from "../redux/types"
import { pick } from "@gatsbyjs/reach-router"
import { pick } from "@reach/router"

// Ranks and picks the best page to match. Each segment gets the highest
// amount of points, then the type of segment gets an additional amount of
Expand Down