You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/spfx/web-parts/guidance/use-existing-javascript-libraries.md
+25-42Lines changed: 25 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,5 @@
1
1
# Use existing JavaScript libraries in SharePoint Framework client-side web parts
2
2
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
-
5
3
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.
6
4
7
5
## Reference existing libraries as packages
@@ -12,21 +10,22 @@ The most common way of referencing existing JavaScript libraries in SharePoint F
12
10
npm install angular --save
13
11
```
14
12
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**:
16
14
17
15
```sh
18
-
tsd install angular --save
16
+
npm install @types/angular --save-dev
19
17
```
20
18
21
19
Finally, you would reference Angular in your web part using the `import` statement:
22
20
23
21
```ts
22
+
import { Version } from'@microsoft/sp-core-library';
@@ -81,10 +75,10 @@ A better approach, to leverage existing libraries in SharePoint Framework client
81
75
82
76
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.
83
77
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**:
85
79
86
80
```sh
87
-
tsd install angular --save
81
+
npm install @types/angular --save-dev
88
82
```
89
83
90
84
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:
@@ -191,13 +174,13 @@ Some organizations don't allow access to public CDNs from the corporate network.
191
174
192
175
## JavaScript libraries formats
193
176
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.
195
178
196
179
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.
197
180
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.
199
182
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:
201
184
202
185
```json
203
186
"angular": {
@@ -218,9 +201,9 @@ jQuery is an AMD script. To register it you could simply use:
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:
@@ -232,9 +215,9 @@ Imagine now, that you wanted to use jQuery with a jQuery plugin that itself is d
232
215
233
216
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.
234
217
235
-

218
+

236
219
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:
@@ -247,11 +230,11 @@ As mentioned before, SharePoint Framework allows you to specify dependencies for
247
230
248
231
Each dependency specified in the **globalDependencies** property must point to another dependency in the **externals** section of the **config/config.json** file.
249
232
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.
251
234
252
-

235
+

253
236
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:
255
238
256
239
```json
257
240
"jquery": {
@@ -269,11 +252,11 @@ This way you specify that the **simpleWeather** script should be loaded after jQ
269
252
270
253
> 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.
271
254
272
-
## Non-AMD scripts considerations
255
+
## Non-module scripts considerations
273
256
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.
275
258
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.
0 commit comments