Skip to content

Commit

Permalink
fix(gatsby): fix bundling image cdn tools (gatsbyjs#36534)
Browse files Browse the repository at this point in the history
Co-authored-by: Ward Peeters <[email protected]>
Co-authored-by: Lennart <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2022
1 parent 67fa4b4 commit 0ef331d
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import importFrom from "import-from"
import type { GatsbyCache } from "gatsby"

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

return getGatsbyCache(`gatsby`)
const cache = getGatsbyCache(`gatsby`)
global._polyfillRemoteFileCache = cache
return cache
}
5 changes: 3 additions & 2 deletions packages/gatsby-script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
],
"sideEffects": false,
"scripts": {
"build": "microbundle -f cjs,modern --jsx React.createElement",
"build": "microbundle -f cjs,modern --jsx React.createElement && tsc --emitDeclarationOnly --declaration --declarationDir dist",
"postbuild": "prepend-directive --files=dist/index.js,dist/index.modern.mjs --directive=\"client export\"",
"watch": "microbundle watch -f cjs,modern --jsx React.createElement --no-compress",
"prepare": "cross-env NODE_ENV=production npm run clean && npm run build",
"clean": "del-cli dist/*"
},
"devDependencies": {
"@gatsbyjs/reach-router": "^1.3.9",
"@testing-library/react": "^11.2.7",
"cross-env": "^7.0.3",
"del-cli": "^5.0.0",
Expand All @@ -47,4 +48,4 @@
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "packages/gatsby-script"
}
}
}
9 changes: 4 additions & 5 deletions packages/gatsby-script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../../tsconfig.json",
"exclude": [
"node_modules",
"src/__tests__",
"dist"
]
"exclude": ["node_modules", "src/__tests__", "dist"],
"compilerOptions": {
"declaration": false
}
}
6 changes: 3 additions & 3 deletions packages/gatsby-sharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
},
"license": "MIT",
"scripts": {
"build": "babel src --out-file dist/index.js --ignore \"**/__tests__\" --extensions \".ts,.js\"",
"build": "babel src --out-dir dist --ignore \"**/__tests__\" --extensions \".ts,.js\"",
"typegen": "tsc --emitDeclarationOnly --declaration --declarationDir dist/",
"prepare": "cross-env NODE_ENV=production npm-run-all --npm-path npm -s build typegen",
"watch": "babel -w src --out-file dist/index.js --ignore \"**/__tests__\" --extensions \".ts,.js\""
"watch": "babel -w src --out-dir dist --ignore \"**/__tests__\" --extensions \".ts,.js\""
}
}
}
1 change: 1 addition & 0 deletions packages/gatsby-sharp/src/sharp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require(`sharp`)
9 changes: 9 additions & 0 deletions packages/gatsby/src/schema/graphql-engine/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// "engines-fs-provider" must be first import, as it sets up global
// fs and this need to happen before anything else tries to import fs
import "../../utils/engines-fs-provider"

import { getCache as getGatsbyCache } from "../../utils/get-cache"

if (!global._polyfillRemoteFileCache) {
global._polyfillRemoteFileCache = getGatsbyCache(`gatsby`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export async function createGraphqlEngineBundle(
// only load one version of lmdb
lmdb: require.resolve(`lmdb`),
"ts-node": require.resolve(`./shims/ts-node`),
"gatsby-sharp$": require.resolve(`./shims/gatsby-sharp`),
},
},
plugins: [
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/schema/graphql-engine/entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// "engines-fs-provider" must be first import, as it sets up global
// fs and this need to happen before anything else tries to import fs
import "../../utils/engines-fs-provider"
// "bootstrap" must be first import, as it sets up multiple globals that need to be set early
// see details in that module
import "./bootstrap"

import { ExecutionResult, Source } from "graphql"
import { uuid } from "gatsby-core-utils"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sharp from "gatsby-sharp/dist/sharp"

sharp.simd(true)
sharp.concurrency(1)

module.exports = function getSharpInstance() {
return sharp
}
2 changes: 2 additions & 0 deletions types/gatsby-monorepo/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ declare module NodeJS {
buildId: string
root: string
}

_polyfillRemoteFileCache?: import("gatsby").GatsbyCache
}
}

0 comments on commit 0ef331d

Please sign in to comment.