{"id":2032,"date":"2018-11-07T14:35:47","date_gmt":"2018-11-07T14:35:47","guid":{"rendered":"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/?p=2032"},"modified":"2018-11-07T14:35:47","modified_gmt":"2018-11-07T14:35:47","slug":"mvp-article-pnp-spfx-yeoman-generator-v1-4-0-angular-elements-support-is-here","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/mvp-article-pnp-spfx-yeoman-generator-v1-4-0-angular-elements-support-is-here\/","title":{"rendered":"MVP Article &#8211; PnP SPFx Yeoman generator v1.4.0 &#8211; Angular Elements support is here!"},"content":{"rendered":"<p>It&#8217;s been a while since <a href=\"https:\/\/twitter.com\/robwormald\">Rob Wormald<\/a> from Google introduced the <a href=\"https:\/\/www.sharepointeurope.com\/full-session-video-sharepoint-angular-elements\/\">first SharePoint Framework client-side web part using Angular Elements<\/a>\u00a0and since Angular Elements was included in the <a href=\"https:\/\/angular.io\/guide\/elements\">Angular 6 release<\/a> earlier this year, there was a clear demand for enabling it as an easy option also for SharePoint Framework developers.<\/p>\n<p>Last week a <a href=\"https:\/\/developer.microsoft.com\/en-us\/sharepoint\/blogs\/announcing-angular-elements-support-in-pnp-spfx-generator\/\">new version 1.4.0 of the PnP SPFx Yeoman generator<\/a> was released with a support of scaffolding SharePoint Framework solutions using Angular elements. Using Angular in SharePoint Framework has some specifics that differs it from the other generators like Handlebars or VueJS, so implementation differs slightly on those options.<\/p>\n<p>There were some considerations for the implementation of this new member in the SharePoint Framework family:<\/p>\n<ol>\n<li>\u00a0Angular developer love their CLI<\/li>\n<li>Angular Elements directly integrate with SPFx can cause larger package sizes<\/li>\n<li>An easy way to join to join to project together<\/li>\n<\/ol>\n<h2>Angular developer love their CLI<\/h2>\n<p>Nearly all framework these days come with their own CLI (command line interface) these days. Those CLIs helps people to set up and configure projects to their needs. Instead of knowing to modify this file there and add that reference there that CLIs offer much help to automate development tasks.\u00a0Angular has its own CLI too, and Angular developers love it.<\/p>\n<p>Integrate Angular Elements directly to a SharePoint Framework would have removed this possibility.<\/p>\n<p>&gt; <em>So how to make sure you can use the Angular CLI?<\/em><\/p>\n<p>Instead of creating just one monolithic project, PnP SPFx Yeoman generator creates two separate projects: one for Angular Elements and second for the SharePoint Framework solution, which is then linked to the Angular project.<\/p>\n<h2>Direct integration to SPFx vs separate project<\/h2>\n<p>Thanks to the base research by <a href=\"https:\/\/twitter.com\/andrewconnell\">Andrew Connell<\/a> (Voitanos) that document his findings in his blog post series on using <a href=\"http:\/\/www.andrewconnell.com\/blog\/using-angular-elements-in-sharepoint-framework-projects\">Angular Elements in SharePoint framework<\/a>. He compared the approach of all in a single project versus the two project approach.<\/p>\n<p><a href=\"http:\/\/www.andrewconnell.com\/blog\/howto-angular-elements-in-sharepoint-framework-projects-one-big-project\">One big project<\/a> containing everything removed as mentioned before the possibility to use the Angular CLI and a single web part would require the user to download 2.5 Megabyte.<\/p>\n<p>That&#8217;s beyond a bundle size acceptable especially for a mobile experience and at least doubtable for the desktop experience.<\/p>\n<p>The <a href=\"http:\/\/www.andrewconnell.com\/blog\/howto-angular-elements-in-sharepoint-framework-projects-two-projects\">two project approach<\/a>, on the other hand, led to a more promising result of 330kb for a web part. Most of this file size comes from the Angular runtime, and the web part code is only a small portion inside this bundle.<\/p>\n<p>This fact set the decision that this second approach is one we followed making a Yeoman generator for Angular Elements possible.\u00a0So the two projects approach is great in case of performance and asset size but what is the downside on this?<\/p>\n<p>The SharePoint Framework project needs to be aware of the Angular Elements project, or at least need to know that there is some external dependency.<\/p>\n<p>Andrew Connell managed this by writing a custom bundle task that merges all required scripts and assets to a single file and copies the resulting bundle directly to the SPFX project.<\/p>\n<p>In our setup, we followed a different approach.<\/p>\n<h2>An easy way to join to join to project together<\/h2>\n<p>Copy the resulting bundle of the Angular Project is a legitimate approach but every Angular Project is an NPM package too. The project root folder contains a &#8216;package.json&#8217; that can serve as a dependency for any other NPM project.<\/p>\n<p>When you execute &#8216;npm install&#8217; for installing, for example, the PnP reusable components you always install the package from <a href=\"https:\/\/www.npmjs.com\">npmjs.com<\/a>.\u00a0There are more ways to you can install packages, this can be any Git repository or even from a local file path.<\/p>\n<pre style=\"padding-left: 30px\">npm install &lt;relative path to local package&gt;<\/pre>\n<p>To install an Angular project named &#8216;hellong&#8217; to a SharePoint Framework project the following command adds this reference:<\/p>\n<pre style=\"padding-left: 30px\">npm install ..\/hellong<\/pre>\n<p>By doing so, the relative file path then can be found in the package.json of the SPFx project.<\/p>\n<pre style=\"padding-left: 30px\">{\n  \"@microsoft\/sp-webpart-base\": \"1.6.0\",\n  \"@pnp\/spfx-property-controls\": \"1.11.0\",\n  \"@types\/es6-promise\": \"0.0.33\",\n  \"@types\/webpack-env\": \"1.13.1\",\n  \"hellong\": \"file:..\/hellong\"\n  },\n  \"devDependencies\": {\n  \"@microsoft\/sp-build-web\": \"1.6.0\",\n  ...\n}<\/pre>\n<p>This reference exists in the `node_modules` folder but it is just a shortcut to the actual path. Similar to how the package manager &#8216;pnpm&#8217; handles its dependencies.\nThis way the &#8220;hellong&#8221; the SharePoint framework project treat the dependency like any other dependency on web parts and extensions.<\/p>\n<pre style=\"padding-left: 30px\">import * as strings from 'HelloWorldWebPartStrings';\n\n\/\/ reference to dist folder of ng bundle\nimport 'hellong\/dist\/hellong\/bundle.js';\n\nexport interface IHelloWorldWebPartProps {\n  description: string;\n}<\/pre>\n<p>Every new build and bundle of the Angular Project automatically references the output correctly.<\/p>\n<h2>Welcome to Angular Elements in SPFx<\/h2>\n<p>A warm welcome to Angular Elements for SPFx, a generator <a href=\"https:\/\/twitter.com\/pawelhawrylak\">Pawe\u0142 Hawrylak<\/a> built. Thank you very much for your contribution to this and the support in the forum. Also big thanks to <a href=\"https:\/\/twitter.com\/andrewconnell\">Andrew Connell<\/a> that laid the foundation for this generator to happen.<\/p>\n<p>Currently, this new generator is marked as experimental because we believe it is a great starting point for now, but we depend on your valuable feedback.\u00a0If you have suggestions on how to improve this, please let us know on the <a href=\"https:\/\/github.com\/pnp\/generator-spfx\/issues\/new\">issue list<\/a>.<\/p>\n<p>If you love what we are doing, please give us a <a href=\"https:\/\/github.com\/pnp\/generator-spfx\">star on GitHub<\/a> to share your support.<\/p>\n<p>If you like to help, we are always open to new pull requests and <a href=\"https:\/\/pnp.github.io\/generator-spfx\/\">check out the documentation<\/a> too.<\/p>\n<p>Also, check out the <a href=\"https:\/\/developer.microsoft.com\/en-us\/sharepoint\/blogs\/announcing-angular-elements-support-in-pnp-spfx-generator\/\">launch blog post<\/a>.<\/p>\n<p>Happy development with PnP SPFx Yeoman generator!<\/p>\n<h2>About the Author<\/h2>\n<p><img decoding=\"async\" class=\"alignleft wp-image-2033 size-full\" style=\"float: left;margin: 10px\" src=\"https:\/\/officedevblogs.wpengine.com\/wp-content\/uploads\/2018\/11\/stefan-blog.png\" alt=\"Stefan Bauer\" width=\"200\" height=\"200\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/stefan-blog.png 200w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/stefan-blog-150x150.png 150w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/stefan-blog-24x24.png 24w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/stefan-blog-48x48.png 48w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/stefan-blog-96x96.png 96w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><a href=\"https:\/\/twitter.com\/stfbauer\">Stefan Bauer<\/a> is an information architect, Microsoft Office Development MVP and part of the Pattern and Practice core team based in Austria. He is also leading the open-source PnP\/SPFx yeoman generator project that helps developers to extend the features to their requirements.<\/p>\n<p>His work focuses on national and international customers on transforming their daily business challenges into usable solutions.<\/p>\n<p>He shares his experiences and passion for web technologies and design at conferences and SharePoint Saturday as a speaker and through <a href=\"https:\/\/n8d.at\/blog\">his blog &#8211; N8D<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p><i>SharePoint Team, Microsoft &#8211; 7th of November 2018<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MVP Article &#8211; Angular Elements support is was released for the open-source PnP SPFx generator and this blog post explains the design decisions and the background around the released implementation. <\/p>\n","protected":false},"author":69078,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2032","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-365-developer"],"acf":[],"blog_post_summary":"<p>MVP Article &#8211; Angular Elements support is was released for the open-source PnP SPFx generator and this blog post explains the design decisions and the background around the released implementation. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/2032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69078"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=2032"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/2032\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=2032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=2032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=2032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}