Skip to content

Commit 08547ab

Browse files
waldekmastykarzVesaJuvonen
authored andcommitted
Updated external libraries guidance to GA (SharePoint#466)
1 parent 2c30b3b commit 08547ab

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

docs/spfx/web-parts/guidance/use-existing-javascript-libraries.md

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Use existing JavaScript libraries in SharePoint Framework client-side web parts
22

3-
> Note. This article has not yet been verified with SPFx GA version, so you might have challenges on making this work as such with the latest release.
4-
53
When building client-side web parts on the SharePoint Framework you can benefit from using existing JavaScript libraries to build powerful solutions. There are however some considerations that you should take into account to ensure that your web parts won't negatively impact the performance of SharePoint pages that they are being used on.
64

75
## Reference existing libraries as packages
@@ -12,21 +10,22 @@ The most common way of referencing existing JavaScript libraries in SharePoint F
1210
npm install angular --save
1311
```
1412

15-
Next, to use Angular with TypeScript, you would install typings using **tsd**:
13+
Next, to use Angular with TypeScript, you would install typings using **npm**:
1614

1715
```sh
18-
tsd install angular --save
16+
npm install @types/angular --save-dev
1917
```
2018

2119
Finally, you would reference Angular in your web part using the `import` statement:
2220

2321
```ts
22+
import { Version } from '@microsoft/sp-core-library';
2423
import {
2524
BaseClientSideWebPart,
26-
IPropertyPaneSettings,
27-
IWebPartContext,
25+
IPropertyPaneConfiguration,
2826
PropertyPaneTextField
29-
} from '@microsoft/sp-client-preview';
27+
} from '@microsoft/sp-webpart-base';
28+
import { escape } from '@microsoft/sp-lodash-subset';
3029

3130
import styles from './HelloWorld.module.scss';
3231
import * as strings from 'helloWorldStrings';
@@ -35,11 +34,6 @@ import { IHelloWorldWebPartProps } from './IHelloWorldWebPartProps';
3534
import * as angular from 'angular';
3635

3736
export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorldWebPartProps> {
38-
39-
public constructor(context: IWebPartContext) {
40-
super(context);
41-
}
42-
4337
public render(): void {
4438
this.domElement.innerHTML = `
4539
<div class="${styles.helloWorld}">
@@ -81,10 +75,10 @@ A better approach, to leverage existing libraries in SharePoint Framework client
8175

8276
Referencing existing JavaScript libraries in the SharePoint Framework is easy and doesn't require any specific changes in the code. Because the library is loaded on runtime from the specified URL, it doesn't need to be installed as a package in the project.
8377

84-
Using Angular as an example, in order to reference it as an external resource in your client-side web part, you start by installing its TypeScript typings using **tsd**:
78+
Using Angular as an example, in order to reference it as an external resource in your client-side web part, you start by installing its TypeScript typings using **npm**:
8579

8680
```sh
87-
tsd install angular --save
81+
npm install @types/angular --save-dev
8882
```
8983

9084
Next, in the **config/config.json** file, to the **externals** property you add the following entry:
@@ -108,13 +102,6 @@ The complete **config/config.json** file would then look similar to:
108102
}
109103
],
110104
"externals": {
111-
"@microsoft/sp-client-base": "node_modules/@microsoft/sp-client-base/dist/sp-client-base.js",
112-
"@microsoft/sp-client-preview": "node_modules/@microsoft/sp-client-preview/dist/sp-client-preview.js",
113-
"@microsoft/sp-lodash-subset": "node_modules/@microsoft/sp-lodash-subset/dist/sp-lodash-subset.js",
114-
"office-ui-fabric-react": "node_modules/office-ui-fabric-react/dist/office-ui-fabric-react.js",
115-
"react": "node_modules/react/dist/react.min.js",
116-
"react-dom": "node_modules/react-dom/dist/react-dom.min.js",
117-
"react-dom/server": "node_modules/react-dom/dist/react-dom-server.min.js",
118105
"angular": {
119106
"path": "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js",
120107
"globalName": "angular"
@@ -129,12 +116,13 @@ The complete **config/config.json** file would then look similar to:
129116
Finally, you reference Angular in your web part, just like you did previously:
130117

131118
```ts
119+
import { Version } from '@microsoft/sp-core-library';
132120
import {
133121
BaseClientSideWebPart,
134-
IPropertyPaneSettings,
135-
IWebPartContext,
122+
IPropertyPaneConfiguration,
136123
PropertyPaneTextField
137-
} from '@microsoft/sp-client-preview';
124+
} from '@microsoft/sp-webpart-base';
125+
import { escape } from '@microsoft/sp-lodash-subset';
138126

139127
import styles from './HelloWorld.module.scss';
140128
import * as strings from 'helloWorldStrings';
@@ -143,11 +131,6 @@ import { IHelloWorldWebPartProps } from './IHelloWorldWebPartProps';
143131
import * as angular from 'angular';
144132

145133
export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorldWebPartProps> {
146-
147-
public constructor(context: IWebPartContext) {
148-
super(context);
149-
}
150-
151134
public render(): void {
152135
this.domElement.innerHTML = `
153136
<div class="${styles.helloWorld}">
@@ -191,13 +174,13 @@ Some organizations don't allow access to public CDNs from the corporate network.
191174

192175
## JavaScript libraries formats
193176

194-
Different JavaScript libraries are built and packaged in a different way. Some are packaged as modules while others are plain scripts that run in the global scope (these scripts are often referred to as non-AMD scripts). When loading JavaScript libraries from a URL, how you register an external script in a SharePoint Framework project depends on the format of the script. There are multiple module formats, such as AMD, UMD or CommonJS but the only thing that you have to know is if the particular script is a module or not.
177+
Different JavaScript libraries are built and packaged in a different way. Some are packaged as modules while others are plain scripts that run in the global scope (these scripts are often referred to as non-module scripts). When loading JavaScript libraries from a URL, how you register an external script in a SharePoint Framework project depends on the format of the script. There are multiple module formats, such as AMD, UMD or CommonJS but the only thing that you have to know is if the particular script is a module or not.
195178

196179
When registering scripts packaged as modules the only thing that you have to specify, is the URL where the particular script should be downloaded from. Dependencies to other scripts are handled already inside the script's module construct.
197180

198-
Non-AMD scripts on the other hand require at minimum the URL from where the script should be downloaded and the name of the variable with which the script will be registered in the global scope. If the non-AMD script depends on other scripts, they can be listed as dependencies. To illustrate this, let's have a look at a few examples.
181+
Non-module scripts on the other hand require at minimum the URL from where the script should be downloaded and the name of the variable with which the script will be registered in the global scope. If the non-module script depends on other scripts, they can be listed as dependencies. To illustrate this, let's have a look at a few examples.
199182

200-
Angular v1.x is a non-AMD script. You register it as an external resource in a SharePoint Framework project by specifying its URL and the name of the global variable it should register with:
183+
Angular v1.x is a non-module script. You register it as an external resource in a SharePoint Framework project by specifying its URL and the name of the global variable it should register with:
201184

202185
```json
203186
"angular": {
@@ -218,9 +201,9 @@ jQuery is an AMD script. To register it you could simply use:
218201

219202
```json
220203
"jquery": "https://code.jquery.com/jquery-2.2.4.js"
221-
```
204+
```
222205

223-
Imagine now, that you wanted to use jQuery with a jQuery plugin that itself is distributed as a non-AMD script. If you registered both scripts using:
206+
Imagine now, that you wanted to use jQuery with a jQuery plugin that itself is distributed as a non-module script. If you registered both scripts using:
224207

225208
```json
226209
"jquery": "https://code.jquery.com/jquery-2.2.4.js",
@@ -232,9 +215,9 @@ Imagine now, that you wanted to use jQuery with a jQuery plugin that itself is d
232215

233216
loading the web part would very likely result in an error: there is a chance that both scripts would be loaded in parallel and the plugin wouldn't be able to register itself with jQuery.
234217

235-
![Error when trying to load a web part using a non-AMD jQuery plugin](../../../../images/external-scripts-non-amd-no-deps-error.png)
218+
![Error when trying to load a web part using a non-module jQuery plugin](../../../../images/external-scripts-non-amd-no-deps-error.png)
236219

237-
As mentioned before, SharePoint Framework allows you to specify dependencies for non-AMD plugins. These dependencies are specified using the **globalDependencies** property:
220+
As mentioned before, SharePoint Framework allows you to specify dependencies for non-module plugins. These dependencies are specified using the **globalDependencies** property:
238221

239222
```json
240223
"jquery": "https://code.jquery.com/jquery-2.2.4.js",
@@ -247,11 +230,11 @@ As mentioned before, SharePoint Framework allows you to specify dependencies for
247230

248231
Each dependency specified in the **globalDependencies** property must point to another dependency in the **externals** section of the **config/config.json** file.
249232

250-
If you would try to build the project now, you would get another error, this time stating that you can't specify a dependency to a non-AMD script.
233+
If you would try to build the project now, you would get another error, this time stating that you can't specify a dependency to a non-module script.
251234

252-
![Error while trying to build a SharePoint Framework with a non-AMD script specifying a dependency to a script that is a module](../../../../images/external-scripts-non-amd-deps-error.png)
235+
![Error while trying to build a SharePoint Framework with a non-module script specifying a dependency to a script that is a module](../../../../images/external-scripts-non-amd-deps-error.png)
253236

254-
To solve this problem, all you need to do is to register jQuery as a non-AMD module:
237+
To solve this problem, all you need to do is to register jQuery as a non-module script:
255238

256239
```json
257240
"jquery": {
@@ -269,11 +252,11 @@ This way you specify that the **simpleWeather** script should be loaded after jQ
269252

270253
> Note how the entry for registering jQuery uses **jquery** for the external resource name but **jQuery** as the global variable name. The name of the external resource is the name that you use in the `import` statements in your code. This is also the name that must match TypeScript typings. The global variable name, specified using the **globalName** property, is the name known to other scripts like plugins built on top of the library. While for some libraries these names might be the same, it's not required and you should carefully check that you are using correct names to avoid any problems.
271254
272-
## Non-AMD scripts considerations
255+
## Non-module scripts considerations
273256

274-
Many JavaScript libraries and scripts developed in the past are distributed as non-AMD scripts. While the SharePoint Framework supports loading non-AMD scripts, you should strive to avoid using them whenever possible.
257+
Many JavaScript libraries and scripts developed in the past are distributed as non-module scripts. While the SharePoint Framework supports loading non-module scripts, you should strive to avoid using them whenever possible.
275258

276-
Non-AMD scripts are registered in the global scope of the page: script loaded by one web part is available to all other web parts on the page. If you had two web parts using different versions of jQuery, both loaded as non-AMD scripts, the web part that loaded the last would overwrite all previously registered versions of jQuery. As you can imagine, this could lead to unpredictable results and very hard to debug issues that would occur only in certain scenarios - only with other web parts using a different version of jQuery on the page and only when they load in particular order. The module architecture solves this problem by isolating scripts and preventing them from affecting each other.
259+
Non-module scripts are registered in the global scope of the page: script loaded by one web part is available to all other web parts on the page. If you had two web parts using different versions of jQuery, both loaded as non-module scripts, the web part that loaded the last would overwrite all previously registered versions of jQuery. As you can imagine, this could lead to unpredictable results and very hard to debug issues that would occur only in certain scenarios - only with other web parts using a different version of jQuery on the page and only when they load in particular order. The module architecture solves this problem by isolating scripts and preventing them from affecting each other.
277260

278261
## When you should consider bundling
279262

0 commit comments

Comments
 (0)