Skip to content

Commit 48ee2b0

Browse files
committed
fix(gatsby): don't output file-loader assets to .cache (#37284)
* 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)
1 parent 8fc123c commit 48ee2b0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { IProgram, Stage } from "../commands/types"
2424
import { eslintConfig, eslintRequiredConfig } from "./eslint-config"
2525
import { store } from "../redux"
2626
import type { RuleSetUseItem } from "webpack"
27+
import { ROUTES_DIRECTORY } from "../constants"
2728

2829
type Loader = string | { loader: string; options?: { [name: string]: any } }
2930
type LoaderResolver<T = Record<string, unknown>> = (options?: T) => Loader
@@ -203,6 +204,25 @@ export const createWebpackUtils = (
203204
return rule
204205
}
205206

207+
const fileLoaderCommonOptions: {
208+
name: string
209+
publicPath?: string
210+
outputPath?: string
211+
} = {
212+
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
213+
}
214+
215+
if (stage === `build-html` || stage === `develop-html`) {
216+
// build-html and develop-html outputs to `.cache/page-ssr/routes/` (ROUTES_DIRECTORY)
217+
// so this config is setting it to output assets to `public` (outputPath)
218+
// while preserving "url" (publicPath)
219+
fileLoaderCommonOptions.outputPath = path.relative(
220+
ROUTES_DIRECTORY,
221+
`public`
222+
)
223+
fileLoaderCommonOptions.publicPath = `/`
224+
}
225+
206226
const loaders: ILoaderUtils = {
207227
json: (options = {}) => {
208228
return {
@@ -349,7 +369,7 @@ export const createWebpackUtils = (
349369
return {
350370
loader: require.resolve(`file-loader`),
351371
options: {
352-
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
372+
...fileLoaderCommonOptions,
353373
...options,
354374
},
355375
}
@@ -360,7 +380,7 @@ export const createWebpackUtils = (
360380
loader: require.resolve(`url-loader`),
361381
options: {
362382
limit: 10000,
363-
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
383+
...fileLoaderCommonOptions,
364384
fallback: require.resolve(`file-loader`),
365385
...options,
366386
},

0 commit comments

Comments
 (0)