Skip to content

Commit ea42d7f

Browse files
gatsbybotpieh
andauthored
fix(gatsby): don't output file-loader assets to .cache (#37284) (#37300)
* 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 3cbad19) Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent 2cc9eaf commit ea42d7f

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

packages/gatsby/src/utils/webpack-utils.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { IProgram, Stage } from "../commands/types"
2323
import { eslintConfig, eslintRequiredConfig } from "./eslint-config"
2424
import { store } from "../redux"
2525
import type { RuleSetUseItem } from "webpack"
26+
import { ROUTES_DIRECTORY } from "../constants"
2627

2728
type Loader = string | { loader: string; options?: { [name: string]: any } }
2829
type LoaderResolver<T = Record<string, unknown>> = (options?: T) => Loader
@@ -199,6 +200,25 @@ export const createWebpackUtils = (
199200
return rule
200201
}
201202

203+
const fileLoaderCommonOptions: {
204+
name: string
205+
publicPath?: string
206+
outputPath?: string
207+
} = {
208+
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
209+
}
210+
211+
if (stage === `build-html` || stage === `develop-html`) {
212+
// build-html and develop-html outputs to `.cache/page-ssr/routes/` (ROUTES_DIRECTORY)
213+
// so this config is setting it to output assets to `public` (outputPath)
214+
// while preserving "url" (publicPath)
215+
fileLoaderCommonOptions.outputPath = path.relative(
216+
ROUTES_DIRECTORY,
217+
`public`
218+
)
219+
fileLoaderCommonOptions.publicPath = `/`
220+
}
221+
202222
const loaders: ILoaderUtils = {
203223
json: (options = {}) => {
204224
return {
@@ -345,7 +365,7 @@ export const createWebpackUtils = (
345365
return {
346366
loader: require.resolve(`file-loader`),
347367
options: {
348-
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
368+
...fileLoaderCommonOptions,
349369
...options,
350370
},
351371
}
@@ -356,7 +376,7 @@ export const createWebpackUtils = (
356376
loader: require.resolve(`url-loader`),
357377
options: {
358378
limit: 10000,
359-
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
379+
...fileLoaderCommonOptions,
360380
fallback: require.resolve(`file-loader`),
361381
...options,
362382
},

0 commit comments

Comments
 (0)