Skip to content

Commit

Permalink
Use toDeferResolve for external polyfills
Browse files Browse the repository at this point in the history
As suggested by @webpro
  • Loading branch information
davidlj95 committed Jan 16, 2025
1 parent 0c75e54 commit 1a78aae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
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
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
34 changes: 24 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 ? toEntry(normalizedPath) : toProductionEntry(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

0 comments on commit 1a78aae

Please sign in to comment.