From f448e12db52841cbb8ff02f7358c865b0aebd49f Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Tue, 20 Dec 2022 09:15:34 -0700 Subject: [PATCH] fix(gatsby): don't output file-loader assets to .cache (#37284) (#37295) * fix(gatsby): don't output file-loader assets to .cache * use adjusted settings also for develop-html stage, as that one uses ROUTES_DIRECTORY too * update comment * some types for file-loader common options (cherry picked from commit 3cbad19275fb1e2596d6ed6d5f342c5439691050) Co-authored-by: Michal Piechowiak --- packages/gatsby/src/utils/webpack-utils.ts | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/utils/webpack-utils.ts b/packages/gatsby/src/utils/webpack-utils.ts index 087ae4b1dfc90..ec211482d498b 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,25 @@ export const createWebpackUtils = ( return rule } + const fileLoaderCommonOptions: { + name: string + publicPath?: string + outputPath?: string + } = { + name: `${assetRelativeRoot}[name]-[hash].[ext]`, + } + + if (stage === `build-html` || stage === `develop-html`) { + // 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( + ROUTES_DIRECTORY, + `public` + ) + fileLoaderCommonOptions.publicPath = `/` + } + const loaders: ILoaderUtils = { json: (options = {}) => { return { @@ -349,7 +369,7 @@ export const createWebpackUtils = ( return { loader: require.resolve(`file-loader`), options: { - name: `${assetRelativeRoot}[name]-[hash].[ext]`, + ...fileLoaderCommonOptions, ...options, }, } @@ -360,7 +380,7 @@ export const createWebpackUtils = ( loader: require.resolve(`url-loader`), options: { limit: 10000, - name: `${assetRelativeRoot}[name]-[hash].[ext]`, + ...fileLoaderCommonOptions, fallback: require.resolve(`file-loader`), ...options, },