From 4ec2863767336e8f989eef8b87537504ff2bcb14 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 11 Mar 2021 12:47:43 +0100 Subject: [PATCH 1/4] fix(gatsby): wokraround some webpack issues causing first save after running gatsby develop to not have any effect --- .../gatsby/src/utils/gatsby-webpack-virtual-modules.ts | 8 ++++++++ packages/gatsby/src/utils/start-server.ts | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts b/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts index b322f4e078103..d7ac46bc0025e 100644 --- a/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts +++ b/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts @@ -1,5 +1,6 @@ import VirtualModulesPlugin from "webpack-virtual-modules" import * as path from "path" +import * as fs from "fs-extra" /* * This module allows creating virtual (in memory only) modules / files * that webpack compilation can access without the need to write module @@ -46,6 +47,13 @@ export function writeModule(filePath: string, fileContents: string): void { return } + // workaround webpack marking virtual modules as deleted because those files don't really exist + // so we create those files just so watchpack doesn't mark them as initially missing + fs.outputFileSync( + path.join(process.cwd(), VIRTUAL_MODULES_BASE_PATH, filePath), + fileContents + ) + fileContentLookup[adjustedFilePath] = fileContents instances.forEach(instance => { diff --git a/packages/gatsby/src/utils/start-server.ts b/packages/gatsby/src/utils/start-server.ts index 9df9e4fb1b3cb..b7e9bad3921d5 100644 --- a/packages/gatsby/src/utils/start-server.ts +++ b/packages/gatsby/src/utils/start-server.ts @@ -78,6 +78,11 @@ export async function startServer( }) webpackActivity.start() + // loading indicator + // write virtual module always to not fail webpack compilation, but only add express route handlers when + // query on demand is enabled and loading indicator is not disabled + writeVirtualLoadingIndicatorModule() + const THIRTY_SECONDS = 30 * 1000 let cancelDevJSNotice: CancelExperimentNoticeCallbackOrUndefined if ( @@ -486,10 +491,6 @@ module.exports = { route({ app, program, store }) } - // loading indicator - // write virtual module always to not fail webpack compilation, but only add express route handlers when - // query on demand is enabled and loading indicator is not disabled - writeVirtualLoadingIndicatorModule() if ( process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND && process.env.GATSBY_QUERY_ON_DEMAND_LOADING_INDICATOR === `true` From bb45125f2460a8e06eac3c823fb348ed2a2c843a Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 11 Mar 2021 13:21:26 +0100 Subject: [PATCH 2/4] adjust unit tests to mock extra fs method --- packages/gatsby/src/bootstrap/__tests__/requires-writer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/gatsby/src/bootstrap/__tests__/requires-writer.js b/packages/gatsby/src/bootstrap/__tests__/requires-writer.js index 35d95e7c72397..dd772a3fab652 100644 --- a/packages/gatsby/src/bootstrap/__tests__/requires-writer.js +++ b/packages/gatsby/src/bootstrap/__tests__/requires-writer.js @@ -20,6 +20,7 @@ const generatePagesState = pages => { jest.mock(`fs-extra`, () => { return { writeFile: () => Promise.resolve(), + outputFileSync: () => {}, move: () => {}, } }) From 996c4ec1461b141f17be33baa6eb896c9a8237b9 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 11 Mar 2021 17:51:34 +0100 Subject: [PATCH 3/4] Update packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts Co-authored-by: Vladimir Razuvaev --- packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts b/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts index d7ac46bc0025e..96b819f9530e2 100644 --- a/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts +++ b/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts @@ -50,7 +50,7 @@ export function writeModule(filePath: string, fileContents: string): void { // workaround webpack marking virtual modules as deleted because those files don't really exist // so we create those files just so watchpack doesn't mark them as initially missing fs.outputFileSync( - path.join(process.cwd(), VIRTUAL_MODULES_BASE_PATH, filePath), + adjustedFilePath fileContents ) From 2cacacabcbb330e7a0240630ebed1c395cac613a Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 11 Mar 2021 18:19:54 +0100 Subject: [PATCH 4/4] format --- packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts b/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts index 96b819f9530e2..a79eea382ae15 100644 --- a/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts +++ b/packages/gatsby/src/utils/gatsby-webpack-virtual-modules.ts @@ -49,10 +49,7 @@ export function writeModule(filePath: string, fileContents: string): void { // workaround webpack marking virtual modules as deleted because those files don't really exist // so we create those files just so watchpack doesn't mark them as initially missing - fs.outputFileSync( - adjustedFilePath - fileContents - ) + fs.outputFileSync(adjustedFilePath, fileContents) fileContentLookup[adjustedFilePath] = fileContents