-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added magic comment visitor and wired it up to the transformer
- Loading branch information
Showing
7 changed files
with
351 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod visitor; | ||
#[cfg(test)] | ||
mod visitor_test; | ||
|
||
pub use self::visitor::*; |
71 changes: 71 additions & 0 deletions
71
packages/transformers/js/core/src/magic_comments/visitor.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::collections::HashMap; | ||
use std::rc::Rc; | ||
|
||
use regex::Regex; | ||
use swc_core::common::Span; | ||
use swc_core::ecma::ast::*; | ||
use swc_core::ecma::utils::ExprExt; | ||
use swc_core::ecma::visit::{Visit, VisitWith}; | ||
|
||
thread_local! { | ||
static RE_CHUNK_NAME: Rc<Regex> = Rc::new(Regex::new(r#"webpackChunkName:\s*['"](?<name>[^'"]+)['"]"#).unwrap()); | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct MagicCommentsVisitor<'a> { | ||
pub magic_comments: HashMap<String, String>, | ||
pub offset: u32, | ||
pub code: &'a str, | ||
} | ||
|
||
impl<'a> MagicCommentsVisitor<'a> { | ||
pub fn new(code: &'a str) -> Self { | ||
Self { | ||
magic_comments: Default::default(), | ||
offset: 0, | ||
code, | ||
} | ||
} | ||
|
||
fn fix_callee(&self, span: &Span) -> (u32, u32) { | ||
let start = span.lo().0 - self.offset; | ||
let end = span.hi().0 - self.offset; | ||
(start, end) | ||
} | ||
} | ||
|
||
impl<'a> Visit for MagicCommentsVisitor<'a> { | ||
fn visit_module(&mut self, node: &Module) { | ||
self.offset = node.span.lo().0; | ||
node.visit_children_with(self); | ||
} | ||
|
||
fn visit_call_expr(&mut self, node: &CallExpr) { | ||
if !node.callee.is_import() || node.args.len() == 0 || !node.args[0].expr.is_str() { | ||
node.visit_children_with(self); | ||
return; | ||
} | ||
|
||
let code_start_index = (code_start - 1) as usize; | ||
let code_end_index = (code_end - 1) as usize; | ||
let inner = &self.code[code_start_index..code_end_index]; | ||
|
||
let re = RE_CHUNK_NAME.with(|re| re.clone()); | ||
|
||
let Some(caps) = re.captures(inner) else { | ||
return; | ||
}; | ||
|
||
let Some(found) = caps.name("name") else { | ||
return; | ||
}; | ||
|
||
let Expr::Lit(Lit::Str(specifier)) = &*node.args[0].expr else { | ||
return; | ||
}; | ||
|
||
self | ||
.magic_comments | ||
.insert(specifier.value.to_string(), found.as_str().to_string()); | ||
} | ||
} |
Oops, something went wrong.