diff --git a/packages/gatsby-source-filesystem/package.json b/packages/gatsby-source-filesystem/package.json index be9b36223a1d5..b37d7a5f06d4f 100644 --- a/packages/gatsby-source-filesystem/package.json +++ b/packages/gatsby-source-filesystem/package.json @@ -19,7 +19,6 @@ "pretty-bytes": "^4.0.2", "progress": "^1.1.8", "read-chunk": "^3.0.0", - "slash": "^1.0.0", "valid-url": "^1.0.9", "xstate": "^3.1.0" }, diff --git a/packages/gatsby-source-filesystem/src/__tests__/utils.js b/packages/gatsby-source-filesystem/src/__tests__/utils.js index 9a8ac914f991a..298a7fe8f982a 100644 --- a/packages/gatsby-source-filesystem/src/__tests__/utils.js +++ b/packages/gatsby-source-filesystem/src/__tests__/utils.js @@ -1,4 +1,4 @@ -const { getRemoteFileExtension, getRemoteFileName } = require(`../utils`) +const { getRemoteFileExtension, getRemoteFileName, slash } = require(`../utils`) describe(`create remote file node`, () => { it(`can correctly retrieve file name and extensions`, () => { @@ -22,3 +22,20 @@ describe(`create remote file node`, () => { }) }) }) + +describe(`slash path`, () => { + it(`can correctly slash path`, () => { + ;[ + [`foo\\bar`, `foo/bar`], + [`foo/bar`, `foo/bar`], + [`foo\\中文`, `foo/中文`], + [`foo/中文`, `foo/中文`], + ].forEach(([path, expectRes]) => { + expect(slash(path)).toBe(expectRes) + }) + }) + it(`does not modify extended length paths`, () => { + const extended = `\\\\?\\some\\path` + expect(slash(extended)).toBe(extended) + }) +}) diff --git a/packages/gatsby-source-filesystem/src/create-file-node.js b/packages/gatsby-source-filesystem/src/create-file-node.js index 2a39890979732..bb9e9ed74a7d6 100644 --- a/packages/gatsby-source-filesystem/src/create-file-node.js +++ b/packages/gatsby-source-filesystem/src/create-file-node.js @@ -1,4 +1,4 @@ -const slash = require(`slash`) +const { slash } = require(`./utils`) const path = require(`path`) const fs = require(`fs-extra`) const mime = require(`mime`) diff --git a/packages/gatsby-source-filesystem/src/create-file-path.js b/packages/gatsby-source-filesystem/src/create-file-path.js index fe930d908fa4d..2d72d3a8e9682 100644 --- a/packages/gatsby-source-filesystem/src/create-file-path.js +++ b/packages/gatsby-source-filesystem/src/create-file-path.js @@ -1,5 +1,5 @@ const path = require(`path`) -const slash = require(`slash`) +const { slash } = require(`./utils`) function findFileNode({ node, getNode }) { // Find the file node. diff --git a/packages/gatsby-source-filesystem/src/utils.js b/packages/gatsby-source-filesystem/src/utils.js index 7d2e4032c7be7..24e03f31e525d 100644 --- a/packages/gatsby-source-filesystem/src/utils.js +++ b/packages/gatsby-source-filesystem/src/utils.js @@ -39,3 +39,22 @@ export function getRemoteFileExtension(url) { export function getRemoteFileName(url) { return getParsedPath(url).name } + +/** + * slash + * -- + * Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar + * + * + * @param {String} path + * @return {String} slashed path + */ +export function slash(path) { + const isExtendedLengthPath = /^\\\\\?\\/.test(path) + + if (isExtendedLengthPath) { + return path + } + + return path.replace(/\\/g, `/`) +}