Skip to content
Merged
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
6 changes: 4 additions & 2 deletions crates/oxc_angular_compiler/src/component/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3542,6 +3542,7 @@ pub fn compile_host_bindings_for_linker(
component_name: &str,
selector: Option<&str>,
pool_starting_index: u32,
core_namespace: &str,
) -> Option<LinkerHostBindingOutput> {
let allocator = Allocator::default();
let result = compile_host_bindings_from_input(
Expand All @@ -3553,7 +3554,7 @@ pub fn compile_host_bindings_for_linker(
None, // Linker always targets latest Angular version
)?;

let emitter = JsEmitter::new();
let emitter = JsEmitter::with_core_namespace(core_namespace);

let host_vars = result.host_vars.unwrap_or(0);

Expand Down Expand Up @@ -3613,6 +3614,7 @@ pub fn compile_template_for_linker<'a>(
component_name: &str,
file_path: &str,
preserve_whitespaces: bool,
core_namespace: &str,
) -> Result<LinkerTemplateOutput, std::vec::Vec<OxcDiagnostic>> {
use crate::pipeline::ingest::{IngestOptions, ingest_component_with_options};
use oxc_allocator::FromIn;
Expand Down Expand Up @@ -3689,7 +3691,7 @@ pub fn compile_template_for_linker<'a>(
let decls = job.root.decl_count.unwrap_or(0);
let vars = job.root.vars.unwrap_or(0);

let emitter = JsEmitter::new();
let emitter = JsEmitter::with_core_namespace(core_namespace);

// Emit consts array as JS expression
let consts_js = if !job.consts.is_empty() {
Expand Down
23 changes: 23 additions & 0 deletions crates/oxc_angular_compiler/src/linker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ fn link_directive(
type_name,
selector,
0, // directives have no template, so pool starts at 0
ns,
) {
if host_output.host_vars > 0 {
parts.push(format!("hostVars: {}", host_output.host_vars));
Expand Down Expand Up @@ -1706,6 +1707,7 @@ fn link_component(
type_name,
filename,
preserve_whitespaces,
ns,
)
.ok()?;

Expand Down Expand Up @@ -1753,6 +1755,7 @@ fn link_component(
type_name,
selector,
template_output.next_pool_index,
ns,
) {
if host_output.host_vars > 0 {
parts.push(format!("hostVars: {}", host_output.host_vars));
Expand Down Expand Up @@ -2492,6 +2495,26 @@ MyComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "
);
}

/// Minifiers rename the `@angular/core` namespace import, so generated code must use
/// the file's namespace rather than `i0`.
#[test]
fn test_link_component_uses_minified_namespace() {
let allocator = Allocator::default();
let code = r#"
import * as e from "@angular/core";
class C {
}
class D {
}
D.ɵdir = e.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.0.0", ngImport: e, type: D, selector: "[d]", host: { properties: { "class.on": "on" } } });
C.ɵcmp = e.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.0.0", ngImport: e, type: C, selector: "c", host: { properties: { "class.on": "on" }, listeners: { "click": "go()" } }, template: "<div (click)=\"go()\" [title]=\"t\">{{ a | uppercase }} {{ [a, b] }}</div>@if (a) {<span i18n>Hi {{ a }}</span>} @defer {<b></b>}" });
"#;
let result = link(&allocator, code, "test.mjs");
assert!(result.linked, "Component should be linked");
assert!(result.code.contains("e.\u{0275}\u{0275}elementStart"), "got:\n{}", result.code);
assert!(!result.code.contains("i0"), "Should not reference i0, got:\n{}", result.code);
}

#[test]
fn test_link_component_with_factory() {
let allocator = Allocator::default();
Expand Down
16 changes: 14 additions & 2 deletions crates/oxc_angular_compiler/src/output/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,20 @@ fn get_source_span(expr: &OutputExpression<'_>) -> Option<Span> {
pub struct JsEmitter {
/// Whether to escape $ in strings.
escape_dollar_in_strings: bool,
/// Name to print instead of `i0` for the `@angular/core` namespace.
core_namespace: Option<String>,
}

impl JsEmitter {
/// Create a new JavaScript emitter.
pub fn new() -> Self {
Self { escape_dollar_in_strings: false }
Self { escape_dollar_in_strings: false, core_namespace: None }
}

/// Print `ns` instead of `i0` for the `@angular/core` namespace. The linker needs this
/// because a minified library may import `@angular/core` under any name.
pub fn with_core_namespace(ns: &str) -> Self {
Self { escape_dollar_in_strings: false, core_namespace: Some(ns.to_string()) }
}

/// Emit an expression to a string.
Expand Down Expand Up @@ -642,7 +650,11 @@ impl JsEmitter {
OutputExpression::ReadVar(e) => {
// Variable references are key for source mapping - map the variable name
let var_span = e.source_span.and_then(|span| ctx.span_to_source_span(span));
ctx.print_with_span(&e.name, var_span);
let name = match &self.core_namespace {
Some(ns) if e.name == "i0" => ns.as_str(),
_ => e.name.as_str(),
};
ctx.print_with_span(name, var_span);
}
OutputExpression::ReadProp(e) => {
self.visit_expression(&e.receiver, ctx);
Expand Down
Loading