Skip to content

Support import() syntax for loadChildren in ViewEngine #13948

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 6 commits into from
Mar 29, 2019

Conversation

filipesilva
Copy link
Contributor

@filipesilva filipesilva commented Mar 19, 2019

This feature ONLY matches the format below:

loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME)

It will not match nor alter variations, for instance:

The only parts that can change are the ones in caps: IMPORT_STRING and EXPORT_NAME.

@filipesilva
Copy link
Contributor Author

Blocked on angular/angular#29392

@filipesilva filipesilva changed the title Import syntax ve Support import() syntax for loadChildren in ViewEngine Mar 19, 2019
@filipesilva filipesilva force-pushed the import-syntax-ve branch 2 times, most recently from 2a37c0c to 451d2cb Compare March 25, 2019 15:44
@filipesilva filipesilva mentioned this pull request Mar 25, 2019
14 tasks
@filipesilva filipesilva added the target: major This PR is targeted for the next major release label Mar 26, 2019
@filipesilva filipesilva marked this pull request as ready for review March 26, 2019 13:08
@@ -103,6 +104,8 @@ export interface AngularCompilerPluginOptions {
nameLazyFiles?: boolean;
logger?: logging.Logger;
directTemplateLoading?: boolean;
discoverLazyRoutes?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you kindly explain the reasoning behind this option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the loadChildren string syntax, @ngtools/webpack must query @angular/compiler-cli via a private API to know which lazy routes exist. This increases build and rebuild time.

When using Ivy, the string syntax is not supported at all. Thus we shouldn't attempt that.

This commit also exists in #13700 for slightly different reasons. There, it is meant to disable this sort of processing for compilations that do not need it.

Copy link
Collaborator

@alan-agius4 alan-agius4 Mar 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification, it makes more sense now :) As to disable lazy route discovery under ivy we can use enableIvy, but seeing that other PR. I can now see the use case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn (or probably error) in the transformer (or somewhere else) if the string form is found when this option is off?

Users may not know why their applications are not working.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not because that means we need to always check, which incurs a performance penalty on every AOT build.

The "right way" of doing that probably a Ivy migration when it's stable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try to generally answer questions like this in the form of a comment in the code - no one later will find this thread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return replaceImport(node, context, emitWarning);
}

return ts.visitEachChild(node, visitNode, context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ngc always creates the variable declaration at the root level then only the source file's children should need to be analyzed. Walking the entire tree can be avoided. The regex test above can be avoided as well since top level nodes are typically fairly minimal and testing a large block of text will be much less efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a better approach, yes. I modified the code to only recurse into VariableStatement to find VariableDeclaration within, since the structure is VariableStatement -> VariableDeclarationList -> VariableDeclaration. PTAL

@@ -103,6 +104,8 @@ export interface AngularCompilerPluginOptions {
nameLazyFiles?: boolean;
logger?: logging.Logger;
directTemplateLoading?: boolean;
discoverLazyRoutes?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn (or probably error) in the transformer (or somewhere else) if the string form is found when this option is off?

Users may not know why their applications are not working.

): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => {
return (sourceFile: ts.SourceFile) => {
const warning = `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a warning or an error, since it will cause a runtime error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't necessarily cause a runtime error. VE can still use both types of imports.

import { expectToFail } from '../../utils/utils';

export default async function () {
const projectName = 'ivy-lazy-loading';
const argv = getGlobalVariable('argv');
const ivyProject = argv['ivy'];
Copy link
Collaborator

@alan-agius4 alan-agius4 Mar 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are these run during the e2e-cli tests, Isn't the ivy arg used only in the cli snapshots?

Hence we would only be testing a subset of scenarios during the e2e's in PRs, to be more precise the VE tests.

We only have a limited tests for Ivy during E2E, and removing some might be risky. Unless we we have some e2e-cli-ivy job for PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Ivy tests are only run for snapshots now. I agree with you that we would love coverage. I think moving the ivy job to not use the snapshots, and to always run, is a good approach. We're starting to have more cde that interacts with Ivy so we should have confidence in PRs for it.

Copy link
Collaborator

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the above questions.

LGTM

@filipesilva filipesilva force-pushed the import-syntax-ve branch 3 times, most recently from a4ac54e to 82f25f9 Compare March 28, 2019 17:48
@@ -106,24 +106,17 @@ jobs:
path: /tmp/dist
destination: cli/new-production

e2e-cli-ng-snapshots:
e2e-cli-ivy:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why switch the order of these? makes the delta harder to review

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep similar e2e jobs (e2e-cli and e2e-cli-ivy) together.

The old e2e-cli-ng-snapshots was after e2e-cli-ivy-snapshots because they shared the snapshot flag and only ran on some builds. Now e2e-cli-ivy-snapshots is instead e2e-cli-ivy and should instead be next to e2e-cli.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@@ -103,6 +104,8 @@ export interface AngularCompilerPluginOptions {
nameLazyFiles?: boolean;
logger?: logging.Logger;
directTemplateLoading?: boolean;
discoverLazyRoutes?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try to generally answer questions like this in the form of a comment in the code - no one later will find this thread


loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME)

Please note that only IMPORT_STRING and EXPORT_NAME can be replaced in this format.`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to the guide again, if ppl get confused we'll be able to help them there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

…iewEngine

This feature ONLY matches the format below:
```
loadChildren: () => import('IMPORT_STRING').then(m => m.EXPORT_NAME)
```

It will not match nor alter variations, for instance:
- not using arrow functions
- not using `m` as the module argument
- using `await` instead of `then`
- using a default export (angular/angular#11402)

The only parts that can change are the ones in caps: IMPORT_STRING and EXPORT_NAME.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants