Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export function elideImports(

// Note: with `verbatimModuleSyntax`, TypeScript reports an error for imports of non-value aliases
// that do not use a `type` modifier (TS1484/TS1485), so no special handling is needed for it.
if ((symbol.flags & ts.SymbolFlags.Alias) !== 0 && !isValueAlias(symbol, typeChecker)) {
if (
(symbol.flags & ts.SymbolFlags.Alias) !== 0 &&
!isValueAlias(symbol, typeChecker, compilerOptions)
) {
return 'type';
}

Expand Down Expand Up @@ -210,9 +213,14 @@ export function elideImports(
/**
* Determines whether an import alias resolves to a runtime value. Aliases that only resolve to
* types, or that pass through a type-only import or export declaration anywhere along the alias
* chain, are always elided by TypeScript.
* chain, are always elided by TypeScript. The same applies to const enums, as their usages are
* inlined, unless const enums are preserved.
*/
function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean {
function isValueAlias(
symbol: ts.Symbol,
typeChecker: ts.TypeChecker,
compilerOptions: ts.CompilerOptions,
): boolean {
const seen = new Set<ts.Symbol>();
let current = symbol;

Expand All @@ -238,5 +246,13 @@ function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean {
current = next;
}

if ((current.flags & ts.SymbolFlags.ConstEnum) !== 0) {
return !!(
compilerOptions.preserveConstEnums ||
compilerOptions.isolatedModules ||
compilerOptions.verbatimModuleSyntax
);
}

return (current.flags & ts.SymbolFlags.Value) !== 0;
}
94 changes: 94 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,76 @@ describe('elide_imports', () => {

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should not retain inlined const enum imports when a value specifier is removed from a declaration', () => {
const input = tags.stripIndent`
import { MixedEnum, MixedService } from './mixed';

export const value = MixedEnum.A;

const remove = MixedService;
`;

const output = tags.stripIndent`
export const value = 1 /* MixedEnum.A */;
`;

const { program, compilerHost } = createTypescriptContext(
input,
additionalFiles,
true,
extraCompilerOptions,
);
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should retain const enum imports when a value specifier is removed and isolatedModules is enabled', () => {
const input = tags.stripIndent`
import { MixedEnum, MixedService } from './mixed';

export const value = MixedEnum.A;

const remove = MixedService;
`;

const output = tags.stripIndent`
import { MixedEnum } from './mixed';
export const value = MixedEnum.A;
`;

const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, {
...extraCompilerOptions,
isolatedModules: true,
});
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should retain const enum imports when a value specifier is removed and preserveConstEnums is enabled', () => {
const input = tags.stripIndent`
import { MixedEnum, MixedService } from './mixed';

export const value = MixedEnum.A;

const remove = MixedService;
`;

const output = tags.stripIndent`
import { MixedEnum } from './mixed';
export const value = 1 /* MixedEnum.A */;
`;

const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, {
...extraCompilerOptions,
preserveConstEnums: true,
});
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});
});

it('keeps jsxFactory imports when configured', () => {
Expand Down Expand Up @@ -1072,6 +1142,30 @@ describe('elide_imports', () => {
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should not retain inlined const enum imports when a value specifier is removed from a declaration', () => {
const input = tags.stripIndent`
import { MixedEnum, MixedService } from './mixed';

export const value = MixedEnum.A;

const remove = MixedService;
`;

const output = tags.stripIndent`
export const value = 1 /* MixedEnum.A */;
`;

const { program, compilerHost } = createTypescriptContext(
input,
additionalFiles,
true,
extraCompilerOptions,
);
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should elide imports that resolve through type-only re-exports', () => {
const input = tags.stripIndent`
import { Decorator } from './decorator';
Expand Down
Loading