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

fix(gatsby): use file:// protocol when importing gatsby-config/node #37257

Merged
merged 5 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/gatsby/src/bootstrap/get-config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import report from "gatsby-cli/lib/reporter"
import path from "path"
import { COMPILED_CACHE_DIR } from "../utils/parcel/compile-gatsby-files"
import { isNearMatch } from "../utils/is-near-match"
import { resolveJSFilepath } from "./resolve-js-file-path"
import { resolveJSFilepath, maybeAddFileProtocol } from "./resolve-js-file-path"
import { preferDefault } from "./prefer-default"

export async function getConfigFile(
Expand Down Expand Up @@ -47,7 +47,7 @@ async function attemptImport(
return { configFilePath: ``, configModule: undefined }
}

const importedModule = await import(configFilePath)
const importedModule = await import(maybeAddFileProtocol(configFilePath))
const configModule = preferDefault(importedModule)

return { configFilePath, configModule }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jest.mock(`../../resolve-js-file-path`, () => {
resolveJSFilepath: jest.fn(
({ filePath }) => `${filePath.replace(`src`, `dist`)}.js`
),
maybeAddFileProtocol: jest.fn(val => val),
}
})

Expand Down
10 changes: 10 additions & 0 deletions packages/gatsby/src/bootstrap/resolve-js-file-path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import path from "path"
import { pathToFileURL } from "url"
import report from "gatsby-cli/lib/reporter"

/**
* On Windows, the file protocol is required for the path to be resolved correctly.
* On other platforms, the file protocol is not required, but supported, so we want to just always use it.
* Except jest doesn't work with that and in that environment we never add the file protocol.
*/
export const maybeAddFileProtocol = process.env.JEST_WORKER_ID
? (module: string): string => module
: (module: string): string => pathToFileURL(module).href

/**
* Figure out if the file path is .js or .mjs without relying on the fs module, and return the file path if it exists.
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby/src/bootstrap/resolve-module-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import report from "gatsby-cli/lib/reporter"
import { babelParseToAst } from "../utils/babel-parse-to-ast"
import { testImportError } from "../utils/test-import-error"
import { resolveModule, ModuleResolver } from "../utils/module-resolver"
import { resolveJSFilepath } from "./resolve-js-file-path"
import { maybeAddFileProtocol, resolveJSFilepath } from "./resolve-js-file-path"
import { preferDefault } from "./prefer-default"

const staticallyAnalyzeExports = (
Expand Down Expand Up @@ -215,7 +215,9 @@ export async function resolveModuleExports(
return []
}

const rawImportedModule = await import(moduleFilePath)
const rawImportedModule = await import(
maybeAddFileProtocol(moduleFilePath)
)

// If the module is cjs, the properties we care about are nested under a top-level `default` property
const importedModule = preferDefault(rawImportedModule)
Expand Down
7 changes: 5 additions & 2 deletions packages/gatsby/src/utils/import-gatsby-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { resolveJSFilepath } from "../bootstrap/resolve-js-file-path"
import {
resolveJSFilepath,
maybeAddFileProtocol,
} from "../bootstrap/resolve-js-file-path"
import { preferDefault } from "../bootstrap/prefer-default"

const pluginModuleCache = new Map<string, any>()
Expand Down Expand Up @@ -38,7 +41,7 @@ export async function importGatsbyPlugin(
filePath: importPluginModulePath,
})

const rawPluginModule = await import(pluginFilePath)
const rawPluginModule = await import(maybeAddFileProtocol(pluginFilePath))

// If the module is cjs, the properties we care about are nested under a top-level `default` property
pluginModule = preferDefault(rawPluginModule)
Expand Down