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

Hash both pitch and main loader for server CSS imports #44063

Merged
merged 2 commits into from Dec 15, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,12 +4,16 @@
* inside a comment.
*/

import crypto from 'crypto'

export function pitch(this: any) {
if (process.env.NODE_ENV !== 'production') {
const content = this.fs.readFileSync(this.resourcePath)
this.data.__checksum = (
typeof content === 'string' ? Buffer.from(content) : content
).toString('hex')
this.data.__checksum = crypto
.createHash('sha256')
.update(typeof content === 'string' ? Buffer.from(content) : content)
.digest()
.toString('hex')
}
}

Expand All @@ -19,15 +23,23 @@ const NextServerCSSLoader = function (this: any, content: string) {
// Only add the checksum during development.
if (process.env.NODE_ENV !== 'production') {
const isCSSModule = this.resourcePath.match(/\.module\.(css|sass|scss)$/)
const checksum = crypto
.createHash('sha256')
.update(
this.data.__checksum +
(typeof content === 'string' ? Buffer.from(content) : content)
)
.digest()
.toString('hex')
.substring(0, 12)

if (isCSSModule) {
return (
content +
'\nmodule.exports.__checksum = ' +
JSON.stringify(this.data.__checksum)
content + '\nmodule.exports.__checksum = ' + JSON.stringify(checksum)
)
}

return `export default ${JSON.stringify(this.data.__checksum)}`
return `export default ${JSON.stringify(checksum)}`
}

return content
Expand Down