Skip to content

Commit 76ac266

Browse files
wardpeetfreiksenet
authored andcommitted
fix(gatsby): fix hashes used in webpack for output (#18973)
* fix(gatsby): use hashes as module identifiers for webpack caching * fix(gatsby): disable hmr on production builds for css * update absolute path to relative ones
1 parent 0c1ce9a commit 76ac266

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

packages/gatsby/src/bootstrap/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,14 @@ module.exports = async (args: BootstrapArgs) => {
352352
)
353353

354354
const browserPluginsRequires = browserPlugins
355-
.map(
356-
plugin =>
357-
`{
358-
plugin: require('${plugin.resolve}'),
355+
.map(plugin => {
356+
// we need a relative import path to keep contenthash the same if directory changes
357+
const relativePluginPath = path.relative(siteDir, plugin.resolve)
358+
return `{
359+
plugin: require('${slash(relativePluginPath)}'),
359360
options: ${JSON.stringify(plugin.options)},
360361
}`
361-
)
362+
})
362363
.join(`,`)
363364

364365
const browserAPIRunner = `module.exports = [${browserPluginsRequires}]\n`

packages/gatsby/src/bootstrap/requires-writer.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const _ = require(`lodash`)
2+
const path = require(`path`)
23
const fs = require(`fs-extra`)
34
const crypto = require(`crypto`)
5+
const slash = require(`slash`)
46
const { store, emitter } = require(`../redux/`)
57
const reporter = require(`gatsby-cli/lib/reporter`)
68
const { match } = require(`@reach/router/lib/utils`)
@@ -143,12 +145,17 @@ const preferDefault = m => m && m.default || m
143145
const preferDefault = m => m && m.default || m
144146
\n`
145147
asyncRequires += `exports.components = {\n${components
146-
.map(
147-
c =>
148-
` "${c.componentChunkName}": () => import("${joinPath(
149-
c.component
150-
)}" /* webpackChunkName: "${c.componentChunkName}" */)`
151-
)
148+
.map(c => {
149+
// we need a relative import path to keep contenthash the same if directory changes
150+
const relativeComponentPath = path.relative(
151+
path.join(program.directory, `.cache`),
152+
c.component
153+
)
154+
155+
return ` "${c.componentChunkName}": () => import("${slash(
156+
relativeComponentPath
157+
)}" /* webpackChunkName: "${c.componentChunkName}" */)`
158+
})
152159
.join(`,\n`)}
153160
}\n\n`
154161

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@ module.exports = async ({
459459
loaders.css({ ...options, importLoaders: 1 }),
460460
loaders.postcss({ browsers }),
461461
]
462-
if (!isSSR) use.unshift(loaders.miniCssExtract({ hmr: !options.modules }))
462+
if (!isSSR)
463+
use.unshift(
464+
loaders.miniCssExtract({ hmr: !PRODUCTION && !options.modules })
465+
)
463466

464467
return {
465468
use,

packages/gatsby/src/utils/webpack.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ module.exports = async (
469469
runtimeChunk: {
470470
name: `webpack-runtime`,
471471
},
472+
// use hashes instead of ids for module identifiers
473+
// TODO update to deterministic in webpack 5 (hashed is deprecated)
474+
// @see https://webpack.js.org/guides/caching/#module-identifiers
475+
moduleIds: `hashed`,
472476
splitChunks: {
473477
name: false,
474478
chunks: `all`,
@@ -497,6 +501,8 @@ module.exports = async (
497501
test: /\.(css|scss|sass|less|styl)$/,
498502
chunks: `all`,
499503
enforce: true,
504+
// this rule trumps all other rules because of the priority.
505+
priority: 10,
500506
},
501507
},
502508
},

0 commit comments

Comments
 (0)