Skip to content

Commit 3817174

Browse files
authored
fix(gatsby-recipes): fix GatsbyShadowFile resource — don't error if package isn't installed (#27790)
* fix(gatsby-recipes): fix GatsbyShadowFile resource — don't error if package isn't installed * Update snapshots * Turn comment into verbose log * Somehow utila started causing errors w/ rollup 🤷‍♂️ * remove reporter * revert addiing utila to rollup externals
1 parent b57e41f commit 3817174

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

packages/gatsby-recipes/rollup.config.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import resolve from "@rollup/plugin-node-resolve"
22
import babel from "@rollup/plugin-babel"
33
import commonjs from "@rollup/plugin-commonjs"
44
import json from "@rollup/plugin-json"
5-
import replace from "@rollup/plugin-replace";
5+
import replace from "@rollup/plugin-replace"
66
import autoExternal from "rollup-plugin-auto-external"
77
import internal from "rollup-plugin-internal"
88
import path from "path"
@@ -28,7 +28,7 @@ export default [
2828
{
2929
input: {
3030
index: `src/index.js`,
31-
"graphql-server/server": `src/graphql-server/server.js`
31+
"graphql-server/server": `src/graphql-server/server.js`,
3232
},
3333
output: {
3434
dir: `dist`,
@@ -39,8 +39,8 @@ export default [
3939
plugins: [
4040
replace({
4141
values: {
42-
"process.env.NODE_ENV": JSON.stringify(`production`)
43-
}
42+
"process.env.NODE_ENV": JSON.stringify(`production`),
43+
},
4444
}),
4545
excludeDevTools(),
4646
json(),
@@ -50,7 +50,7 @@ export default [
5050
exclude: `node_modules/**`,
5151
}),
5252
commonjs({
53-
transformMixedEsModules: true
53+
transformMixedEsModules: true,
5454
}),
5555
resolve({
5656
dedupe: [
@@ -63,10 +63,10 @@ export default [
6363
`@mdx-js/mdx`,
6464
`@mdx-js/react`,
6565
`@mdx-js/runtime`,
66-
`urql`,
66+
`urql`,
6767
`@urql/core`,
6868
`subscriptions-transport-ws`,
69-
]
69+
],
7070
}),
7171
autoExternal(),
7272
internal([
@@ -108,20 +108,14 @@ export default [
108108
exclude: `node_modules/**`,
109109
}),
110110
commonjs({
111-
transformMixedEsModules: true
111+
transformMixedEsModules: true,
112112
}),
113113
resolve({
114-
dedupe: [
115-
`@mdx-js/react`,
116-
]
114+
dedupe: [`@mdx-js/react`],
117115
}),
118116
autoExternal(),
119-
internal([
120-
`@mdx-js/react`
121-
])
117+
internal([`@mdx-js/react`]),
122118
],
123-
external: [
124-
`react`
125-
]
126-
}
119+
external: [`react`],
120+
},
127121
]

packages/gatsby-recipes/src/providers/gatsby/__snapshots__/shadow-file.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export default () => <h1>F. Scott Fitzgerald</h1>
1616
exports[`Shadow File resource e2e shadow file resource test: GatsbyShadowFile create plan 1`] = `
1717
Object {
1818
"currentState": Object {},
19+
"dependsOn": Array [
20+
Object {
21+
"name": "gatsby-theme-blog",
22+
"resourceName": "NPMPackage",
23+
},
24+
],
1925
"describe": "Shadow src/components/author.js from the theme gatsby-theme-blog",
2026
"diff": "- Original - 0
2127
+ Modified + 4
@@ -77,6 +83,12 @@ export default () => <h1>F. Scott Fitzgerald</h1>
7783
"path": "src/gatsby-theme-blog/components/author.js",
7884
"theme": "gatsby-theme-blog",
7985
},
86+
"dependsOn": Array [
87+
Object {
88+
"name": "gatsby-theme-blog",
89+
"resourceName": "NPMPackage",
90+
},
91+
],
8092
"describe": "Shadow src/components/author.js from the theme gatsby-theme-blog",
8193
"diff": "Compared values have no visual difference.",
8294
"id": "src/gatsby-theme-blog/components/author.js",

packages/gatsby-recipes/src/providers/gatsby/shadow-file.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const message = resource =>
104104

105105
export const plan = async ({ root }, { theme, path: filePath, id }) => {
106106
let currentResource = ``
107+
let newContents = ``
107108
if (!id) {
108109
// eslint-disable-next-line
109110
id = relativePathForShadowedFile({ theme, filePath })
@@ -114,22 +115,29 @@ export const plan = async ({ root }, { theme, path: filePath, id }) => {
114115
// eslint-disable-next-line
115116
const fullFilePathToShadow = path.join(root, `node_modules`, theme, filePath)
116117

117-
const newContents = await fs.readFile(fullFilePathToShadow, `utf8`)
118+
try {
119+
newContents = await fs.readFile(fullFilePathToShadow, `utf8`)
120+
} catch (e) {
121+
// We couldn't read the specified ShadowFile while planning. Probably just doesn't
122+
// exist yet because the theme's NPMPackage isn't yet installed
123+
}
124+
118125
const newResource = {
119126
id,
120127
theme,
121128
path: filePath,
122129
contents: newContents,
123130
}
124131

125-
const diff = await getDiff(currentResource.contents || ``, newContents)
132+
const diff = await getDiff(currentResource?.contents || ``, newContents)
126133

127134
return {
128135
id,
129136
theme,
130137
path: filePath,
131138
diff,
132139
currentState: currentResource,
140+
dependsOn: [{ resourceName: `NPMPackage`, name: theme }],
133141
newState: newResource,
134142
describe: `Shadow ${filePath} from the theme ${theme}`,
135143
}

0 commit comments

Comments
 (0)