Unverified Commit 64d3ba50 authored by Shu Ding's avatar Shu Ding Committed by GitHub

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

Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>

This should solve the problem that CSS hash happens before PostCSS
loaders.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 327634eb
...@@ -4,12 +4,16 @@ ...@@ -4,12 +4,16 @@
* inside a comment. * inside a comment.
*/ */
import crypto from 'crypto'
export function pitch(this: any) { export function pitch(this: any) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const content = this.fs.readFileSync(this.resourcePath) const content = this.fs.readFileSync(this.resourcePath)
this.data.__checksum = ( this.data.__checksum = crypto
typeof content === 'string' ? Buffer.from(content) : content .createHash('sha256')
).toString('hex') .update(typeof content === 'string' ? Buffer.from(content) : content)
.digest()
.toString('hex')
} }
} }
...@@ -19,15 +23,23 @@ const NextServerCSSLoader = function (this: any, content: string) { ...@@ -19,15 +23,23 @@ const NextServerCSSLoader = function (this: any, content: string) {
// Only add the checksum during development. // Only add the checksum during development.
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const isCSSModule = this.resourcePath.match(/\.module\.(css|sass|scss)$/) 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) { if (isCSSModule) {
return ( return (
content + content + '\nmodule.exports.__checksum = ' + JSON.stringify(checksum)
'\nmodule.exports.__checksum = ' +
JSON.stringify(this.data.__checksum)
) )
} }
return `export default ${JSON.stringify(this.data.__checksum)}` return `export default ${JSON.stringify(checksum)}`
} }
return content return content
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment