Skip to content

Commit a283319

Browse files
gatsbybotpieh
andauthored
fix(gatsby): use file:// protocol when importing gatsby-config/node (#37257) (#37260)
* 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) Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 287cabd commit a283319

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

packages/gatsby/src/bootstrap/get-config-file.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import report from "gatsby-cli/lib/reporter"
44
import path from "path"
55
import { COMPILED_CACHE_DIR } from "../utils/parcel/compile-gatsby-files"
66
import { isNearMatch } from "../utils/is-near-match"
7-
import { resolveJSFilepath } from "./resolve-js-file-path"
7+
import { resolveJSFilepath, maybeAddFileProtocol } from "./resolve-js-file-path"
88
import { preferDefault } from "./prefer-default"
99

1010
export async function getConfigFile(
@@ -47,7 +47,7 @@ async function attemptImport(
4747
return { configFilePath: ``, configModule: undefined }
4848
}
4949

50-
const importedModule = await import(configFilePath)
50+
const importedModule = await import(maybeAddFileProtocol(configFilePath))
5151
const configModule = preferDefault(importedModule)
5252

5353
return { configFilePath, configModule }

packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jest.mock(`../../resolve-js-file-path`, () => {
3333
resolveJSFilepath: jest.fn(
3434
({ filePath }) => `${filePath.replace(`src`, `dist`)}.js`
3535
),
36+
maybeAddFileProtocol: jest.fn(val => val),
3637
}
3738
})
3839

packages/gatsby/src/bootstrap/resolve-js-file-path.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import path from "path"
2+
import { pathToFileURL } from "url"
23
import report from "gatsby-cli/lib/reporter"
34

5+
/**
6+
* On Windows, the file protocol is required for the path to be resolved correctly.
7+
* On other platforms, the file protocol is not required, but supported, so we want to just always use it.
8+
* Except jest doesn't work with that and in that environment we never add the file protocol.
9+
*/
10+
export const maybeAddFileProtocol = process.env.JEST_WORKER_ID
11+
? (module: string): string => module
12+
: (module: string): string => pathToFileURL(module).href
13+
414
/**
515
* Figure out if the file path is .js or .mjs without relying on the fs module, and return the file path if it exists.
616
*/

packages/gatsby/src/bootstrap/resolve-module-exports.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import report from "gatsby-cli/lib/reporter"
66
import { babelParseToAst } from "../utils/babel-parse-to-ast"
77
import { testImportError } from "../utils/test-import-error"
88
import { resolveModule, ModuleResolver } from "../utils/module-resolver"
9-
import { resolveJSFilepath } from "./resolve-js-file-path"
9+
import { maybeAddFileProtocol, resolveJSFilepath } from "./resolve-js-file-path"
1010
import { preferDefault } from "./prefer-default"
1111

1212
const staticallyAnalyzeExports = (
@@ -215,7 +215,9 @@ export async function resolveModuleExports(
215215
return []
216216
}
217217

218-
const rawImportedModule = await import(moduleFilePath)
218+
const rawImportedModule = await import(
219+
maybeAddFileProtocol(moduleFilePath)
220+
)
219221

220222
// If the module is cjs, the properties we care about are nested under a top-level `default` property
221223
const importedModule = preferDefault(rawImportedModule)

packages/gatsby/src/utils/import-gatsby-plugin.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { resolveJSFilepath } from "../bootstrap/resolve-js-file-path"
1+
import {
2+
resolveJSFilepath,
3+
maybeAddFileProtocol,
4+
} from "../bootstrap/resolve-js-file-path"
25
import { preferDefault } from "../bootstrap/prefer-default"
36

47
const pluginModuleCache = new Map<string, any>()
@@ -38,7 +41,7 @@ export async function importGatsbyPlugin(
3841
filePath: importPluginModulePath,
3942
})
4043

41-
const rawPluginModule = await import(pluginFilePath)
44+
const rawPluginModule = await import(maybeAddFileProtocol(pluginFilePath))
4245

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

0 commit comments

Comments
 (0)