I have been working with AngularJS to develop SharePoint Apps. Now I wanted to develop new SharePoint Apps with Angular 4 with all the awesomeness of new open source tools and build chains like Typescript, WebPack, GulpJS etc.
So I started building the app as guide lines provided by Angular Docs. The scenario is for single angular application on a page so CommonsChunkPlugin plays very well in order to eliminate common code from multiple bundles. But it develops module resolution structure only specific to the files in that application. So, you cannot use the vendor files for another application.
In context of SharePoint I would like to have single vendor file and all the SharePoint Apps/WebParts should use the same vendor otherwise it will end up multiple vendor files with duplicate code.
I came across an interesting article Optimizing Webpack build times and improving caching with DLL bundles. It helped me solve the issue in SharePoint world, so now I can have single vendor file. You may read more about WebPack DllPlugin.
The main idea is to structure the application in two parts vendor bundle build once and deploy to SharePoint site. Application/WebPart bundle to refer the vendor bundle. There can be many Applications/WebParts on the same page.
Vendor Bundle
In Vendor Bundle project install following npm packages:
- @angular/animations
- @angular/common
- @angular/compiler
- @angular/core
- @angular/forms
- @angular/http
- @angular/platform-browser
- @angular/platform-browser-dynamic
- @angular/platform-server
- @angular/router
- core-js
- rxjs
- zone.js
- awesome-typescript-loader
- source-map-loader
- typescript
- webpack
Create a WebPack configuration, tsconfig and vendor files in the root of the project.
Edit package.json file and paste following:
"scripts": {
"build": "webpack –config ./webpack.vendor.config.js –progress –profile – bail",
...
}
...
Open command line and execute "npm run build". It will generate vendor.js contains all the base code from Angular and RxJS, also vendor-manifest.json file contains all the reference about how to resolve the different modules from vendor.js. We will be using this file in the SharePoint Application/WebPart. You must publish these file as a npm package to your private/public registry to consume it.
Application Bundle
In order to create application you have to follow the guidelines as mentioned in the Angular Docs, also we need to do some extra work. Install "vendor" package which we have built above and WebPack configuration file. Change the path of directories based on your application structure. As you may notice I have used DllReferencePlugin in the consumer/application project which provides information to WebPack from where to resolve the modules.
This approach will not only eliminate duplicate code, but also reduce the build time as well because main chunk of code is coming from Angular which we have already bundled.
Originally posted on Medium Blog Posting
Top comments (0)