Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3: Webpack Magic-Comment Transformer #220

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ impl TransformerPlugin for AtlaspackJsTransformerPlugin {
})
})
.unwrap_or_default(),
magic_comments: self.config.magic_comments.unwrap_or_default(),
..atlaspack_js_swc_core::Config::default()
},
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;

use atlaspack_core::diagnostic;
use indexmap::IndexMap;
use std::collections::HashMap;
use swc_core::atoms::{Atom, JsWord};

use atlaspack_core::plugin::{PluginOptions, TransformResult};
Expand Down Expand Up @@ -44,6 +45,7 @@ pub(crate) fn convert_result(
&options.project_root,
transformer_config,
result.dependencies,
result.magic_comments,
&asset,
)?;

Expand Down Expand Up @@ -381,6 +383,7 @@ pub(crate) fn convert_dependencies(
project_root: &Path,
transformer_config: &atlaspack_js_swc_core::Config,
dependencies: Vec<atlaspack_js_swc_core::DependencyDescriptor>,
magic_comments: HashMap<String, String>,
asset: &Asset,
) -> Result<(IndexMap<Atom, Dependency>, Vec<PathBuf>), Vec<Diagnostic>> {
let mut dependency_by_specifier = IndexMap::new();
Expand All @@ -400,7 +403,13 @@ pub(crate) fn convert_dependencies(
)?;

match result {
DependencyConversionResult::Dependency(dependency) => {
DependencyConversionResult::Dependency(mut dependency) => {
if let Some(chunk_name) = magic_comments.get(&dependency.specifier) {
dependency.meta.insert(
"chunkNameMagicComment".to_string(),
chunk_name.clone().into(),
);
}
dependency_by_specifier.insert(placeholder, dependency);
}
DependencyConversionResult::InvalidateOnFileChange(file_path) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ pub struct JsTransformerConfig {

#[serde(rename = "inlineFS")]
pub inline_fs: Option<bool>,

pub magic_comments: Option<bool>,
}
1 change: 1 addition & 0 deletions packages/transformers/js/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ atlaspack_contextual_imports = { path = "../../../../crates/atlaspack_contextual
atlaspack_swc_runner = { path = "../../../../crates/atlaspack_swc_runner" }
parking_lot = { workspace = true }
regex = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
tracing-test = { workspace = true }
11 changes: 11 additions & 0 deletions packages/transformers/js/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod env_replacer;
mod fs;
mod global_replacer;
mod hoist;
mod magic_comments;
mod modules;
mod node_replacer;
pub mod test_utils;
Expand Down Expand Up @@ -39,6 +40,7 @@ pub use hoist::ExportedSymbol;
use hoist::HoistResult;
pub use hoist::ImportedSymbol;
use indexmap::IndexMap;
use magic_comments::MagicCommentsVisitor;
use modules::esm2cjs;
use node_replacer::NodeReplacer;
use path_slash::PathExt;
Expand Down Expand Up @@ -134,6 +136,7 @@ pub struct Config {
pub standalone: bool,
pub inline_constants: bool,
pub conditional_bundling: bool,
pub magic_comments: bool,
}

#[derive(Serialize, Debug, Default)]
Expand All @@ -152,6 +155,7 @@ pub struct TransformResult {
pub has_node_replacements: bool,
pub is_constant_module: bool,
pub conditions: HashSet<Condition>,
pub magic_comments: HashMap<String, String>,
}

fn targets_to_versions(targets: &Option<HashMap<String, String>>) -> Option<Versions> {
Expand Down Expand Up @@ -266,6 +270,13 @@ pub fn transform(

let global_mark = Mark::fresh(Mark::root());
let unresolved_mark = Mark::fresh(Mark::root());

if config.magic_comments && MagicCommentsVisitor::has_magic_comment(code) {
let mut magic_comment_visitor = MagicCommentsVisitor::new(code);
module.visit_with(&mut magic_comment_visitor);
result.magic_comments = magic_comment_visitor.magic_comments;
}

let module = module.fold_with(&mut chain!(
resolver(unresolved_mark, global_mark, config.is_type_script),
// Decorators can use type information, so must run before the TypeScript pass.
Expand Down
5 changes: 5 additions & 0 deletions packages/transformers/js/core/src/magic_comments/mod.rs
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::*;
84 changes: 84 additions & 0 deletions packages/transformers/js/core/src/magic_comments/visitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::collections::HashMap;

use regex::Regex;
use swc_core::ecma::ast::*;
use swc_core::ecma::visit::Visit;
use swc_core::ecma::visit::VisitWith;

thread_local! {
static RE_CHUNK_NAME: Regex = Regex::new(r#"webpackChunkName:\s*['"](?<name>[^'"]+)['"]"#).unwrap();
}

const MAGIC_COMMENT_DEFAULT_KEYWORD: &str = "webpackChunkName";

/// MagicCommentsVisitor will scan code for Webpack Magic Comments
#[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,
}
}

pub fn has_magic_comment(code: &str) -> bool {
code.contains(MAGIC_COMMENT_DEFAULT_KEYWORD)
}
}

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.visit_children_with(self);
return;
}

let Some(expr) = node.args.first() else {
return;
};

let Expr::Lit(Lit::Str(specifier)) = &*expr.expr else {
return;
};

// Comments are not available in the AST so we have to get the start/end
// positions of the code for the call expression and match it with a regular
// expression that matches the magic comment keyword within the code slice
let code_start = (node.span.lo().0 - self.offset) as usize;
alshdavid marked this conversation as resolved.
Show resolved Hide resolved
let code_end = (node.span.hi().0 - self.offset) as usize;

// swc index starts at 1
let code_start_index = code_start - 1;
let code_end_index = code_end - 1;

let slice = &self.code[code_start_index..code_end_index];

let Some(found) = match_re(slice) else {
return;
};

self
.magic_comments
.insert(specifier.value.to_string(), found.as_str().to_string());
}
}

fn match_re(src: &str) -> Option<String> {
RE_CHUNK_NAME.with(|re| {
let caps = re.captures(src)?;
let found = caps.name("name")?;
Some(found.as_str().to_string())
})
}
Loading
Loading