Skip to content

Commit 4be2253

Browse files
committed
Copy over emotion's hash code, faux ESM makes it tricky to use as dep
1 parent 5678797 commit 4be2253

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

.changeset/mighty-berries-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compai/css-gui': patch
3+
---
4+
5+
Copy over emotion's hash code, faux ESM makes it tricky to use as dep

packages/gui/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"typescript": "^4.6.4"
4242
},
4343
"dependencies": {
44-
"@emotion/hash": "^0.9.0",
4544
"@emotion/react": "^11.9.0",
4645
"@mdx-js/react": "^1.6.22",
4746
"@radix-ui/react-accordion": "^0.1.6",

packages/gui/src/lib/hash.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* eslint-disable */
2+
// Copied from https://github.com/emotion-js/emotion/blob/6eef2e3ede044d64530d94f08fe0e92f6267dc6c/packages/hash/src/index.js
3+
// License https://github.com/emotion-js/emotion/blob/6eef2e3ede044d64530d94f08fe0e92f6267dc6c/packages/hash/LICENSE
4+
// Inspired by https://github.com/garycourt/murmurhash-js
5+
// Ported from https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash2.cpp#L37-L86
6+
7+
export default function murmur2(str: string): string {
8+
// 'm' and 'r' are mixing constants generated offline.
9+
// They're not really 'magic', they just happen to work well.
10+
11+
// const m = 0x5bd1e995;
12+
// const r = 24;
13+
14+
// Initialize the hash
15+
16+
var h = 0
17+
18+
// Mix 4 bytes at a time into the hash
19+
20+
var k,
21+
i = 0,
22+
len = str.length
23+
for (; len >= 4; ++i, len -= 4) {
24+
k =
25+
(str.charCodeAt(i) & 0xff) |
26+
((str.charCodeAt(++i) & 0xff) << 8) |
27+
((str.charCodeAt(++i) & 0xff) << 16) |
28+
((str.charCodeAt(++i) & 0xff) << 24)
29+
30+
k =
31+
/* Math.imul(k, m): */
32+
(k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0xe995) << 16)
33+
k ^= /* k >>> r: */ k >>> 24
34+
35+
h =
36+
/* Math.imul(k, m): */
37+
((k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0xe995) << 16)) ^
38+
/* Math.imul(h, m): */
39+
((h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16))
40+
}
41+
42+
// Handle the last few bytes of the input array
43+
44+
switch (len) {
45+
case 3:
46+
h ^= (str.charCodeAt(i + 2) & 0xff) << 16
47+
case 2:
48+
h ^= (str.charCodeAt(i + 1) & 0xff) << 8
49+
case 1:
50+
h ^= str.charCodeAt(i) & 0xff
51+
h =
52+
/* Math.imul(h, m): */
53+
(h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16)
54+
}
55+
56+
// Do a few final mixes of the hash to ensure the last few
57+
// bytes are well-incorporated.
58+
59+
h ^= h >>> 13
60+
h =
61+
/* Math.imul(h, m): */
62+
(h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0xe995) << 16)
63+
64+
return ((h ^ (h >>> 15)) >>> 0).toString(36)
65+
}

packages/gui/src/lib/transformers/inline-styles-to-style-element.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { cloneDeep } from 'lodash-es'
22
import { unified } from 'unified'
33
import { visit } from 'unist-util-visit'
4-
import hash from '@emotion/hash'
4+
import hash from '../hash'
55
import { stringifyCSSObject } from '../codegen/stringify-css-object'
66
import { toCSSObject } from '../codegen/to-css-object'
77
import { addCSSClassSyntax } from '../classes'
@@ -17,7 +17,7 @@ export const inlineStylesToStyleElement = (node: any) => {
1717

1818
const style = node.properties.style
1919
// @ts-ignore
20-
const selector = 'css-' + hash.default(JSON.stringify(style))
20+
const selector = 'css-' + hash(JSON.stringify(style))
2121

2222
if (!node.properties.class) {
2323
node.properties.class = selector

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,6 @@
565565
resolved "https://registry.yarnpkg.com/@emotion/hash/-/hash-0.8.0.tgz#bbbff68978fefdbe68ccb533bc8cbe1d1afb5413"
566566
integrity sha512-kBJtf7PH6aWwZ6fka3zQ0p6SBYzx4fl1LoZXE2RrnYST9Xljm7WfKJrU4g/Xr3Beg72MLrp1AWNUmuYJTL7Cow==
567567

568-
"@emotion/hash@^0.9.0":
569-
version "0.9.0"
570-
resolved "https://registry.yarnpkg.com/@emotion/hash/-/hash-0.9.0.tgz#c5153d50401ee3c027a57a177bc269b16d889cb7"
571-
integrity sha512-14FtKiHhy2QoPIzdTcvh//8OyBlknNs2nXRwIhG904opCby3l+9Xaf/wuPvICBF0rc1ZCNBd3nKe9cd2mecVkQ==
572-
573568
"@emotion/is-prop-valid@^0.8.1":
574569
version "0.8.8"
575570
resolved "https://registry.yarnpkg.com/@emotion/is-prop-valid/-/is-prop-valid-0.8.8.tgz#db28b1c4368a259b60a97311d6a952d4fd01ac1a"

0 commit comments

Comments
 (0)