Skip to content

Commit

Permalink
Initialize emotion plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Feb 22, 2022
1 parent ea532c7 commit 3bfdf51
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/next-swc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions packages/next-swc/crates/core/Cargo.toml
Expand Up @@ -7,25 +7,27 @@ version = "0.0.0"
crate-type = ["cdylib", "rlib"]

[dependencies]
byteorder = "1"
chrono = "0.4"
once_cell = "1.8.0"
easy-error = "1.0.0"
either = "1"
fxhash = "0.2.1"
once_cell = "1.8.0"
pathdiff = "0.2.0"
radix_fmt = "1"
regex = "1.5"
serde = "1"
serde_json = "1"
styled_components = "0.14.0"
swc = "0.126.2"
swc_atoms = "0.2.7"
swc_common = { version = "0.17.0", features = ["concurrent", "sourcemap"] }
swc_common = {version = "0.17.0", features = ["concurrent", "sourcemap"]}
swc_css = "0.87.0"
swc_ecma_loader = { version = "0.28.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.114.2", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_ecma_loader = {version = "0.28.0", features = ["node", "lru"]}
swc_ecmascript = {version = "0.114.2", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"]}
swc_node_base = "0.5.1"
swc_stylis = "0.83.0"
tracing = {version = "0.1.28", features = ["release_max_level_off"]}
regex = "1.5"

[dev-dependencies]
swc_ecma_transforms_testing = "0.60.0"
Expand Down
62 changes: 62 additions & 0 deletions packages/next-swc/crates/core/src/emotion/global_parent_cache.rs
@@ -0,0 +1,62 @@
use std::path::{Path, PathBuf};

use fxhash::FxHashMap;
use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_json::from_reader;
use swc_common::sync::RwLock;

pub(crate) static GLOBAL_PARENT_CACHE: Lazy<GlobalParentCache> = Lazy::new(GlobalParentCache::new);

#[derive(Deserialize, Debug, Clone)]
struct PackageJson {
name: String,
}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub(crate) struct RootPathInfo {
pub(crate) package_name: String,
pub(crate) root_path: PathBuf,
}

impl RootPathInfo {
pub(crate) fn new(package_name: String, root_path: PathBuf) -> Self {
Self {
package_name,
root_path,
}
}
}

pub(crate) struct GlobalParentCache {
cache: RwLock<FxHashMap<PathBuf, RootPathInfo>>,
}

impl GlobalParentCache {
fn new() -> Self {
Self {
cache: RwLock::new(FxHashMap::default()),
}
}
}

impl GlobalParentCache {
pub(crate) fn get(&self, p: &Path) -> Option<RootPathInfo> {
let guard = self.cache.read();
guard.get(p).cloned()
}

pub(crate) fn insert(&self, p: PathBuf, parent: PathBuf) -> RootPathInfo {
let mut write_lock = self.cache.borrow_mut();
// Safe to unwrap, because `existed` is true
let file = std::fs::File::open(parent.join("package.json")).unwrap();
let package_json: PackageJson = from_reader(file).unwrap();
let info = RootPathInfo {
package_name: package_json.name,
root_path: parent,
};
write_lock.insert(p, info.clone());
info
}
}
59 changes: 59 additions & 0 deletions packages/next-swc/crates/core/src/emotion/hash.rs
@@ -0,0 +1,59 @@
// Ported from https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash2.cpp#L37-L86

use byteorder::{ByteOrder, LittleEndian};

const M: u32 = 0x5bd1_e995;

pub(crate) fn murmurhash2(key: &[u8]) -> String {
let mut h: u32 = 0;

let mut four_bytes_chunks = key.chunks_exact(4);
for chunk in four_bytes_chunks.by_ref() {
let mut k: u32 = LittleEndian::read_u32(chunk);
k = k.wrapping_mul(M);
k ^= k >> 24;
h = k.wrapping_mul(M) ^ h.wrapping_mul(M);
}
let remainder = four_bytes_chunks.remainder();

// Handle the last few bytes of the input array
match remainder.len() {
3 => {
h ^= u32::from(remainder[2]) << 16;
}
2 => {
h ^= u32::from(remainder[1]) << 8;
}
1 => {
h ^= u32::from(remainder[0]);
h = h.wrapping_mul(M);
}
_ => {}
}
h ^= h >> 13;
h = h.wrapping_mul(M);
format!("{}", radix_fmt::radix_36(h ^ (h >> 15)))
}

#[cfg(test)]
mod test {

use super::murmurhash2;

#[test]
fn test_murmur2() {
let s1 = "abcdef";
let s2 = "abcdeg";
for i in 0..5 {
assert_eq!(
murmurhash2(&s1[i..5].as_bytes()),
murmurhash2(&s2[i..5].as_bytes())
);
}
}

#[test]
fn verify_hash() {
assert_eq!(murmurhash2("something".as_bytes()), "crsxd7".to_owned());
}
}

0 comments on commit 3bfdf51

Please sign in to comment.