From 41c57b18dfdb88af17c5dda909e4ff5590d37ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 24 Jul 2023 11:34:06 +0900 Subject: [PATCH] fix(css): Expose modules mapping (#39) --- crates/css_node/src/lib.rs | 55 ++++++++++++++++++++++++++++++++--- packages/css/binding.d.ts | 2 ++ packages/css/binding.js | 2 +- packages/css/examples/deps.js | 9 ++++-- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/crates/css_node/src/lib.rs b/crates/css_node/src/lib.rs index daf0dc2..f3ce621 100644 --- a/crates/css_node/src/lib.rs +++ b/crates/css_node/src/lib.rs @@ -1,7 +1,9 @@ #[macro_use] extern crate napi_derive; -use std::{backtrace::Backtrace, env, fmt::Write, panic::set_hook, sync::Arc}; +use std::{ + backtrace::Backtrace, collections::HashMap, env, fmt::Write, panic::set_hook, sync::Arc, +}; use anyhow::{bail, Context}; use napi::{bindgen_prelude::*, Task}; @@ -54,6 +56,10 @@ pub struct TransformOutput { /// JSON string. #[serde(skip_serializing_if = "Option::is_none")] pub deps: Option, + + /// JSON string. + #[serde(skip_serializing_if = "Option::is_none")] + pub modules_mapping: Option, } struct MinifyTask { @@ -120,6 +126,14 @@ enum CssClassNameSegment { Hash, } +#[derive(Serialize)] +#[serde(tag = "type")] +pub enum CssClassName { + Local { name: JsWord }, + Global { name: JsWord }, + Import { name: JsWord, from: JsWord }, +} + impl swc_css_modules::TransformConfig for CssModuleTransformConfig { fn new_name_for(&self, local: &JsWord) -> JsWord { let mut buf = String::new(); @@ -326,6 +340,7 @@ fn minify_inner(code: &str, opts: MinifyOptions) -> anyhow::Result anyhow::Result anyhow::Result = result + .renamed + .into_iter() + .map(|(k, v)| { + ( + k, + v.into_iter() + .map(|v| match v { + swc_css_modules::CssClassName::Local { name } => { + CssClassName::Local { name: name.value } + } + swc_css_modules::CssClassName::Global { name } => { + CssClassName::Global { name: name.value } + } + swc_css_modules::CssClassName::Import { name, from } => { + CssClassName::Import { + name: name.value, + from, + } + } + }) + .collect::>(), + ) + }) + .collect(); + Some( + serde_json::to_string(&map) + .context("failed to serialize the mapping for CSS Modules")?, + ) + } else { + None + }; ss.visit_mut_with(&mut Compiler::new(Config { // TODO: preset-env @@ -461,6 +507,7 @@ fn transform_inner(code: &str, opts: TransformOptions) -> anyhow::Result /** JSON string. */ deps?: string + /** JSON string. */ + modulesMapping?: string } export function minify(code: Buffer, opts: Buffer, signal?: AbortSignal | undefined | null): Promise export function minifySync(code: Buffer, opts: Buffer): TransformOutput diff --git a/packages/css/binding.js b/packages/css/binding.js index 41f3f1d..ab2c94f 100644 --- a/packages/css/binding.js +++ b/packages/css/binding.js @@ -116,7 +116,7 @@ switch (platform) { nativeBinding = require('@swc/css-darwin-universal') } break - } catch {} + } catch { } switch (arch) { case 'x64': localFileExisted = existsSync(join(__dirname, 'css.darwin-x64.node')) diff --git a/packages/css/examples/deps.js b/packages/css/examples/deps.js index cbade4c..f94b3bc 100644 --- a/packages/css/examples/deps.js +++ b/packages/css/examples/deps.js @@ -9,9 +9,12 @@ const css = ` `; async function main() { - const output = swc.transformSync(Buffer.from(css), { - analyzeDependencies: true + const output = await swc.transform(Buffer.from(css), { + analyzeDependencies: true, + cssModules: { + pattern: "[name]-[local]-[hash]", + }, }); - console.log(output.deps); + console.log(output); } main();