Skip to content

Commit

Permalink
Track Angular polyfills (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Jan 17, 2025
1 parent 4011b23 commit e568802
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 24 deletions.
6 changes: 2 additions & 4 deletions packages/knip/fixtures/plugins/angular/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"outputPath": "dist/knip-angular-example",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": [
"zone.js"
],
"polyfills": "src/polyfill.js",
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
Expand Down Expand Up @@ -103,4 +101,4 @@
}
}
}
}
}
3 changes: 3 additions & 0 deletions packages/knip/fixtures/plugins/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"dependencies": {
"zone.js": "*"
},
"devDependencies": {
"@angular/cli": "*",
"karma": "*",
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion packages/knip/fixtures/plugins/angular2/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@
}
}
}
}
}
3 changes: 3 additions & 0 deletions packages/knip/fixtures/plugins/angular2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"scripts": {
"ng": "ng"
},
"dependencies": {
"zone.js": "*"
},
"devDependencies": {
"@angular/cli": "*",
"@angular/ssr": "*",
Expand Down
10 changes: 7 additions & 3 deletions packages/knip/fixtures/plugins/angular3/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
"ssr": {
"entry": "src/server.ts"
},
"server": "src/main.server-for-non-prod.ts"
"server": "src/main.server-for-non-prod.ts",
"polyfills": [
"src/polyfill.js"
]
},
"configurations": {
"production": {
"server": "src/main.server.ts",
"scripts": ["src/script.js"]
},
"development": {
"scripts": ["src/script-for-non-prod.js"]
"scripts": ["src/script-for-non-prod.js"],
"polyfills": ["src/polyfill-for-non-prod.js"]
}
}
},
Expand All @@ -43,4 +47,4 @@
}
}
}
}
}
Empty file.
Empty file.
40 changes: 30 additions & 10 deletions packages/knip/src/plugins/angular/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync } from 'node:fs';
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';
import { type Input, toConfig, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
import { join } from '../../util/path.js';
import { type Input, toConfig, toDeferResolve, toDependency, toEntry, toProductionEntry } from '../../util/input.js';
import { isInternal, join } from '../../util/path.js';
import { hasDependency } from '../../util/plugin.js';
import * as karma from '../karma/helpers.js';
import type {
Expand Down Expand Up @@ -44,24 +45,37 @@ const resolveConfig: ResolveConfig<AngularCLIWorkspaceConfiguration> = async (co
);
const productionEntriesByOption: EntriesByOption =
entriesByOptionByConfig.get(PRODUCTION_CONFIG_NAME) ?? new Map();
const normalizePath = (path: string) => join(cwd, path);
const isBuildTarget = targetName === BUILD_TARGET_NAME;
const maybeExternal = (option: string) => option === 'polyfills';
const toInput = (specifier: string, opts: { isProduction: boolean; maybeExternal: boolean }): Input => {
const normalizedPath = join(cwd, specifier);
// 👇 `isInternal` will report `false` for specifiers not starting with `.`
// However, relative imports are usually specified in `angular.json` without `.` prefix
// Hence checking also that file doesn't exist before considering it external
if (opts.maybeExternal && !isInternal(specifier) && !existsSync(normalizedPath)) {
return toDeferResolve(specifier);
}
return opts.isProduction ? toProductionEntry(normalizedPath) : toEntry(normalizedPath);
};
for (const [configName, entriesByOption] of entriesByOptionByConfig.entries()) {
for (const entries of entriesByOption.values()) {
for (const [option, entries] of entriesByOption.entries()) {
for (const entry of entries) {
inputs.add(
targetName === BUILD_TARGET_NAME && configName === PRODUCTION_CONFIG_NAME
? toProductionEntry(normalizePath(entry))
: toEntry(normalizePath(entry))
toInput(entry, {
isProduction: isBuildTarget && configName === PRODUCTION_CONFIG_NAME,
maybeExternal: maybeExternal(option),
})
);
}
}
}
for (const [option, entries] of defaultEntriesByOption.entries()) {
for (const entry of entries) {
inputs.add(
targetName === BUILD_TARGET_NAME && !productionEntriesByOption.get(option)?.length
? toProductionEntry(normalizePath(entry))
: toEntry(normalizePath(entry))
toInput(entry, {
isProduction: isBuildTarget && !productionEntriesByOption.get(option)?.length,
maybeExternal: maybeExternal(option),
})
);
}
}
Expand Down Expand Up @@ -109,6 +123,12 @@ const entriesByOption = (opts: TargetOptions): EntriesByOption =>
typeof scriptStringOrObject === 'string' ? scriptStringOrObject : scriptStringOrObject.input
)
: [],
polyfills:
'polyfills' in opts && opts.polyfills
? Array.isArray(opts.polyfills)
? opts.polyfills
: [opts.polyfills]
: [],
fileReplacements:
'fileReplacements' in opts && opts.fileReplacements && Array.isArray(opts.fileReplacements)
? (opts.fileReplacements as FileReplacementsBuildOption).map(fileReplacement =>
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/test/plugins/angular.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('Find dependencies with the Angular plugin', async () => {
devDependencies: 1,
unlisted: 1,
unresolved: 1,
processed: 3,
total: 3,
processed: 4,
total: 4,
});
});
8 changes: 4 additions & 4 deletions packages/knip/test/plugins/angular3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test('Find dependencies with the Angular plugin (non-production)', async () => {
assert.deepEqual(counters, {
...baseCounters,
devDependencies: 1,
processed: 8,
total: 8,
processed: 10,
total: 10,
});
});

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

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

0 comments on commit e568802

Please sign in to comment.