Skip to content

Commit 1d9ba86

Browse files
wucongfreiksenet
wucong
authored andcommitted
feat(gatsby-source-filesystem): remove slash (#14372)
* remove slash * remove unnecessary semicolon && add more test * add test for extended length paths
1 parent fcbc66a commit 1d9ba86

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

packages/gatsby-source-filesystem/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"pretty-bytes": "^4.0.2",
2020
"progress": "^1.1.8",
2121
"read-chunk": "^3.0.0",
22-
"slash": "^1.0.0",
2322
"valid-url": "^1.0.9",
2423
"xstate": "^3.1.0"
2524
},

packages/gatsby-source-filesystem/src/__tests__/utils.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getRemoteFileExtension, getRemoteFileName } = require(`../utils`)
1+
const { getRemoteFileExtension, getRemoteFileName, slash } = require(`../utils`)
22

33
describe(`create remote file node`, () => {
44
it(`can correctly retrieve file name and extensions`, () => {
@@ -22,3 +22,20 @@ describe(`create remote file node`, () => {
2222
})
2323
})
2424
})
25+
26+
describe(`slash path`, () => {
27+
it(`can correctly slash path`, () => {
28+
;[
29+
[`foo\\bar`, `foo/bar`],
30+
[`foo/bar`, `foo/bar`],
31+
[`foo\\中文`, `foo/中文`],
32+
[`foo/中文`, `foo/中文`],
33+
].forEach(([path, expectRes]) => {
34+
expect(slash(path)).toBe(expectRes)
35+
})
36+
})
37+
it(`does not modify extended length paths`, () => {
38+
const extended = `\\\\?\\some\\path`
39+
expect(slash(extended)).toBe(extended)
40+
})
41+
})

packages/gatsby-source-filesystem/src/create-file-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const slash = require(`slash`)
1+
const { slash } = require(`./utils`)
22
const path = require(`path`)
33
const fs = require(`fs-extra`)
44
const mime = require(`mime`)

packages/gatsby-source-filesystem/src/create-file-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require(`path`)
2-
const slash = require(`slash`)
2+
const { slash } = require(`./utils`)
33

44
function findFileNode({ node, getNode }) {
55
// Find the file node.

packages/gatsby-source-filesystem/src/utils.js

+19
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,22 @@ export function getRemoteFileExtension(url) {
3939
export function getRemoteFileName(url) {
4040
return getParsedPath(url).name
4141
}
42+
43+
/**
44+
* slash
45+
* --
46+
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
47+
*
48+
*
49+
* @param {String} path
50+
* @return {String} slashed path
51+
*/
52+
export function slash(path) {
53+
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
54+
55+
if (isExtendedLengthPath) {
56+
return path
57+
}
58+
59+
return path.replace(/\\/g, `/`)
60+
}

0 commit comments

Comments
 (0)