Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): fix hashes used in webpack for output #18973

Merged
merged 5 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,14 @@ module.exports = async (args: BootstrapArgs) => {
)

const browserPluginsRequires = browserPlugins
.map(
plugin =>
`{
plugin: require('${plugin.resolve}'),
.map(plugin => {
// we need a relative import path to keep contenthash the same if directory changes
const relativePluginPath = path.relative(siteDir, plugin.resolve)
return `{
plugin: require('${slash(relativePluginPath)}'),
options: ${JSON.stringify(plugin.options)},
}`
)
})
.join(`,`)

const browserAPIRunner = `module.exports = [${browserPluginsRequires}]\n`
Expand Down
19 changes: 13 additions & 6 deletions packages/gatsby/src/bootstrap/requires-writer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const _ = require(`lodash`)
const path = require(`path`)
const fs = require(`fs-extra`)
const crypto = require(`crypto`)
const slash = require(`slash`)
const { store, emitter } = require(`../redux/`)
const reporter = require(`gatsby-cli/lib/reporter`)
const { match } = require(`@reach/router/lib/utils`)
Expand Down Expand Up @@ -133,12 +135,17 @@ const preferDefault = m => m && m.default || m
const preferDefault = m => m && m.default || m
\n`
asyncRequires += `exports.components = {\n${components
.map(
c =>
` "${c.componentChunkName}": () => import("${joinPath(
c.component
)}" /* webpackChunkName: "${c.componentChunkName}" */)`
)
.map(c => {
// we need a relative import path to keep contenthash the same if directory changes
const relativeComponentPath = path.relative(
path.join(program.directory, `.cache`),
c.component
)

return ` "${c.componentChunkName}": () => import("${slash(
relativeComponentPath
)}" /* webpackChunkName: "${c.componentChunkName}" */)`
})
.join(`,\n`)}
}\n\n`

Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby/src/utils/webpack-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@ module.exports = async ({
loaders.css({ ...options, importLoaders: 1 }),
loaders.postcss({ browsers }),
]
if (!isSSR) use.unshift(loaders.miniCssExtract({ hmr: !options.modules }))
if (!isSSR)
use.unshift(
loaders.miniCssExtract({ hmr: !PRODUCTION && !options.modules })
)

return {
use,
Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ module.exports = async (
runtimeChunk: {
name: `webpack-runtime`,
},
// use hashes instead of ids for module identifiers
// TODO update to deterministic in webpack 5 (hashed is deprecated)
// @see https://webpack.js.org/guides/caching/#module-identifiers
moduleIds: `hashed`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it deprecated though? Is the recommendation to use something else (that is deterministic as well)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value is "deterministic" not "hashed". It's a new value in webpack 5

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh alrighty

splitChunks: {
name: false,
chunks: `all`,
Expand Down Expand Up @@ -497,6 +501,8 @@ module.exports = async (
test: /\.(css|scss|sass|less|styl)$/,
chunks: `all`,
enforce: true,
// this rule trumps all other rules because of the priority.
priority: 10,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The z-index of webpack builds

},
},
},
Expand Down