Skip to content

Commit

Permalink
Track Angular environment files as entry points (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Dec 2, 2024
1 parent f24abfb commit 7950bf3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/knip/fixtures/plugins/angular2/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
"sourceMap": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.development.ts"
}
]
}
},
"defaultConfiguration": "production"
Expand Down
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/angular2/src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {environment} from "./environments/environment";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const environment = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const environment = {}
29 changes: 26 additions & 3 deletions packages/knip/src/plugins/angular/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';
import { type Input, toConfig, toDependency, toProductionEntry } from '../../util/input.js';
import { type Input, toConfig, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
import { join } from '../../util/path.js';
import { hasDependency } from '../../util/plugin.js';
import type { AngularCLIWorkspaceConfiguration } from './types.js';
import type { AngularCLIWorkspaceConfiguration, WebpackBrowserSchemaForBuildFacade } from './types.js';

// https://angular.io/guide/workspace-config

Expand All @@ -26,7 +26,7 @@ const resolveConfig: ResolveConfig<AngularCLIWorkspaceConfiguration> = async (co
for (const project of Object.values(config.projects)) {
if (!project.architect) return [];
for (const target of Object.values(project.architect)) {
const { options: opts } = target;
const { options: opts, configurations: configs } = target;
const [packageName] = typeof target.builder === 'string' ? target.builder.split(':') : [];
if (typeof packageName === 'string') inputs.add(toDependency(packageName));
if (opts) {
Expand All @@ -47,13 +47,36 @@ const resolveConfig: ResolveConfig<AngularCLIWorkspaceConfiguration> = async (co
if ('server' in opts && opts.server && typeof opts.server === 'string') {
inputs.add(toProductionEntry(join(cwd, opts.server)));
}
if ('fileReplacements' in opts && opts.fileReplacements && Array.isArray(opts.fileReplacements)) {
for (const fileReplacedBy of filesReplacedBy(opts.fileReplacements)) {
inputs.add(toEntry(fileReplacedBy));
}
}
}
if (configs) {
for (const [configName, config] of Object.entries(configs)) {
const isProductionConfig = configName === 'production';
if ('fileReplacements' in config && config.fileReplacements && Array.isArray(config.fileReplacements)) {
for (const fileReplacedBy of filesReplacedBy(config.fileReplacements)) {
inputs.add(isProductionConfig ? toProductionEntry(fileReplacedBy) : toEntry(fileReplacedBy));
}
}
}
}
}
}

return Array.from(inputs);
};

const filesReplacedBy = (
//👇 Using Webpack-based browser schema to support old `replaceWith` file replacements
fileReplacements: Exclude<WebpackBrowserSchemaForBuildFacade['fileReplacements'], undefined>
): readonly string[] =>
fileReplacements.map(fileReplacement =>
'with' in fileReplacement ? fileReplacement.with : fileReplacement.replaceWith
);

export default {
title,
enablers,
Expand Down
2 changes: 1 addition & 1 deletion packages/knip/src/plugins/angular/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ interface AppShellTarget {
/**
* Browser target options
*/
interface WebpackBrowserSchemaForBuildFacade {
export interface WebpackBrowserSchemaForBuildFacade {
/**
* List of static application assets.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/test/plugins/angular2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('Find dependencies with the Angular plugin (2)', async () => {

assert.deepEqual(counters, {
...baseCounters,
processed: 3,
total: 3,
processed: 5,
total: 5,
});
});

0 comments on commit 7950bf3

Please sign in to comment.