Skip to content

Commit 8bf7fbe

Browse files
filipesilvaKeen Yee Liau
authored andcommitted
test(@ngtools/webpack): add test for changing import arg name
1 parent eddb2e0 commit 8bf7fbe

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/ngtools/webpack/src/transformers/import_factory.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export function importFactory(
5959
Found 'loadChildren' with a non-string syntax in ${sourceFile.fileName} but could not transform it.
6060
Make sure it matches the format below:
6161
62-
loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME)
62+
loadChildren: () => import('IMPORT_STRING').then(M => M.EXPORT_NAME)
6363
64-
Please note that only IMPORT_STRING and EXPORT_NAME can be replaced in this format.
64+
Please note that only IMPORT_STRING, M, and EXPORT_NAME can be replaced in this format.
6565
6666
Visit https://next.angular.io/guide/ivy for more information on using Ivy.
6767
`;
@@ -97,14 +97,13 @@ function replaceImport(
9797
emitWarning: () => void,
9898
): ts.Node {
9999
// This ONLY matches the original source code format below:
100-
// loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME)
100+
// loadChildren: () => import('IMPORT_STRING').then(M => M.EXPORT_NAME)
101101
// And expects that source code to be transformed by NGC (see comment for importFactory).
102102
// It will not match nor alter variations, for instance:
103103
// - not using arrow functions
104-
// - not using `m` as the module argument
105104
// - using `await` instead of `then`
106105
// - using a default export (https://github.com/angular/angular/issues/11402)
107-
// The only parts that can change are the ones in caps: IMPORT_STRING and EXPORT_NAME.
106+
// The only parts that can change are the ones in caps: IMPORT_STRING, M and EXPORT_NAME.
108107

109108
// Exit early if the structure is not what we expect.
110109

packages/ngtools/webpack/src/transformers/import_factory_spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,29 @@ describe('@ngtools/webpack transformers', () => {
5151
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${input}`);
5252
expect(warningCalled).toBeTruthy();
5353
});
54+
55+
it('should support different arg name', () => {
56+
const input = tags.stripIndent`
57+
const ɵ0 = () => import('./lazy/lazy.module').then(a => a.LazyModule);
58+
const routes = [{
59+
path: 'lazy',
60+
loadChildren: ɵ0
61+
}];
62+
`;
63+
const output = tags.stripIndent`
64+
const ɵ0 = () => import("./lazy/lazy.module.ngfactory").then(a => a.LazyModuleNgFactory);
65+
const routes = [{
66+
path: 'lazy',
67+
loadChildren: ɵ0
68+
}];
69+
`;
70+
71+
let warningCalled = false;
72+
const transformer = importFactory(() => warningCalled = true);
73+
const result = transformTypescript(input, [transformer]);
74+
75+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
76+
expect(warningCalled).toBeFalsy();
77+
});
5478
});
5579
});

0 commit comments

Comments
 (0)