From 0f1a5d739b97ee58a2357e3dc0d85e89c486751a Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 14 Dec 2022 11:40:27 +0100 Subject: [PATCH] fix(gatsby): use file:// protocol when importing gatsby-config/node (#37257) * fix(gatsby): use file:// protocol when importing gatsby-config/node * Revert "fix(gatsby): use file:// protocol when importing gatsby-config/node" This reverts commit 74d409068506a70039fe41a3be55bd03b7112f12. * different solution? * only file://ify when importing * update mock (cherry picked from commit e0ea47cc43f4d5523edea7c3eaa0ae384750c773) --- packages/gatsby/src/bootstrap/get-config-file.ts | 4 ++-- .../bootstrap/load-plugins/__tests__/load-plugins.ts | 1 + packages/gatsby/src/bootstrap/resolve-js-file-path.ts | 10 ++++++++++ .../gatsby/src/bootstrap/resolve-module-exports.ts | 6 ++++-- packages/gatsby/src/utils/import-gatsby-plugin.ts | 7 +++++-- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/src/bootstrap/get-config-file.ts b/packages/gatsby/src/bootstrap/get-config-file.ts index c2dd3651608e4..aec93b78d497a 100644 --- a/packages/gatsby/src/bootstrap/get-config-file.ts +++ b/packages/gatsby/src/bootstrap/get-config-file.ts @@ -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( @@ -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 } diff --git a/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts b/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts index 817fbc2c262e7..c3e1afafed364 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts +++ b/packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts @@ -33,6 +33,7 @@ jest.mock(`../../resolve-js-file-path`, () => { resolveJSFilepath: jest.fn( ({ filePath }) => `${filePath.replace(`src`, `dist`)}.js` ), + maybeAddFileProtocol: jest.fn(val => val), } }) diff --git a/packages/gatsby/src/bootstrap/resolve-js-file-path.ts b/packages/gatsby/src/bootstrap/resolve-js-file-path.ts index b4ad51bb88b40..d35845068bc5d 100644 --- a/packages/gatsby/src/bootstrap/resolve-js-file-path.ts +++ b/packages/gatsby/src/bootstrap/resolve-js-file-path.ts @@ -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. */ diff --git a/packages/gatsby/src/bootstrap/resolve-module-exports.ts b/packages/gatsby/src/bootstrap/resolve-module-exports.ts index d530a096abfe0..e52b795dd4ee2 100644 --- a/packages/gatsby/src/bootstrap/resolve-module-exports.ts +++ b/packages/gatsby/src/bootstrap/resolve-module-exports.ts @@ -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 = ( @@ -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) diff --git a/packages/gatsby/src/utils/import-gatsby-plugin.ts b/packages/gatsby/src/utils/import-gatsby-plugin.ts index f902c262dfa97..6c4ef347ad9c9 100644 --- a/packages/gatsby/src/utils/import-gatsby-plugin.ts +++ b/packages/gatsby/src/utils/import-gatsby-plugin.ts @@ -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() @@ -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)