From 9889385932631bf02f522208ef7ac78b057806b8 Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 16 Dec 2022 19:10:10 +0100 Subject: [PATCH 1/4] fix(gatsby): don't output file-loader assets to .cache --- packages/gatsby/src/utils/webpack-utils.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/utils/webpack-utils.ts b/packages/gatsby/src/utils/webpack-utils.ts index 087ae4b1dfc90..101f977eaa08d 100644 --- a/packages/gatsby/src/utils/webpack-utils.ts +++ b/packages/gatsby/src/utils/webpack-utils.ts @@ -24,6 +24,7 @@ import { IProgram, Stage } from "../commands/types" import { eslintConfig, eslintRequiredConfig } from "./eslint-config" import { store } from "../redux" import type { RuleSetUseItem } from "webpack" +import { ROUTES_DIRECTORY } from "../constants" type Loader = string | { loader: string; options?: { [name: string]: any } } type LoaderResolver> = (options?: T) => Loader @@ -203,6 +204,21 @@ export const createWebpackUtils = ( return rule } + const fileLoaderCommonOptions: any = { + name: `${assetRelativeRoot}[name]-[hash].[ext]`, + } + + if (stage === `build-html`) { + // build-html builds to `.cache/page-ssr/routes/` (ROUTES_DIRECTORY) + // so this config is setting it to output assets to `public` (outputPath) + // while preserving "url" (publicPath) + fileLoaderCommonOptions.outputPath = path.relative( + ROUTES_DIRECTORY, + `public` + ) + fileLoaderCommonOptions.publicPath = `/` + } + const loaders: ILoaderUtils = { json: (options = {}) => { return { @@ -349,7 +365,7 @@ export const createWebpackUtils = ( return { loader: require.resolve(`file-loader`), options: { - name: `${assetRelativeRoot}[name]-[hash].[ext]`, + ...fileLoaderCommonOptions, ...options, }, } @@ -360,7 +376,7 @@ export const createWebpackUtils = ( loader: require.resolve(`url-loader`), options: { limit: 10000, - name: `${assetRelativeRoot}[name]-[hash].[ext]`, + ...fileLoaderCommonOptions, fallback: require.resolve(`file-loader`), ...options, }, From 108611545d7a71e35a79617f2236ccc8625a5261 Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 16 Dec 2022 19:18:36 +0100 Subject: [PATCH 2/4] use adjusted settings also for develop-html stage, as that one uses ROUTES_DIRECTORY too --- packages/gatsby/src/utils/webpack-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/webpack-utils.ts b/packages/gatsby/src/utils/webpack-utils.ts index 101f977eaa08d..d91e1f75d4abb 100644 --- a/packages/gatsby/src/utils/webpack-utils.ts +++ b/packages/gatsby/src/utils/webpack-utils.ts @@ -208,7 +208,7 @@ export const createWebpackUtils = ( name: `${assetRelativeRoot}[name]-[hash].[ext]`, } - if (stage === `build-html`) { + if (stage === `build-html` || stage === `develop-html`) { // build-html builds to `.cache/page-ssr/routes/` (ROUTES_DIRECTORY) // so this config is setting it to output assets to `public` (outputPath) // while preserving "url" (publicPath) From 7a4bbfd18c06a670a431f87d0565f366af06c272 Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 16 Dec 2022 19:30:47 +0100 Subject: [PATCH 3/4] update comment --- packages/gatsby/src/utils/webpack-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/webpack-utils.ts b/packages/gatsby/src/utils/webpack-utils.ts index d91e1f75d4abb..2f4c5dd5f0c5a 100644 --- a/packages/gatsby/src/utils/webpack-utils.ts +++ b/packages/gatsby/src/utils/webpack-utils.ts @@ -209,7 +209,7 @@ export const createWebpackUtils = ( } if (stage === `build-html` || stage === `develop-html`) { - // build-html builds to `.cache/page-ssr/routes/` (ROUTES_DIRECTORY) + // build-html and develop-html outputs to `.cache/page-ssr/routes/` (ROUTES_DIRECTORY) // so this config is setting it to output assets to `public` (outputPath) // while preserving "url" (publicPath) fileLoaderCommonOptions.outputPath = path.relative( From 360c9bc0be3fe061193dc5c9e17acd4cf0cdf43e Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 16 Dec 2022 20:29:58 +0100 Subject: [PATCH 4/4] some types for file-loader common options --- packages/gatsby/src/utils/webpack-utils.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/webpack-utils.ts b/packages/gatsby/src/utils/webpack-utils.ts index 2f4c5dd5f0c5a..ec211482d498b 100644 --- a/packages/gatsby/src/utils/webpack-utils.ts +++ b/packages/gatsby/src/utils/webpack-utils.ts @@ -204,7 +204,11 @@ export const createWebpackUtils = ( return rule } - const fileLoaderCommonOptions: any = { + const fileLoaderCommonOptions: { + name: string + publicPath?: string + outputPath?: string + } = { name: `${assetRelativeRoot}[name]-[hash].[ext]`, }