| [9af201e] | 1 | import crypto from 'crypto'
|
|---|
| 2 | import * as sharedState from './sharedState'
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Calculate the hash of a string.
|
|---|
| 6 | *
|
|---|
| 7 | * This doesn't need to be cryptographically secure or
|
|---|
| 8 | * anything like that since it's used only to detect
|
|---|
| 9 | * when the CSS changes to invalidate the context.
|
|---|
| 10 | *
|
|---|
| 11 | * This is wrapped in a try/catch because it's really dependent
|
|---|
| 12 | * on how Node itself is build and the environment and OpenSSL
|
|---|
| 13 | * version / build that is installed on the user's machine.
|
|---|
| 14 | *
|
|---|
| 15 | * Based on the environment this can just outright fail.
|
|---|
| 16 | *
|
|---|
| 17 | * See https://github.com/nodejs/node/issues/40455
|
|---|
| 18 | *
|
|---|
| 19 | * @param {string} str
|
|---|
| 20 | */
|
|---|
| 21 | function getHash(str) {
|
|---|
| 22 | try {
|
|---|
| 23 | return crypto.createHash('md5').update(str, 'utf-8').digest('binary')
|
|---|
| 24 | } catch (err) {
|
|---|
| 25 | return ''
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Determine if the CSS tree is different from the
|
|---|
| 31 | * previous version for the given `sourcePath`.
|
|---|
| 32 | *
|
|---|
| 33 | * @param {string} sourcePath
|
|---|
| 34 | * @param {import('postcss').Node} root
|
|---|
| 35 | */
|
|---|
| 36 | export function hasContentChanged(sourcePath, root) {
|
|---|
| 37 | let css = root.toString()
|
|---|
| 38 |
|
|---|
| 39 | // We only care about files with @tailwind directives
|
|---|
| 40 | // Other files use an existing context
|
|---|
| 41 | if (!css.includes('@tailwind')) {
|
|---|
| 42 | return false
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | let existingHash = sharedState.sourceHashMap.get(sourcePath)
|
|---|
| 46 | let rootHash = getHash(css)
|
|---|
| 47 | let didChange = existingHash !== rootHash
|
|---|
| 48 |
|
|---|
| 49 | sharedState.sourceHashMap.set(sourcePath, rootHash)
|
|---|
| 50 |
|
|---|
| 51 | return didChange
|
|---|
| 52 | }
|
|---|