Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Update Angular2Spa template to Angular 2.0/TypeScript 2.0, based on @MarkPieszak's PR #321

Merged
merged 16 commits into from
Sep 19, 2016

Conversation

SteveSandersonMS
Copy link
Member

Following on from @MarkPieszak's PR #319, I've simplified the template further as far as I think makes sense at the moment.

Putting this out as a PR for feedback before merging and publishing.

@hheexx
Copy link

hheexx commented Sep 19, 2016

why is @types/node not added to devDependencies with other @types ?

@SteveSandersonMS
Copy link
Member Author

@MarkPieszak, do you have any feedback on the simplifications/changes I've made here? Your PR was extremely helpful. On top of that, the main things I wanted to change were:

  • Making HMR work
  • Removing files/folders and NPM dependencies that we can avoid the need for
  • Adjusting the filename/folder conventions to more closely match those emitted by angular-cli
  • Simplifying boot code (just because few developers are going to understand this at first glance)
  • Simplifying webpack config (eliminating dev/prod override files). I have plans to simplify this further, but will do that when I get time to update all the templates, not just the Angular one.

I also attempted to get the Angular 2 compiler (ngc), or at least Webpack 2 tree shaking, to work with this, but completely failed. It doesn't seem like ngc and arbitrary Webpack configs work together right now, so that will have to wait, unless someone else has a way to make it work! There are a bunch of experimental webpack-ngc-loader type things on GitHub right now, but it's not clear that any of them really work or will be supported in the long run.

@hheexx - check the later commits. It's in dependencies because it's needed at runtime if you're doing server-side prerendering.

});

return requestZone.run<Promise<string>>(() => platform.serializeModule(AppModule))
Copy link
Contributor

Choose a reason for hiding this comment

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

Much cleaner I like it 👍

HomeComponent
],
imports: [
UniversalModule,
Copy link
Contributor

@MarkPieszak MarkPieszak Sep 19, 2016

Choose a reason for hiding this comment

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

Can we leave a Note that UniversalModule should be the first Import? (Just want to prevent common errors that can creep up from this)

Also, we should point out that BrowserModule HttpModule JsonpModule are all automatically included here.
(The same goes for the Node versions on the main.server.ts side, NodeModule / NodeHttpModule etc)
Things can error out if they include those. We're working on looking into fixing that.

Patrick also thought it'd be nice to keep the main files separate since that allows someone to have specific platform logic injected for either browser/server, but it's up to you. It's definitely cleaner/simpler this way!

Copy link

Choose a reason for hiding this comment

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

Please keep server and client main files separate.
For me they diverged very early in development. And I am pretty sure it will for most users.

Copy link
Member Author

@SteveSandersonMS SteveSandersonMS Sep 19, 2016

Choose a reason for hiding this comment

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

Added the comment about UniversalModule (and BrowserModule/HttpModule/JsonpModule)

main files separate

I'd hope that the boot files serve that purpose well enough (this is where you configure DI, after all), and am reluctant to add more duplicate files. Having one root @NgModule makes things much clearer, especially given how much tedious config goes into that file. We can monitor this and see if people often report issues that are best solved by having separate root modules.

Copy link

Choose a reason for hiding this comment

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

Can we have logic in main file based on platform? Then we don't need 2 files.

if (server) {
declarations: [......]
} else if (client) {
declarations: [.....]
}

?

Copy link
Member Author

Choose a reason for hiding this comment

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

@hheexx Yes, just import { isNode, isBrowser } from 'angular2-universal'; and then do if (isNode) { ... } etc.

Copy link

Choose a reason for hiding this comment

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

That is sweet. I would add it as a comment so people know.

@hheexx
Copy link

hheexx commented Sep 19, 2016

Oh.. I see they are removed in later commit.

const _config = Object.assign({
get cancel() { return cancel; },
cancelHandler() { return Zone.current.get('cancel') }
}, platformConfig);
Copy link
Contributor

Choose a reason for hiding this comment

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

Only thing I'm worried about is the Zone canceling itself if errors/issues occur.
Hence these cancelHandler's being added to the zone.
Let me look into it!

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, please let me know if you think something needs to be added for this.


// * NOTE: Needs to be your first import (!)
UniversalModule,
// * ^ BrowserModule, HttpModule, and JsonpModule are included here
Copy link
Contributor

Choose a reason for hiding this comment

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

These notes would be nice to keep to avoid issues if people think they need to add them in.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@MarkPieszak
Copy link
Contributor

MarkPieszak commented Sep 19, 2016

@SteveSandersonMS
Added a few comments above just now, otherwise I like it!!
Cleaned it up nicely 😀
I was focused on getting it working this weekend didn't have time to gut out unnecessary code, apologies.

One thing recommended by the Angular team is DOMContentLoaded. You don't want angular bootstrapping prior to that, it can cause sporadic/bizarre issues. Can we leave that in there?

document.addEventListener('DOMContentLoaded', () => { 
    platformRef.bootstrapModule(MainModule);
});

Another thing I thought might be beneficial is leaving those comments about UniversalModule. It DepInjects several modules (for you), and if they're included on top of it, it can error things out. (We're looking into it fixing this or at least providing better error messaging for it)


As for AOT Compilation, right now we don't have it fully working, it would be additional code needed in the boot-server, but there's more that needs to be done to get that working on our end.

// eventually it'll be somewhat like this
 return zone.run(() => (_options.precompile ?
    platformRef.serializeModule(ngModule, _data) :
    platformRef.serializeModuleFactory(ngModule, _data) 
  )

Webpack2 does work with Universal, what problems were you getting with it, we can look into it another time.

@SteveSandersonMS
Copy link
Member Author

One thing recommended by the Angular team is DOMContentLoaded

Thanks for the feedback. Done.

As for AOT Compilation, right now we don't have it fully working, it would be additional code needed in the boot-server, but there's more that needs to be done to get that working on our end.

The issue isn't just making it work at runtime in Univeral. Right now I'm not successful in being able to actually do the AOT compilation at all, because it's not generally compatible with arbitrary webpack configs (e.g., using raw-loader and require to reference template .html files).

Webpack2 does work with Universal, what problems were you getting with it

No, that part works fine. I was meaning to say that the end-to-end story with Webpack 2 tree shaking isn't usable right now because of the inability to precompile Angular 2 templates.

@SteveSandersonMS SteveSandersonMS merged commit 41f1f6f into dev Sep 19, 2016
@SteveSandersonMS
Copy link
Member Author

Thanks for the feedback! This is now merged into the default branch. I'll publish actual Yeoman templates tomorrow.

@SteveSandersonMS SteveSandersonMS deleted the angular2-final branch September 19, 2016 16:43
@MarkPieszak
Copy link
Contributor

Excellent! :)
If we need to circle back and add the cancelHandler stuff later we can take care of it, for now at least this should be good enough! I'm still looking into it how important it is, and if it's possible for us to handle it internally with Universal instead perhaps.

Cheers.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants