1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! Angular framework plugin.
//!
//! Detects Angular projects and marks component, module, service, guard,
//! pipe, directive, resolver, and interceptor files as entry points.
//! Parses `angular.json` to extract styles, scripts, main, and polyfills
//! from build targets as additional entry points. For ng-packagr library
//! packages, parses `ng-package.json` / `ng-package.prod.json` and treats
//! `lib.entryFile` (default `src/public_api.ts`) as the package public API
//! entry point so the library surface stays reachable.
/// ng-packagr's documented default `lib.entryFile` when the field is omitted.
/// Matches ng-packagr's `ng-package.schema.json` (`"default": "src/public_api.ts"`,
/// underscore); modern libraries scaffolded with a hyphenated `public-api.ts` set
/// `entryFile` explicitly, so the default only applies to configs that omit it.
const NG_PACKAGE_DEFAULT_ENTRY_FILE: &str = "src/public_api.ts";
use std::path::Path;
use super::config_parser;
use super::{Plugin, PluginResult};
define_plugin!(
struct AngularPlugin => "angular",
enablers: &["@angular/core", "ng-packagr"],
entry_patterns: &[
// Standard Angular CLI layout
"src/main.ts",
"src/app/**/*.component.ts",
"src/app/**/*.module.ts",
"src/app/**/*.service.ts",
"src/app/**/*.guard.ts",
"src/app/**/*.pipe.ts",
"src/app/**/*.directive.ts",
"src/app/**/*.resolver.ts",
"src/app/**/*.interceptor.ts",
// Nx monorepo layout (apps and libs under arbitrary paths)
"**/src/main.ts",
"**/src/app/**/*.component.ts",
"**/src/app/**/*.module.ts",
"**/src/app/**/*.service.ts",
"**/src/app/**/*.guard.ts",
"**/src/app/**/*.pipe.ts",
"**/src/app/**/*.directive.ts",
"**/src/app/**/*.resolver.ts",
"**/src/app/**/*.interceptor.ts",
],
config_patterns: &[
"angular.json",
".angular.json",
"ng-package.json",
"ng-package.prod.json",
],
always_used: &[
"angular.json",
".angular.json",
"src/polyfills.ts",
"src/environments/**/*.ts",
// Angular 17+ standalone app bootstrap config (runtime, not tool config)
"src/app/app.config.ts",
"src/app/app.config.server.ts",
],
tooling_dependencies: &[
"@angular/cli",
"@angular-devkit/build-angular",
"@angular/compiler-cli",
"@angular/compiler",
"@angular/build",
// ng-packagr is a build tool invoked via `ng build` / scripts, never
// imported in source; without this, a library that activates the plugin
// via `ng-packagr` would report `ng-packagr` itself as unused.
"ng-packagr",
"zone.js",
"tslib",
// Peer dependencies of @angular/core that may not be directly imported
// but are required by the Angular framework at runtime
"rxjs",
"@angular/common",
"@angular/platform-browser",
"@angular/platform-browser-dynamic",
],
resolve_config(config_path, source, _root) {
let mut result = PluginResult::default();
// ng-package.json / ng-package.prod.json: ng-packagr library entry files.
// Treat `lib.entryFile` (default `src/public_api.ts`) as the package's
// public API entry point, resolved relative to the config directory.
// Nested secondary-entry-point configs in the package subtree (which the
// non-recursive config discovery never reaches) are scanned too.
if is_ng_package_config(config_path) {
for entry in resolve_ng_package_entries(config_path, source, _root) {
result.push_entry_pattern(entry);
}
return result;
}
// angular.json: projects.*.architect.build.options.styles -> entry patterns
// These are CSS/SCSS files loaded by the Angular CLI build system.
let styles = config_parser::extract_config_object_nested_string_or_array(
source,
config_path,
&["projects"],
&["architect", "build", "options", "styles"],
);
for style in &styles {
let path = style.trim_start_matches("./");
result.push_entry_pattern(path.to_string());
}
// angular.json: projects.*.architect.build.options.scripts -> entry patterns
let scripts = config_parser::extract_config_object_nested_string_or_array(
source,
config_path,
&["projects"],
&["architect", "build", "options", "scripts"],
);
for script in &scripts {
let path = script.trim_start_matches("./");
result.push_entry_pattern(path.to_string());
}
// angular.json: projects.*.architect.build.options.main -> entry patterns
// Also check "browser" -- newer Angular CLI uses "browser" instead of "main"
for field in &["main", "browser"] {
let mains = config_parser::extract_config_object_nested_strings(
source,
config_path,
&["projects"],
&["architect", "build", "options", field],
);
for main in &mains {
let path = main.trim_start_matches("./");
result.push_entry_pattern(path.to_string());
}
}
// angular.json: projects.*.architect.build.options.polyfills -> entry patterns
// Can be a string or array
let polyfills = config_parser::extract_config_object_nested_string_or_array(
source,
config_path,
&["projects"],
&["architect", "build", "options", "polyfills"],
);
for polyfill in &polyfills {
let trimmed = polyfill.trim_start_matches("./");
// Skip npm package references like "zone.js" -- only add file paths.
// File paths contain "/" (directory separators) or start with "src/", etc.
// Bare package names like "zone.js" have no "/" and shouldn't be entry points.
if trimmed.contains('/') {
result.push_entry_pattern(trimmed.to_string());
}
}
// angular.json: projects.*.architect.test.options.main -> entry patterns
let test_mains = config_parser::extract_config_object_nested_strings(
source,
config_path,
&["projects"],
&["architect", "test", "options", "main"],
);
for main in &test_mains {
let path = main.trim_start_matches("./");
result.push_entry_pattern(path.to_string());
}
// angular.json: projects.*.architect.build.options.stylePreprocessorOptions.includePaths
// Angular CLI resolves bare SCSS imports (`@import 'variables'`) by
// searching these directories. Without threading them into fallow's
// SCSS resolver, the imports become false-positive unresolved imports.
// Paths are resolved relative to the workspace/project root per the
// Angular workspace configuration reference. See issue #103.
let include_paths = config_parser::extract_config_object_nested_string_or_array(
source,
config_path,
&["projects"],
&[
"architect",
"build",
"options",
"stylePreprocessorOptions",
"includePaths",
],
);
result
.scss_include_paths
.extend(resolve_scss_include_paths(&include_paths, _root));
result
},
);
/// Resolve each SCSS include path entry to an absolute directory.
///
/// Skips entries whose resolved directory does not exist on disk — a missing
/// include path cannot resolve anything and would only waste syscalls during
/// SCSS resolution.
fn resolve_scss_include_paths(entries: &[String], root: &Path) -> Vec<std::path::PathBuf> {
entries
.iter()
.map(|entry| root.join(entry.trim_start_matches("./")))
.filter(|path| path.is_dir())
.collect()
}
/// Maximum subdirectory depth scanned for nested (secondary-entry-point)
/// ng-package configs, relative to the primary config's directory. ng-packagr
/// secondary entry points sit a level or two below the primary; the cap bounds
/// the walk on pathological trees.
const NG_PACKAGE_SCAN_MAX_DEPTH: usize = 6;
/// Directory names never traversed when scanning for nested ng-package configs.
const NG_PACKAGE_SCAN_SKIP_DIRS: &[&str] = &["node_modules", "dist", "out", "tmp", "coverage"];
/// Whether `name` is an ng-packagr library-descriptor file name.
fn is_ng_package_file_name(name: &str) -> bool {
name == "ng-package.json" || name == "ng-package.prod.json"
}
/// Whether `config_path` is an ng-packagr library descriptor.
fn is_ng_package_config(config_path: &Path) -> bool {
config_path
.file_name()
.and_then(|name| name.to_str())
.is_some_and(is_ng_package_file_name)
}
/// Resolve every ng-packagr entry file reachable from a primary ng-package
/// config: the primary `lib.entryFile` plus each nested secondary-entry-point
/// config in the package subtree. Each entry file is resolved relative to the
/// directory of the config that declares it, matching ng-packagr's per-entry
/// semantics. Returns deduped project-relative entry patterns (workspace runs
/// have them prefixed back by the registry).
fn resolve_ng_package_entries(config_path: &Path, source: &str, root: &Path) -> Vec<String> {
let mut entries = Vec::new();
if let Some(entry) = resolve_ng_package_entry_from_source(config_path, source, root) {
entries.push(entry);
}
// Secondary entry points live in nested `ng-package.json` files in
// subdirectories that the non-recursive config discovery never reaches, so
// scan the package subtree ourselves. Same-directory sibling configs (e.g.
// a `ng-package.prod.json` next to the primary) are left to discovery,
// which surfaces them as their own `resolve_config` calls.
if let Some(base) = config_path.parent() {
let mut nested = Vec::new();
collect_nested_ng_package_configs(base, 0, &mut nested);
for nested_path in nested {
let Ok(nested_source) = std::fs::read_to_string(&nested_path) else {
continue;
};
if let Some(entry) =
resolve_ng_package_entry_from_source(&nested_path, &nested_source, root)
{
entries.push(entry);
}
}
}
entries.sort();
entries.dedup();
entries
}
/// Resolve a single ng-package config's `lib.entryFile` to a project-relative
/// entry pattern, falling back to ng-packagr's documented default
/// (`src/public_api.ts`) when omitted or empty. Returns `None` only when
/// normalization escapes the project root.
fn resolve_ng_package_entry_from_source(
config_path: &Path,
source: &str,
root: &Path,
) -> Option<String> {
let entry_file =
config_parser::extract_config_string(source, config_path, &["lib", "entryFile"])
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| NG_PACKAGE_DEFAULT_ENTRY_FILE.to_string());
config_parser::normalize_config_path(&entry_file, config_path, root)
}
/// Recursively collect nested ng-package config paths in subdirectories of
/// `dir`, bounded by depth and a skip-dir list. Configs in `dir` itself
/// (depth 0) are intentionally not collected: they are either the primary
/// config or same-directory siblings that config discovery already surfaces.
fn collect_nested_ng_package_configs(
dir: &Path,
depth: usize,
found: &mut Vec<std::path::PathBuf>,
) {
if depth > NG_PACKAGE_SCAN_MAX_DEPTH {
return;
}
let Ok(read_dir) = std::fs::read_dir(dir) else {
return;
};
for entry in read_dir.flatten() {
let Ok(file_type) = entry.file_type() else {
continue;
};
let path = entry.path();
if file_type.is_dir() {
let skip = path
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| {
name.starts_with('.') || NG_PACKAGE_SCAN_SKIP_DIRS.contains(&name)
});
if !skip {
collect_nested_ng_package_configs(&path, depth + 1, found);
}
} else if depth >= 1
&& path
.file_name()
.and_then(|name| name.to_str())
.is_some_and(is_ng_package_file_name)
{
found.push(path);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn has_entry_pattern(result: &PluginResult, pattern: &str) -> bool {
result
.entry_patterns
.iter()
.any(|entry_pattern| entry_pattern.pattern == pattern)
}
#[test]
fn resolve_config_extracts_styles() {
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"styles": ["src/styles.css", "src/theme.scss"]
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result =
plugin.resolve_config(Path::new("angular.json"), source, Path::new("/project"));
assert!(has_entry_pattern(&result, "src/styles.css"));
assert!(has_entry_pattern(&result, "src/theme.scss"));
}
#[test]
fn resolve_config_extracts_styles_object_form() {
// Angular CLI schema: `styles` entries can be `{ input, bundleName, inject }`.
// Used for vendor stylesheets that must opt out of auto-injection.
// Previously silently dropped. See #126.
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"styles": [
"src/styles.scss",
{ "input": "src/theme.scss", "bundleName": "theme", "inject": false }
]
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result =
plugin.resolve_config(Path::new("angular.json"), source, Path::new("/project"));
assert!(has_entry_pattern(&result, "src/styles.scss"));
assert!(
has_entry_pattern(&result, "src/theme.scss"),
"object-form entry `input` must be extracted as entry pattern"
);
}
#[test]
fn resolve_config_extracts_main() {
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"main": "src/main.ts"
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result =
plugin.resolve_config(Path::new("angular.json"), source, Path::new("/project"));
assert!(has_entry_pattern(&result, "src/main.ts"));
}
#[test]
fn resolve_config_extracts_scripts() {
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"scripts": ["node_modules/some-lib/dist/script.js"]
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result =
plugin.resolve_config(Path::new("angular.json"), source, Path::new("/project"));
assert!(has_entry_pattern(
&result,
"node_modules/some-lib/dist/script.js"
));
}
#[test]
fn resolve_config_multiple_projects() {
let source = r#"{
"projects": {
"app-one": {
"architect": {
"build": {
"options": {
"styles": ["apps/one/src/styles.css"],
"main": "apps/one/src/main.ts"
}
}
}
},
"app-two": {
"architect": {
"build": {
"options": {
"styles": ["apps/two/src/styles.css"],
"main": "apps/two/src/main.ts"
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result =
plugin.resolve_config(Path::new("angular.json"), source, Path::new("/project"));
assert!(has_entry_pattern(&result, "apps/one/src/styles.css"));
assert!(has_entry_pattern(&result, "apps/two/src/styles.css"));
assert!(has_entry_pattern(&result, "apps/one/src/main.ts"));
assert!(has_entry_pattern(&result, "apps/two/src/main.ts"));
}
#[test]
fn resolve_config_extracts_scss_include_paths() {
// Issue #103: stylePreprocessorOptions.includePaths must be threaded
// through to the SCSS resolver. On-disk existence is checked in the
// plugin so the test creates the directory.
let tmp = tempfile::tempdir().expect("create temp dir");
let root = tmp.path();
std::fs::create_dir_all(root.join("src/styles")).unwrap();
std::fs::create_dir_all(root.join("libs/shared/scss")).unwrap();
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"stylePreprocessorOptions": {
"includePaths": ["src/styles", "./libs/shared/scss"]
}
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(Path::new("angular.json"), source, root);
assert_eq!(result.scss_include_paths.len(), 2);
assert!(result.scss_include_paths.contains(&root.join("src/styles")));
assert!(
result
.scss_include_paths
.contains(&root.join("libs/shared/scss"))
);
}
#[test]
fn resolve_config_scss_include_paths_skips_missing_dirs() {
// Missing directories are filtered out so they don't trigger pointless
// filesystem lookups during SCSS resolution.
let tmp = tempfile::tempdir().expect("create temp dir");
let root = tmp.path();
std::fs::create_dir_all(root.join("src/styles")).unwrap();
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"stylePreprocessorOptions": {
"includePaths": ["src/styles", "missing/dir"]
}
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(Path::new("angular.json"), source, root);
assert_eq!(result.scss_include_paths.len(), 1);
assert_eq!(result.scss_include_paths[0], root.join("src/styles"));
}
#[test]
fn resolve_config_ng_package_entry_file() {
// ng-package.json lib.entryFile is credited as an entry point, resolved
// relative to the config directory.
let source = r#"{
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
"dest": "./dist",
"lib": {
"entryFile": "src/public-api.ts"
}
}"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.json"),
source,
Path::new("/project"),
);
assert!(has_entry_pattern(&result, "src/public-api.ts"));
}
#[test]
fn resolve_config_ng_package_entry_file_nested_dir() {
// In a monorepo, the entry file resolves relative to the directory
// containing ng-package.json (workspace-package root), not the project
// root. The registry prefixes the pattern back with the workspace prefix.
let source = r#"{
"lib": { "entryFile": "src/public-api.ts" }
}"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/repo/packages/angular/ng-package.json"),
source,
Path::new("/repo"),
);
assert!(has_entry_pattern(
&result,
"packages/angular/src/public-api.ts"
));
}
#[test]
fn resolve_config_ng_package_entry_file_default_when_omitted() {
// ng-packagr defaults lib.entryFile to src/public_api.ts (underscore).
let source = r#"{ "dest": "./dist", "lib": {} }"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.json"),
source,
Path::new("/project"),
);
assert!(has_entry_pattern(&result, "src/public_api.ts"));
}
#[test]
fn resolve_config_ng_package_default_when_lib_absent() {
// No `lib` key at all still falls back to the documented default.
let source = r#"{ "$schema": "x", "dest": "./dist" }"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.json"),
source,
Path::new("/project"),
);
assert!(has_entry_pattern(&result, "src/public_api.ts"));
}
#[test]
fn resolve_config_ng_package_prod_variant() {
// ng-package.prod.json is handled identically.
let source = r#"{ "lib": { "entryFile": "src/prod-api.ts" } }"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.prod.json"),
source,
Path::new("/project"),
);
assert!(has_entry_pattern(&result, "src/prod-api.ts"));
}
#[test]
fn resolve_config_ng_package_empty_entry_file_uses_default() {
// An explicitly empty entryFile is treated as omitted.
let source = r#"{ "lib": { "entryFile": "" } }"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.json"),
source,
Path::new("/project"),
);
assert!(has_entry_pattern(&result, "src/public_api.ts"));
}
#[test]
fn resolve_config_ng_package_malformed_does_not_panic() {
// Malformed JSON yields no entry pattern and does not panic.
let source = "{ this is not valid json";
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.json"),
source,
Path::new("/project"),
);
// The default fallback still applies since the named config matched; a
// malformed body simply means entryFile extraction returns None.
assert!(has_entry_pattern(&result, "src/public_api.ts"));
}
#[test]
fn resolve_config_ng_package_collects_nested_secondary_entries() {
// ng-packagr secondary entry points live in nested ng-package.json
// files; each entryFile resolves relative to its own directory.
let tmp = tempfile::tempdir().expect("temp dir");
let root = tmp.path();
std::fs::create_dir_all(root.join("client")).unwrap();
std::fs::create_dir_all(root.join("server")).unwrap();
std::fs::write(
root.join("ng-package.json"),
r#"{ "lib": { "entryFile": "src/public-api.ts" } }"#,
)
.unwrap();
std::fs::write(
root.join("client/ng-package.json"),
r#"{ "lib": { "entryFile": "src/public_api.ts" } }"#,
)
.unwrap();
// Secondary entry omitting entryFile falls back to the default
// (ng-packagr's `src/public_api.ts`, underscore).
std::fs::write(root.join("server/ng-package.json"), r"{}").unwrap();
let source = std::fs::read_to_string(root.join("ng-package.json")).unwrap();
let plugin = AngularPlugin;
let result = plugin.resolve_config(&root.join("ng-package.json"), &source, root);
assert!(has_entry_pattern(&result, "src/public-api.ts"));
assert!(has_entry_pattern(&result, "client/src/public_api.ts"));
assert!(has_entry_pattern(&result, "server/src/public_api.ts"));
}
#[test]
fn resolve_config_ng_package_skips_node_modules_nested_configs() {
// A vendored ng-package.json inside node_modules must not be collected.
let tmp = tempfile::tempdir().expect("temp dir");
let root = tmp.path();
std::fs::create_dir_all(root.join("node_modules/some-lib")).unwrap();
std::fs::write(
root.join("ng-package.json"),
r#"{ "lib": { "entryFile": "src/public-api.ts" } }"#,
)
.unwrap();
std::fs::write(
root.join("node_modules/some-lib/ng-package.json"),
r#"{ "lib": { "entryFile": "src/leaked.ts" } }"#,
)
.unwrap();
let source = std::fs::read_to_string(root.join("ng-package.json")).unwrap();
let plugin = AngularPlugin;
let result = plugin.resolve_config(&root.join("ng-package.json"), &source, root);
assert!(has_entry_pattern(&result, "src/public-api.ts"));
assert!(
!has_entry_pattern(&result, "node_modules/some-lib/src/leaked.ts"),
"node_modules configs must not be collected: {:?}",
result.entry_patterns
);
}
#[test]
fn resolve_config_ng_package_same_dir_sibling_left_to_discovery() {
// A same-directory ng-package.prod.json is surfaced by config discovery
// as its own resolve_config call, so the primary's subtree scan (which
// only collects depth >= 1) must not also emit its entry here.
let tmp = tempfile::tempdir().expect("temp dir");
let root = tmp.path();
std::fs::write(
root.join("ng-package.json"),
r#"{ "lib": { "entryFile": "src/public-api.ts" } }"#,
)
.unwrap();
std::fs::write(
root.join("ng-package.prod.json"),
r#"{ "lib": { "entryFile": "src/prod-api.ts" } }"#,
)
.unwrap();
let source = std::fs::read_to_string(root.join("ng-package.json")).unwrap();
let plugin = AngularPlugin;
let result = plugin.resolve_config(&root.join("ng-package.json"), &source, root);
assert_eq!(result.entry_patterns.len(), 1);
assert!(has_entry_pattern(&result, "src/public-api.ts"));
}
#[test]
fn resolve_config_ng_package_does_not_run_angular_json_extractors() {
// ng-package.json must not pick up angular.json `projects.*` style/main
// extraction; only the entry-file pattern is emitted.
let source = r#"{ "lib": { "entryFile": "src/public-api.ts" } }"#;
let plugin = AngularPlugin;
let result = plugin.resolve_config(
Path::new("/project/ng-package.json"),
source,
Path::new("/project"),
);
assert_eq!(result.entry_patterns.len(), 1);
assert!(has_entry_pattern(&result, "src/public-api.ts"));
}
#[test]
fn resolve_config_polyfills_skips_packages() {
let source = r#"{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"polyfills": ["zone.js", "src/polyfills.ts"]
}
}
}
}
}
}"#;
let plugin = AngularPlugin;
let result =
plugin.resolve_config(Path::new("angular.json"), source, Path::new("/project"));
// zone.js is a package, not a file — should be skipped
assert!(!has_entry_pattern(&result, "zone.js"));
// src/polyfills.ts is a file path — should be included
assert!(has_entry_pattern(&result, "src/polyfills.ts"));
}
#[test]
fn ng_packagr_is_enabler_and_tooling_dependency() {
let plugin = AngularPlugin;
assert!(plugin.enablers().contains(&"ng-packagr"));
// ng-packagr is a build tool invoked via `ng build` / scripts, never
// imported in source. It must be credited as a tooling dependency so a
// library that activates the plugin via `ng-packagr` does not then
// report `ng-packagr` itself as an unused dependency.
assert!(plugin.tooling_dependencies().contains(&"ng-packagr"));
}
}