Skip to content

Commit

Permalink
feat(frontend): change lazy routes to use dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed May 10, 2019
1 parent 3d7ddbe commit bed2ae7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
24 changes: 0 additions & 24 deletions packages/angular/src/schematics/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,6 @@ function addTsconfigs(options: NormalizedSchema): Rule {
move(options.appProjectRoot)
])
)
// options.e2eTestRunner === 'protractor'
// ? mergeWith(
// apply(url('./files'), [
// template({
// ...options,
// offsetFromRoot: offsetFromRoot(options.e2eProjectRoot)
// }),
// move(options.e2eProjectRoot)
// ])
// )
// : noop()
]);
}

Expand Down Expand Up @@ -312,19 +301,6 @@ function updateE2eProject(options: NormalizedSchema): Rule {
delete json.projects[options.name].architect.e2e;
return json;
}),
updateJsonInTree(`${options.e2eProjectRoot}/tsconfig.json`, json => {
return {
...json,
compilerOptions: {
...json.compilerOptions,
types: [
...(json.compilerOptions.types || []),
'jasmine',
'jasminewd2'
]
}
};
}),
updateJsonInTree(`${options.e2eProjectRoot}/tsconfig.e2e.json`, json => {
return {
...json,
Expand Down
28 changes: 20 additions & 8 deletions packages/angular/src/schematics/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ describe('lib', () => {
expect(moduleContents).toContain(`
{
path: 'my-dir-my-lib',
loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule'
loadChildren: () =>
import('@proj/my-dir/my-lib').then(module => module.MyDirMyLibModule)
}`);

const tsConfigAppJson = JSON.parse(
Expand Down Expand Up @@ -516,12 +517,16 @@ describe('lib', () => {
expect(moduleContents2).toContain(`
{
path: 'my-dir-my-lib',
loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule'
loadChildren: () =>
import('@proj/my-dir/my-lib').then(module => module.MyDirMyLibModule)
}`);
expect(moduleContents2).toContain(`
{
path: 'my-dir-my-lib2',
loadChildren: '@proj/my-dir/my-lib2#MyDirMyLib2Module'
loadChildren: () =>
import('@proj/my-dir/my-lib2').then(
module => module.MyDirMyLib2Module
)
}`);

const tsConfigAppJson2 = JSON.parse(
Expand Down Expand Up @@ -556,16 +561,23 @@ describe('lib', () => {
expect(moduleContents3).toContain(`
{
path: 'my-dir-my-lib',
loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule'
loadChildren: () =>
import('@proj/my-dir/my-lib').then(module => module.MyDirMyLibModule)
}`);
expect(moduleContents3).toContain(`
{
path: 'my-dir-my-lib2',
loadChildren: '@proj/my-dir/my-lib2#MyDirMyLib2Module'
loadChildren: () =>
import('@proj/my-dir/my-lib2').then(
module => module.MyDirMyLib2Module
)
}`);
expect(moduleContents3).toContain(`
{
path: 'my-lib3',
loadChildren: () =>
import('@proj/my-dir/my-lib3').then(module => module.MyLib3Module)
}`);
expect(moduleContents3).toContain(
`{ path: 'my-lib3', loadChildren: '@proj/my-dir/my-lib3#MyLib3Module' }`
);

const tsConfigAppJson3 = JSON.parse(
stripJsonComments(
Expand Down
8 changes: 3 additions & 5 deletions packages/angular/src/schematics/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,15 @@ function addLoadChildren(options: NormalizedSchema): Rule {
true
);

const loadChildren = `@${npmScope}/${options.projectDirectory}#${
options.moduleName
}`;

insert(host, options.parentModule, [
...addRoute(
options.parentModule,
sourceFile,
`{path: '${toFileName(
options.fileName
)}', loadChildren: '${loadChildren}'}`
)}', loadChildren: () => import('@${npmScope}/${
options.projectDirectory
}').then(module => module.${options.moduleName})}`
)
]);

Expand Down
11 changes: 11 additions & 0 deletions packages/workspace/src/command-line/deps-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ export class DepsCalculator {
return; // stop traversing downwards
}

if (
ts.isCallExpression(node) &&
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
node.arguments.length === 1 &&
ts.isStringLiteral(node.arguments[0])
) {
const imp = this.getStringLiteralValue(node.arguments[0]);
this.addDepIfNeeded(imp, filePath, DependencyType.loadChildren);
return;
}

if (node.kind === ts.SyntaxKind.PropertyAssignment) {
const name = this.getPropertyAssignmentName(
(node as ts.PropertyAssignment).name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"module": "es2015",
"module": "esnext",
"typeRoots": ["node_modules/@types"],
"lib": ["es2017", "dom"],
"skipLibCheck": true,
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace/src/utils/strip-source-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
import {
Component
} from "react"
import "./app.scss"`;
import "./app.scss"
import('./module.ts')`;

expect(stripSourceCode(scanner, input)).toEqual(expected);
});
Expand Down
7 changes: 4 additions & 3 deletions packages/workspace/src/utils/strip-source-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export function stripSourceCode(scanner: Scanner, contents: string): string {
) {
token = scanner.scan();
}
if (token !== SyntaxKind.OpenParenToken) {
start = potentialStart;
}
start = potentialStart;
break;
}

Expand All @@ -46,6 +44,9 @@ export function stripSourceCode(scanner: Scanner, contents: string): string {
case SyntaxKind.StringLiteral: {
if (start !== null) {
token = scanner.scan();
if (token === SyntaxKind.CloseParenToken) {
token = scanner.scan();
}
const end = scanner.getStartPos();
statements.push(contents.substring(start, end));
start = null;
Expand Down

0 comments on commit bed2ae7

Please sign in to comment.