Skip to content

Commit

Permalink
fix(gatsby): PnP fixes (#35194) (#35199)
Browse files Browse the repository at this point in the history
(cherry picked from commit 79c5598)

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
ViCo0TeCH and pieh committed Mar 22, 2022
1 parent 0b6067a commit a56b652
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .circleci/config.yml
Expand Up @@ -350,9 +350,6 @@ jobs:
- run: # Quick upgrade to the v2 (any version, we just need the real set version)
command: yarn policies set-version berry
working_directory: /tmp/e2e-tests/gatsby-pnp
- run: # TODO: remove pinned version
command: yarn set version 3.1.1
working_directory: /tmp/e2e-tests/gatsby-pnp
- run: # Explicitly set nodeLinker to avoid Yarn selecting node_modules due to the Yarn 1.x lockfile
command: yarn config set nodeLinker pnp
working_directory: /tmp/e2e-tests/gatsby-pnp
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby-worker/src/child.ts
Expand Up @@ -6,6 +6,7 @@ import {
ERROR,
RESULT,
CUSTOM_MESSAGE,
WORKER_READY,
} from "./types"
import { isPromise } from "./utils"

Expand Down Expand Up @@ -102,6 +103,8 @@ if (process.send && process.env.GATSBY_WORKER_MODULE_PATH) {
}

process.on(`message`, messageHandler)

ensuredSendToMain([WORKER_READY])
}

export { isWorker, getMessenger }
14 changes: 12 additions & 2 deletions packages/gatsby-worker/src/index.ts
Expand Up @@ -7,6 +7,7 @@ import {
ERROR,
RESULT,
CUSTOM_MESSAGE,
WORKER_READY,
ParentMessageUnion,
ChildMessageUnion,
} from "./types"
Expand Down Expand Up @@ -87,6 +88,7 @@ interface IWorkerInfo<T> {
signal: NodeJS.Signals | null
}>
currentTask?: TaskInfo<T>
ready: Promise<void>
}

export interface IPublicWorkerInfo {
Expand Down Expand Up @@ -183,9 +185,13 @@ export class WorkerPool<
silent: options && options.silent,
})

let workerReadyResolve: () => void
const workerInfo: IWorkerInfo<keyof WorkerModuleExports> = {
workerId,
worker,
ready: new Promise<void>(resolve => {
workerReadyResolve = resolve
}),
exitedPromise: new Promise(resolve => {
worker.on(`exit`, (code, signal) => {
if (workerInfo.currentTask) {
Expand Down Expand Up @@ -247,6 +253,8 @@ export class WorkerPool<
for (const listener of this.listeners) {
listener(msg[1] as MessagesFromChild, workerId)
}
} else if (msg[0] === WORKER_READY) {
workerReadyResolve()
}
})

Expand Down Expand Up @@ -322,14 +330,16 @@ export class WorkerPool<
this.idleWorkers.add(workerInfo)
}

private doWork<T extends keyof WorkerModuleExports>(
private async doWork<T extends keyof WorkerModuleExports>(
taskInfo: TaskInfo<T>,
workerInfo: IWorkerInfo<T>
): void {
): Promise<void> {
// block worker
workerInfo.currentTask = taskInfo
this.idleWorkers.delete(workerInfo)

await workerInfo.ready

const msg: ParentMessageUnion = [
EXECUTE,
taskInfo.functionName,
Expand Down
8 changes: 7 additions & 1 deletion packages/gatsby-worker/src/types.ts
Expand Up @@ -3,6 +3,7 @@ export const ERROR = 0b10
export const RESULT = 0b11
export const END = 0b00
export const CUSTOM_MESSAGE = 0b100
export const WORKER_READY = 0b1000

type CustomMessage = [typeof CUSTOM_MESSAGE, unknown]

Expand All @@ -11,6 +12,7 @@ type FunctionArgs = Array<any>

type ExecuteMessage = [typeof EXECUTE, FunctionName, FunctionArgs]
type EndMessage = [typeof END]
type WorkerReadyMessage = [typeof WORKER_READY]

export type ParentMessageUnion = ExecuteMessage | EndMessage | CustomMessage

Expand All @@ -30,4 +32,8 @@ type ResultType = unknown

type TaskResult = [typeof RESULT, ResultType]

export type ChildMessageUnion = TaskError | TaskResult | CustomMessage
export type ChildMessageUnion =
| TaskError
| TaskResult
| CustomMessage
| WorkerReadyMessage
4 changes: 1 addition & 3 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Expand Up @@ -18,9 +18,7 @@ export function constructParcel(siteRoot: string): Parcel {
`${siteRoot}/${gatsbyFileRegex}`,
`${siteRoot}/plugins/**/${gatsbyFileRegex}`,
],
defaultConfig: require.resolve(`gatsby-parcel-config`, {
paths: [siteRoot],
}),
defaultConfig: require.resolve(`gatsby-parcel-config`),
mode: `production`,
targets: {
root: {
Expand Down

0 comments on commit a56b652

Please sign in to comment.