Skip to content

Commit f9933b1

Browse files
emilpalssonGatsbyJS Bot
authored and
GatsbyJS Bot
committed
fix(gatsby-plugin-sharp): Allow brackets in paths (#18289)
* fix(gatsby-plugin-sharp): Allow brackets in paths * Add tests to assert brackets in paths are set correctly * Fix linting issues * Fix already queued job not being returned properly * Reapply my initial fix for brackets in paths. * Fix setJobToProcess test
1 parent db3f7b0 commit f9933b1

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`setJobToProcess allows brackets in paths 1`] = `
4+
Object {
5+
"1234/file%2Ejpg": Object {
6+
"myoutputpath/1234/file[new]%2Ejpg": Object {
7+
"deferred": Object {
8+
"promise": Promise {},
9+
"reject": [Function],
10+
"resolve": [Function],
11+
},
12+
"job": Object {
13+
"args": Object {},
14+
"contentDigest": "8675309jenny",
15+
"inputPath": "1234/file.jpg",
16+
"outputPath": "myoutputpath/1234/file[new].jpg",
17+
},
18+
},
19+
},
20+
}
21+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { setJobToProcess } = require(`../scheduler`)
2+
3+
describe(`setJobToProcess`, () => {
4+
it(`allows brackets in paths`, () => {
5+
let deferred = {}
6+
deferred.promise = new Promise((resolve, reject) => {
7+
deferred.resolve = resolve
8+
deferred.reject = reject
9+
})
10+
const toProcess = {}
11+
const job = {
12+
args: {},
13+
inputPath: `1234/file.jpg`,
14+
contentDigest: `8675309jenny`,
15+
outputPath: `myoutputpath/1234/file[new].jpg`,
16+
}
17+
setJobToProcess(toProcess, job, deferred)
18+
expect(toProcess).toMatchSnapshot()
19+
})
20+
})

packages/gatsby-plugin-sharp/src/scheduler.js

+36-19
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,66 @@ q.drain = () => {
2020
totalJobs = 0
2121
}
2222

23-
exports.scheduleJob = async (
24-
job,
25-
boundActionCreators,
26-
pluginOptions,
27-
reporter,
28-
reportStatus = true
29-
) => {
30-
const inputFileKey = job.inputPath.replace(/\./g, `%2E`)
31-
const outputFileKey = job.outputPath.replace(/\./g, `%2E`)
32-
const jobPath = `${inputFileKey}.${outputFileKey}`
23+
const getFileKey = filePath => filePath.replace(/\./g, `%2E`)
24+
25+
const setJobToProcess = (toProcess, job, deferred) => {
26+
const inputFileKey = getFileKey(job.inputPath)
27+
const outputFileKey = getFileKey(job.outputPath)
28+
const jobPath = `["${inputFileKey}"].["${outputFileKey}"]`
3329

3430
// Check if the job has already been queued. If it has, there's nothing
3531
// to do, return.
3632
if (_.has(toProcess, jobPath)) {
37-
return _.get(toProcess, `${jobPath}.deferred.promise`)
33+
return { existingPromise: _.get(toProcess, `${jobPath}.deferred.promise`) }
3834
}
3935

4036
// Check if the output file already exists so we don't redo work.
4137
if (existsSync(job.outputPath)) {
42-
return Promise.resolve(job)
38+
return { existingPromise: Promise.resolve(job) }
4339
}
4440

4541
let isQueued = false
4642
if (toProcess[inputFileKey]) {
4743
isQueued = true
4844
}
4945

46+
_.set(toProcess, jobPath, {
47+
job: job,
48+
deferred,
49+
})
50+
51+
return { isQueued }
52+
}
53+
54+
const scheduleJob = async (
55+
job,
56+
boundActionCreators,
57+
pluginOptions,
58+
reporter,
59+
reportStatus = true
60+
) => {
5061
// deferred naming comes from https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred
5162
let deferred = {}
5263
deferred.promise = new Promise((resolve, reject) => {
5364
deferred.resolve = resolve
5465
deferred.reject = reject
5566
})
67+
68+
const { existingPromise, isQueued } = setJobToProcess(
69+
toProcess,
70+
job,
71+
deferred
72+
)
73+
if (existingPromise) {
74+
return existingPromise
75+
}
76+
5677
if (totalJobs === 0) {
5778
bar = createProgress(`Generating image thumbnails`, reporter)
5879
bar.start()
5980
}
60-
6181
totalJobs += 1
6282

63-
_.set(toProcess, jobPath, {
64-
job: job,
65-
deferred,
66-
})
67-
6883
if (!isQueued) {
6984
// Create image job
7085
const jobId = uuidv4()
@@ -80,7 +95,7 @@ exports.scheduleJob = async (
8095
q.push(cb => {
8196
runJobs(
8297
jobId,
83-
inputFileKey,
98+
getFileKey(job.inputPath),
8499
boundActionCreators,
85100
pluginOptions,
86101
reportStatus,
@@ -169,3 +184,5 @@ function runJobs(
169184
})
170185
}
171186
}
187+
188+
export { scheduleJob, setJobToProcess }

0 commit comments

Comments
 (0)