Skip to content
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

feat(ls-routes): introduce ls-routes #778

Closed
wants to merge 1 commit into from

Conversation

Toxicable
Copy link

@Toxicable Toxicable commented Aug 14, 2017

  • Please check if the PR fulfills these requirements
- [x] The commit message follows our guidelines
- [ ] Tests for the changes have been added (for bug fixes / features)
- [x] Docs have been added / updated (for bug fixes / features)
  • What modules are related to this pull-request
- [x] ls-routes (new)
  • What kind of change does this PR introduce?
    A tool for traversing a given factory and retrieving the routes.
    Useful when used with Universal pre-rendering when you want to prerender each potential static route

This differs from Alex's one, ref https://github.com/alxhub/universal/blob/pwa-tools/modules/pwa-tools/lib/ls-routes/lib.ts
In that it works off a factory bundle rather than the Module src and is only hunting for routes instead of other metadata

@Toxicable Toxicable force-pushed the routes-ls branch 2 times, most recently from 78e1e63 to 8334355 Compare August 14, 2017 22:21
function resolveLazyChildren(route: Route, injector: Injector): Promise<Route> {
if (typeof route.loadChildren === 'function') {
//not supported
//return route.loadChildren().;
Copy link
Member

Choose a reason for hiding this comment

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

why would this not be supported if it returns a Promise?

Copy link
Author

Choose a reason for hiding this comment

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

The issue I had with this one is that loadChildren is typed as () => Type<any> | NgModuleFactory<any> | Promise<Type<any>> | Observable<Type<any>>
So out of those we could do a instanceof to see if it's a NgModuleFactory
and Coerce the Observable into a Promise
But I'm uncertain how to how to retrieved a NgModuleFactory from a Type

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

That's a Private API since you cant find it here https://github.com/angular/angular/blob/master/tools/public_api_guard/router/router.d.ts
but it does infer a reasonable point

        if (t instanceof NgModuleFactory) {
          return of (t);
        } else {
          return fromPromise(this.compiler.compileModuleAsync(t));
        }

this indicates to me that the returned function will be a factory if it's in AOT or just a normal type if it's JIT, hence the compiler

@Toxicable Toxicable force-pushed the routes-ls branch 2 times, most recently from 1eb506b to 3d046c1 Compare August 15, 2017 06:36
@Toxicable
Copy link
Author

Toxicable commented Aug 15, 2017

I've added Karma config for testing to the project since it was becoming very tedious to test if specific scenarios were working.
I'm not sure if this is wanted to be in the same commit or if the config I've done is any good so feel free to advise me otherwise.
Also you'll see Travis failing because it cant find @nguniversal/module-map-ngfactory-loader that's because the Tool I'm adding depends on it but it's not actually published to NPM yet

@Toxicable Toxicable force-pushed the routes-ls branch 2 times, most recently from c06886b to c58dda3 Compare August 15, 2017 09:22

class FileLoader implements ResourceLoader {
get(url: string): Promise<string> {
return new Promise((resolve) => {
Copy link

Choose a reason for hiding this comment

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

ResourceLoader#get returns a Promise | string union. You can thus return readFileSync directly instead of the resolved promise.

Copy link
Author

Choose a reason for hiding this comment

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

With fs.ReadFile you have to provide callbacks and what not, it's not as easy as just calling .toString(). I did simplify it a little bit more, I don't think using the sync version could get it anymore concise

Copy link

Choose a reason for hiding this comment

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

huh, readFileSync is used in that code. No promise needed.

Copy link
Author

@Toxicable Toxicable Aug 15, 2017

Choose a reason for hiding this comment

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

oh! sorry, I misread what you said, gotcha

@Toxicable Toxicable force-pushed the routes-ls branch 5 times, most recently from 7391eb2 to 2755b92 Compare August 16, 2017 04:05
class MockServerModule{}
return jitCompiler.compileModuleAsync(MockServerModule);
}
function createFactoryAndGetRotues(routeConfig: Route[], moduleMap: {[key: string]: any} = {}) {
Copy link

Choose a reason for hiding this comment

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

typo in createFactoryAndGetRotues

});
}

function cocerceIntoPromise<T>(mightBePromise: Observable<T> | Promise<T> | T): Promise<T> {

Choose a reason for hiding this comment

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

I guess this has to be coerceIntoPromise ?

@Toxicable Toxicable force-pushed the routes-ls branch 5 times, most recently from 2e136e6 to 32406c9 Compare August 16, 2017 21:56
@Toxicable Toxicable changed the title [WIP] feat(ls-routes): introduce ls-routes feat(ls-routes): introduce ls-routes Aug 18, 2017
@Toxicable Toxicable force-pushed the routes-ls branch 6 times, most recently from ea09dbb to 93a6a46 Compare August 29, 2017 00:06
@Toxicable
Copy link
Author

Toxicable commented Aug 29, 2017

now that @nguniversal/module-map-ngfactory-loader is published this is no longer blocked by travis

@Toxicable Toxicable force-pushed the routes-ls branch 2 times, most recently from 72622e3 to 62c9292 Compare September 22, 2017 10:23
@PatrickJS
Copy link
Member

PatrickJS commented Sep 26, 2017

got the tests to pass. If Alex already wrote this then I think this would be a great example of how people can build stuff with Universal. @Toxicable maybe you can turn this into a blog post

Tipe CMS

@Toxicable
Copy link
Author

I've updated it but I don't know how to configure the build system.
@CaerusKaru You're going to have to fix that

@CaerusKaru
Copy link
Member

@Toxicable Do you want me to push to this branch or just walk you through it?

@Toxicable
Copy link
Author

@CaerusKaru Feel free to push to this branch

@Toxicable
Copy link
Author

closed in favour of #1002

@Toxicable Toxicable closed this May 20, 2018
@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 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
state: blocked state: WIP target: minor target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants