First, lets start off with the improvements we made to installation. In the past the version 1 library had peer dependencies, which may be logically correct, but leads to confusion and a lot of extra work to install each of the required libraries. For version 2 we decided to change to a dependency model and so going forward you only need run:
npm install @pnp/sp --save
Let’s be honest, we all tend to code first and optimize second. But when it came to optimize for version 1 you didn’t have much choice as the monolithic libraries offered no way to minimize them short of forking the repository and managing your own build. In version 2 we’ve implemented modern ES Modules that supports selective imports. This allows your bundling code to only import the bits of the PnPjs library that you’re using therefore reducing your bundle size significantly.
Of course, for those of you doing Node.js development (verses SPFx development) this becomes a bit of a problem when using older versions of Node.js that don’t support ES Modules, and so we’ve addressed that to with a set of modules built in CommonJS. You can read more in our updated article on working in Node.js.
Speaking of presets, we’ve created a set of presets for easier transition from version 1 or getting started quickly, if bundle size isn’t of critical importance for your project. And be sure to checkout our sample on creating in-project presets or create a custom bundle to support your projects.
Another enhancement from version 1 is the use of invokable objects. Previously you would need to finalize any command to PnPjs with .get() to execute the query. Going forward you will just be able to invoke the object itself: sp.web(). Not only does this make your code more readable but it also allows for default actions that are not get but instead might be post. For backward compatibility all .get() will continue to work.
Factory Functions & Interfaces
We’ve been hard at work refactoring all the exports. We’re no longer exporting the objects themselves but rather only exposing the factory function and interfaces. This allow us to decouple what developers use from our internal implementation. So, going forward instead of using the “new” keyword to create an instance of the object you will simply call the object and its type will be based on the interface not the object.
One of the more exciting enhancements to the version 2 library is the introduction of extensions. The idea is that you would be able to override or add functionality to an object without altering the underlying classes. This can be extremely useful for debugging and testing but also adding some custom functionality to the library based on your personal requirements. Better yet, extensions work with ANY invokable object. Which means, you can customize the behavior of just about anything in the library.
Because of the ability to do selective imports, a new benefit will be the ability for you to create your own custom PnPjs bundles. These bundles can have specific pieces of the PnPjs library you want to make available to your solution, or development team, but also allows you to bundle in any extension methods you created thereby building the perfect stand-alone library.
Well that’s a whole lot of great new functionality and we hope everyone is as excited as we are to try out the new library. We have a series of videos coming to help you get started, so be sure to check out our YouTube channel! Please take the time to review our transition guide as it’s really going to help you make the move from version 1 to version 2 a lot smoother. And if you have issues please submit an issue to our issues list so that someone in the community can assist you!
Also, if you feel like you’d like to contribute we’d be more than happy to have you, please see our contribution guide. You can also check out the “Sharing is Caring” initiative, where you can benefit from live hands-on guidance sessions on contributing to a variety of the Patterns and Practices projects. Additionally you can watch the video, “No Code Microsoft SharePoint PnP (Patterns and Practices) Contributions”, which provides an introduction to PnP contribution guidance principles.
“Sharing is caring!”
SharePoint Team, Microsoft – 29th of January 2020