Modern SPFx on SharePoint 2019
February 12, 2020
SharePoint Framework (SPFx) allows developers to create modern applications (web parts) that can seamlessly integrate into a SharePoint environment to provide business solutions for users. Developers can use modern frameworks like React with Typescript out of the box.
The latest version of SPFx (1.10 at the time of writing) supports React 16.7 and Typescript 3.3. This is great, but we do miss out on some of the latest paradigms such as the hooks and context APIs in React.
While SharePoint Online (SPO) supports the latest versions of SPFx, officially, SharePoint 2019 only supports up to version 1.4.1. This is quite an issue as this version only supports React 15.6 and Typescript 2.4, meaning a lot of other packages will require old versions.
Another limiting factor is the Office UI Fabric React library that ships is stuck on 5.21 whereas the latest (7.9) has a lot of bug fixes that we miss out on.
The main problem here is that because SPFx is locked to a specific version of React, we cannot use a later version of typescript as React doesn't support it. And because we don't have the latest versions of React OR Typescript, we are locked to a lower version of Fabric as well.
Using any package
Luckily we get around these limitations by modifying the gulp script and specifying later versions of those packages in our installed modules.
Installing the above will grant us access to the packages in our dev environment, however, SPFx has a built-in webpack operation that strips out included versions of React by default, and locks it to the version used in the SharePoint environment. This will cause any code that leverages newer features not available in these versions to blow up.
Luckily, we can edit the gulp script to tell web pack to ignore that rule and bundle in our specified versions:
Now when the project is built and packaged it will include our version of React. This means that we can safely use the latest Typescript, which in turn means that we can use the latest version of Fabric as well.
Fabric
The only issue we have found so far is that there are some slight naming conflicts with the default theme in the version of fabric that is embedded in SharePoint 2019. The workaround we have in place for this is to force default values in the components we use our project: