Skip to content

Commit

Permalink
perf: Make cjs_optimizer use Atom instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Feb 3, 2025
1 parent 041011a commit 3760b1d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
25 changes: 13 additions & 12 deletions crates/next-core/src/next_shared/transforms/next_cjs_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use async_trait::async_trait;
use next_custom_transforms::transforms::cjs_optimizer::{cjs_optimizer, Config, PackageConfig};
use rustc_hash::FxHashMap;
use swc_core::{
atoms::atom,
common::SyntaxContext,
ecma::{ast::*, visit::VisitMutWith},
};
Expand All @@ -18,30 +19,30 @@ pub fn get_next_cjs_optimizer_rule(enable_mdx_rs: bool) -> ModuleRule {
// build it internally without accepting customization.
let config = Config {
packages: FxHashMap::from_iter([(
"next/server".to_string(),
atom!("next/server"),
PackageConfig {
transforms: FxHashMap::from_iter([
(
"NextRequest".into(),
"next/dist/server/web/spec-extension/request".into(),
atom!("NextRequest"),
atom!("next/dist/server/web/spec-extension/request"),
),
(
"NextResponse".into(),
"next/dist/server/web/spec-extension/response".into(),
atom!("NextResponse"),
atom!("next/dist/server/web/spec-extension/response"),
),
(
"ImageResponse".into(),
"next/dist/server/web/spec-extension/image-response".into(),
atom!("ImageResponse"),
atom!("next/dist/server/web/spec-extension/image-response"),
),
(
"userAgentFromString".into(),
"next/dist/server/web/spec-extension/user-agent".into(),
atom!("userAgentFromString"),
atom!("next/dist/server/web/spec-extension/user-agent"),
),
(
"userAgent".into(),
"next/dist/server/web/spec-extension/user-agent".into(),
atom!("userAgent"),
atom!("next/dist/server/web/spec-extension/user-agent"),
),
("after".into(), "next/dist/server/after".into()),
(atom!("after"), atom!("next/dist/server/after")),
]),
},
)]),
Expand Down
12 changes: 6 additions & 6 deletions crates/next-custom-transforms/src/transforms/cjs_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_core::{
CallExpr, Callee, Decl, Expr, Id, Ident, IdentName, Lit, MemberExpr, MemberProp,
Module, ModuleItem, Pat, Script, Stmt, VarDecl, VarDeclKind, VarDeclarator,
},
atoms::{Atom, JsWord},
atoms::Atom,
utils::{prepend_stmts, private_ident, ExprFactory, IdentRenamer},
visit::{noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith},
},
Expand All @@ -23,18 +23,18 @@ pub fn cjs_optimizer(config: Config, unresolved_ctxt: SyntaxContext) -> CjsOptim

#[derive(Clone, Debug, Deserialize)]
pub struct Config {
pub packages: FxHashMap<String, PackageConfig>,
pub packages: FxHashMap<Atom, PackageConfig>,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PackageConfig {
pub transforms: FxHashMap<JsWord, JsWord>,
pub transforms: FxHashMap<Atom, Atom>,
}

pub struct CjsOptimizer {
data: State,
packages: FxHashMap<String, PackageConfig>,
packages: FxHashMap<Atom, PackageConfig>,
unresolved_ctxt: SyntaxContext,
}

Expand All @@ -46,7 +46,7 @@ struct State {
imports: FxHashMap<Id, ImportRecord>,

/// `(module_specifier, property): (identifier)`
replaced: FxHashMap<(Atom, JsWord), Id>,
replaced: FxHashMap<(Atom, Atom), Id>,

extra_stmts: Vec<Stmt>,

Expand All @@ -64,7 +64,7 @@ struct ImportRecord {
}

impl CjsOptimizer {
fn should_rewrite(&self, module_specifier: &str) -> Option<&FxHashMap<JsWord, JsWord>> {
fn should_rewrite(&self, module_specifier: &Atom) -> Option<&FxHashMap<Atom, Atom>> {
self.packages.get(module_specifier).map(|v| &v.transforms)
}
}
Expand Down

0 comments on commit 3760b1d

Please sign in to comment.