Skip to content

Commit e568802

Browse files
authoredJan 17, 2025··
Track Angular polyfills (#913)
1 parent 4011b23 commit e568802

File tree

11 files changed

+52
-24
lines changed

11 files changed

+52
-24
lines changed
 

‎packages/knip/fixtures/plugins/angular/angular.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
"outputPath": "dist/knip-angular-example",
2121
"index": "src/index.html",
2222
"main": "src/main.ts",
23-
"polyfills": [
24-
"zone.js"
25-
],
23+
"polyfills": "src/polyfill.js",
2624
"tsConfig": "tsconfig.app.json",
2725
"inlineStyleLanguage": "scss",
2826
"assets": [
@@ -103,4 +101,4 @@
103101
}
104102
}
105103
}
106-
}
104+
}

‎packages/knip/fixtures/plugins/angular/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"watch": "ng build --watch --configuration development",
99
"test": "ng test"
1010
},
11+
"dependencies": {
12+
"zone.js": "*"
13+
},
1114
"devDependencies": {
1215
"@angular/cli": "*",
1316
"karma": "*",

‎packages/knip/fixtures/plugins/angular/src/polyfill.js

Whitespace-only changes.

‎packages/knip/fixtures/plugins/angular2/angular.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@
105105
}
106106
}
107107
}
108-
}
108+
}

‎packages/knip/fixtures/plugins/angular2/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"scripts": {
44
"ng": "ng"
55
},
6+
"dependencies": {
7+
"zone.js": "*"
8+
},
69
"devDependencies": {
710
"@angular/cli": "*",
811
"@angular/ssr": "*",

‎packages/knip/fixtures/plugins/angular3/angular.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
"ssr": {
1717
"entry": "src/server.ts"
1818
},
19-
"server": "src/main.server-for-non-prod.ts"
19+
"server": "src/main.server-for-non-prod.ts",
20+
"polyfills": [
21+
"src/polyfill.js"
22+
]
2023
},
2124
"configurations": {
2225
"production": {
2326
"server": "src/main.server.ts",
2427
"scripts": ["src/script.js"]
2528
},
2629
"development": {
27-
"scripts": ["src/script-for-non-prod.js"]
30+
"scripts": ["src/script-for-non-prod.js"],
31+
"polyfills": ["src/polyfill-for-non-prod.js"]
2832
}
2933
}
3034
},
@@ -43,4 +47,4 @@
4347
}
4448
}
4549
}
46-
}
50+
}

‎packages/knip/fixtures/plugins/angular3/src/polyfill-for-non-prod.js

Whitespace-only changes.

‎packages/knip/fixtures/plugins/angular3/src/polyfill.js

Whitespace-only changes.

‎packages/knip/src/plugins/angular/index.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { existsSync } from 'node:fs';
12
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';
2-
import { type Input, toConfig, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
3-
import { join } from '../../util/path.js';
3+
import { type Input, toConfig, toDeferResolve, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
4+
import { isInternal, join } from '../../util/path.js';
45
import { hasDependency } from '../../util/plugin.js';
56
import * as karma from '../karma/helpers.js';
67
import type {
@@ -44,24 +45,37 @@ const resolveConfig: ResolveConfig<AngularCLIWorkspaceConfiguration> = async (co
4445
);
4546
const productionEntriesByOption: EntriesByOption =
4647
entriesByOptionByConfig.get(PRODUCTION_CONFIG_NAME) ?? new Map();
47-
const normalizePath = (path: string) => join(cwd, path);
48+
const isBuildTarget = targetName === BUILD_TARGET_NAME;
49+
const maybeExternal = (option: string) => option === 'polyfills';
50+
const toInput = (specifier: string, opts: { isProduction: boolean; maybeExternal: boolean }): Input => {
51+
const normalizedPath = join(cwd, specifier);
52+
// 👇 `isInternal` will report `false` for specifiers not starting with `.`
53+
// However, relative imports are usually specified in `angular.json` without `.` prefix
54+
// Hence checking also that file doesn't exist before considering it external
55+
if (opts.maybeExternal && !isInternal(specifier) && !existsSync(normalizedPath)) {
56+
return toDeferResolve(specifier);
57+
}
58+
return opts.isProduction ? toProductionEntry(normalizedPath) : toEntry(normalizedPath);
59+
};
4860
for (const [configName, entriesByOption] of entriesByOptionByConfig.entries()) {
49-
for (const entries of entriesByOption.values()) {
61+
for (const [option, entries] of entriesByOption.entries()) {
5062
for (const entry of entries) {
5163
inputs.add(
52-
targetName === BUILD_TARGET_NAME && configName === PRODUCTION_CONFIG_NAME
53-
? toProductionEntry(normalizePath(entry))
54-
: toEntry(normalizePath(entry))
64+
toInput(entry, {
65+
isProduction: isBuildTarget && configName === PRODUCTION_CONFIG_NAME,
66+
maybeExternal: maybeExternal(option),
67+
})
5568
);
5669
}
5770
}
5871
}
5972
for (const [option, entries] of defaultEntriesByOption.entries()) {
6073
for (const entry of entries) {
6174
inputs.add(
62-
targetName === BUILD_TARGET_NAME && !productionEntriesByOption.get(option)?.length
63-
? toProductionEntry(normalizePath(entry))
64-
: toEntry(normalizePath(entry))
75+
toInput(entry, {
76+
isProduction: isBuildTarget && !productionEntriesByOption.get(option)?.length,
77+
maybeExternal: maybeExternal(option),
78+
})
6579
);
6680
}
6781
}
@@ -109,6 +123,12 @@ const entriesByOption = (opts: TargetOptions): EntriesByOption =>
109123
typeof scriptStringOrObject === 'string' ? scriptStringOrObject : scriptStringOrObject.input
110124
)
111125
: [],
126+
polyfills:
127+
'polyfills' in opts && opts.polyfills
128+
? Array.isArray(opts.polyfills)
129+
? opts.polyfills
130+
: [opts.polyfills]
131+
: [],
112132
fileReplacements:
113133
'fileReplacements' in opts && opts.fileReplacements && Array.isArray(opts.fileReplacements)
114134
? (opts.fileReplacements as FileReplacementsBuildOption).map(fileReplacement =>

‎packages/knip/test/plugins/angular.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('Find dependencies with the Angular plugin', async () => {
2222
devDependencies: 1,
2323
unlisted: 1,
2424
unresolved: 1,
25-
processed: 3,
26-
total: 3,
25+
processed: 4,
26+
total: 4,
2727
});
2828
});

‎packages/knip/test/plugins/angular3.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ test('Find dependencies with the Angular plugin (non-production)', async () => {
1818
assert.deepEqual(counters, {
1919
...baseCounters,
2020
devDependencies: 1,
21-
processed: 8,
22-
total: 8,
21+
processed: 10,
22+
total: 10,
2323
});
2424
});
2525

@@ -32,7 +32,7 @@ test('Find dependencies with the Angular plugin (production)', async () => {
3232

3333
assert.deepEqual(counters, {
3434
...baseCounters,
35-
processed: 4,
36-
total: 4,
35+
processed: 5,
36+
total: 5,
3737
});
3838
});

0 commit comments

Comments
 (0)
Please sign in to comment.