Skip to content

Commit

Permalink
fix(gatsby): don't output file-loader assets to .cache (#37284) (#37295)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
gatsbybot and pieh authored Dec 20, 2022
1 parent 8fc123c commit f448e12
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions packages/gatsby/src/utils/webpack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = Record<string, unknown>> = (options?: T) => Loader
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -349,7 +369,7 @@ export const createWebpackUtils = (
return {
loader: require.resolve(`file-loader`),
options: {
name: `${assetRelativeRoot}[name]-[hash].[ext]`,
...fileLoaderCommonOptions,
...options,
},
}
Expand All @@ -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,
},
Expand Down

0 comments on commit f448e12

Please sign in to comment.