Skip to content

Commit 0ef331d

Browse files
piehwardpeetLekoArts
authored
fix(gatsby): fix bundling image cdn tools (gatsbyjs#36534)
Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: Lennart <[email protected]>
1 parent 67fa4b4 commit 0ef331d

File tree

10 files changed

+40
-14
lines changed

10 files changed

+40
-14
lines changed

packages/gatsby-plugin-utils/src/polyfill-remote-file/utils/cache.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import importFrom from "import-from"
22
import type { GatsbyCache } from "gatsby"
33

44
export function getCache(): GatsbyCache {
5+
if (global._polyfillRemoteFileCache) {
6+
return global._polyfillRemoteFileCache
7+
}
58
// We need to use import-from to remove circular dependency
69
const { getCache: getGatsbyCache } = importFrom(
710
global.__GATSBY?.root ?? process.cwd(),
811
`gatsby/dist/utils/get-cache`
912
) as { getCache: (key: string) => GatsbyCache }
1013

11-
return getGatsbyCache(`gatsby`)
14+
const cache = getGatsbyCache(`gatsby`)
15+
global._polyfillRemoteFileCache = cache
16+
return cache
1217
}

packages/gatsby-script/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
],
1616
"sideEffects": false,
1717
"scripts": {
18-
"build": "microbundle -f cjs,modern --jsx React.createElement",
18+
"build": "microbundle -f cjs,modern --jsx React.createElement && tsc --emitDeclarationOnly --declaration --declarationDir dist",
1919
"postbuild": "prepend-directive --files=dist/index.js,dist/index.modern.mjs --directive=\"client export\"",
2020
"watch": "microbundle watch -f cjs,modern --jsx React.createElement --no-compress",
2121
"prepare": "cross-env NODE_ENV=production npm run clean && npm run build",
2222
"clean": "del-cli dist/*"
2323
},
2424
"devDependencies": {
25+
"@gatsbyjs/reach-router": "^1.3.9",
2526
"@testing-library/react": "^11.2.7",
2627
"cross-env": "^7.0.3",
2728
"del-cli": "^5.0.0",
@@ -47,4 +48,4 @@
4748
"url": "https://github.com/gatsbyjs/gatsby.git",
4849
"directory": "packages/gatsby-script"
4950
}
50-
}
51+
}

packages/gatsby-script/tsconfig.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"exclude": [
4-
"node_modules",
5-
"src/__tests__",
6-
"dist"
7-
]
3+
"exclude": ["node_modules", "src/__tests__", "dist"],
4+
"compilerOptions": {
5+
"declaration": false
6+
}
87
}

packages/gatsby-sharp/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
},
3636
"license": "MIT",
3737
"scripts": {
38-
"build": "babel src --out-file dist/index.js --ignore \"**/__tests__\" --extensions \".ts,.js\"",
38+
"build": "babel src --out-dir dist --ignore \"**/__tests__\" --extensions \".ts,.js\"",
3939
"typegen": "tsc --emitDeclarationOnly --declaration --declarationDir dist/",
4040
"prepare": "cross-env NODE_ENV=production npm-run-all --npm-path npm -s build typegen",
41-
"watch": "babel -w src --out-file dist/index.js --ignore \"**/__tests__\" --extensions \".ts,.js\""
41+
"watch": "babel -w src --out-dir dist --ignore \"**/__tests__\" --extensions \".ts,.js\""
4242
}
43-
}
43+
}

packages/gatsby-sharp/src/sharp.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require(`sharp`)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// "engines-fs-provider" must be first import, as it sets up global
2+
// fs and this need to happen before anything else tries to import fs
3+
import "../../utils/engines-fs-provider"
4+
5+
import { getCache as getGatsbyCache } from "../../utils/get-cache"
6+
7+
if (!global._polyfillRemoteFileCache) {
8+
global._polyfillRemoteFileCache = getGatsbyCache(`gatsby`)
9+
}

packages/gatsby/src/schema/graphql-engine/bundle-webpack.ts

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export async function createGraphqlEngineBundle(
179179
// only load one version of lmdb
180180
lmdb: require.resolve(`lmdb`),
181181
"ts-node": require.resolve(`./shims/ts-node`),
182+
"gatsby-sharp$": require.resolve(`./shims/gatsby-sharp`),
182183
},
183184
},
184185
plugins: [

packages/gatsby/src/schema/graphql-engine/entry.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// "engines-fs-provider" must be first import, as it sets up global
2-
// fs and this need to happen before anything else tries to import fs
3-
import "../../utils/engines-fs-provider"
1+
// "bootstrap" must be first import, as it sets up multiple globals that need to be set early
2+
// see details in that module
3+
import "./bootstrap"
44

55
import { ExecutionResult, Source } from "graphql"
66
import { uuid } from "gatsby-core-utils"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sharp from "gatsby-sharp/dist/sharp"
2+
3+
sharp.simd(true)
4+
sharp.concurrency(1)
5+
6+
module.exports = function getSharpInstance() {
7+
return sharp
8+
}

types/gatsby-monorepo/global.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ declare module NodeJS {
88
buildId: string
99
root: string
1010
}
11+
12+
_polyfillRemoteFileCache?: import("gatsby").GatsbyCache
1113
}
1214
}

0 commit comments

Comments
 (0)