Skip to content

fix(@ngtools/webpack): perform import eliding before TypeScript transforms #19841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
2 changes: 1 addition & 1 deletion packages/ngtools/webpack/src/ivy/transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createAotTransformers(
const removeNgModuleScope = !options.emitNgModuleScope;
if (removeClassMetadata || removeNgModuleScope) {
// tslint:disable-next-line: no-non-null-assertion
transformers.after!.push(
transformers.before!.push(
removeIvyJitSupportCalls(removeClassMetadata, removeNgModuleScope, getTypeChecker),
);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export function elideImports(
}

const isUnused = (node: ts.Identifier) => {
// Do not remove JSX factory imports
if (node.text === compilerOptions.jsxFactory) {
return false;
}

const symbol = typeChecker.getSymbolAtLocation(node);

return symbol && !usedSymbols.has(symbol);
Expand Down
44 changes: 44 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ describe('@ngtools/webpack transformers', () => {
[propName: string]: unknown;
}
`,
'jsx.ts': `
export function createElement() {}
`,
};

it('should remove unused imports', () => {
Expand Down Expand Up @@ -348,6 +351,47 @@ describe('@ngtools/webpack transformers', () => {
});
});

it('keeps jsxFactory imports when configured', () => {
const extraCompilerOptions: ts.CompilerOptions = {
jsxFactory: 'createElement',
experimentalDecorators: true,
jsx: ts.JsxEmit.React,
};

const input = tags.stripIndent`
import { Decorator } from './decorator';
import { Service } from './service';
import { createElement } from './jsx';

const test = <p>123</p>;

@Decorator()
export class Foo {
constructor(param: Service) {
}
}

${dummyNode}
`;

const output = tags.stripIndent`
import { __decorate } from "tslib";
import { Decorator } from './decorator';
import { createElement } from './jsx';

const test = createElement("p", null, "123");

let Foo = class Foo { constructor(param) { } };
Foo = __decorate([ Decorator() ], Foo);
export { Foo };
`;

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

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

describe('should not elide imports decorator type references when emitDecoratorMetadata is true', () => {
const extraCompilerOptions: ts.CompilerOptions = {
emitDecoratorMetadata: true,
Expand Down
6 changes: 4 additions & 2 deletions packages/ngtools/webpack/src/transformers/spec_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { WebpackCompilerHost } from '../compiler_host';

// Test transform helpers.
const basePath = '/project/src/';
const fileName = basePath + 'test-file.ts';
const basefileName = basePath + 'test-file.ts';
const typeScriptLibFiles = loadTypeScriptLibFiles();
const tsLibFiles = loadTsLibFiles();

Expand All @@ -23,7 +23,9 @@ export function createTypescriptContext(
additionalFiles?: Record<string, string>,
useLibs = false,
extraCompilerOptions: ts.CompilerOptions = {},
jsxFile = false,
) {
const fileName = basefileName + (jsxFile ? 'x' : '');
// Set compiler options.
const compilerOptions: ts.CompilerOptions = {
noEmitOnError: useLibs,
Expand Down Expand Up @@ -107,7 +109,7 @@ export function transformTypescript(
}

// Return the transpiled js.
return compilerHost.readFile(fileName.replace(/\.tsx?$/, '.js'));
return compilerHost.readFile(basefileName.replace(/\.tsx?$/, '.js'));
}

function loadTypeScriptLibFiles(): Record<string, string> {
Expand Down