Skip to content

Commit

Permalink
fix(gatsby): use file:// protocol when importing gatsby-config/node (#…
Browse files Browse the repository at this point in the history
…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 74d4090.

* different solution?

* only file://ify when importing

* update mock

(cherry picked from commit e0ea47c)
  • Loading branch information
pieh authored and marvinjude committed Dec 14, 2022
1 parent 287cabd commit 0f1a5d7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby/src/bootstrap/get-config-file.ts
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
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
@@ -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
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
@@ -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

0 comments on commit 0f1a5d7

Please sign in to comment.