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
* Updated the Integrate your SharePoint client-side web part with the property pane guide
* Updated the guide on adding external libraries
* Updated code snippets in the __RequestDigest guide
* Updated request digest guidance
* Updated notes on solution packaging with the latest types
* Updated guidance on using existing JavaScript libraries
* Updated guidance on referencing 3rd party CSS styles
* Updated guidance on using preconfigured entries
Copy file name to clipboardExpand all lines: docs/spfx/web-parts/basics/add-an-external-library.md
+73-73Lines changed: 73 additions & 73 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,6 @@
2
2
3
3
You might want to include one or more JavaScript libraries in your web part. This article shows you how to bundle an external library and share libraries.
4
4
5
-
6
5
## Bundling a script
7
6
8
7
By default, the web part bundler will automatically include any library that is a dependency of the web part module. This means that the library will be deployed in the same JavaScript bundle file as your web part. This is more useful for smaller libraries that are not used in multiple web parts.
@@ -12,71 +11,70 @@ By default, the web part bundler will automatically include any library that is
12
11
Include the string validating library [validator](https://www.npmjs.com/package/validator) package into a web part.
13
12
14
13
Download the validator package from npm:
15
-
16
-
```
14
+
15
+
```sh
17
16
npm install validator --save
18
17
```
19
-
20
-
>**Note:** Because you're using TypeScript, you need typings for the package you add. This is essential when you are writing code because TypeScript is just a superset of JavaScript. All the TypeScript code is still converted to JavaScript code when you compile. You can search for and find typings by using **tsd** package, for example: `tsd install {package} --save`
21
-
18
+
19
+
>**Note:** Because you're using TypeScript, you need typings for the package you add. This is essential when you are writing code because TypeScript is just a superset of JavaScript. All the TypeScript code is still converted to JavaScript code when you compile. You can search for and find typings by using **npm**, for example: `npm install @types/{package} --save`
20
+
22
21
Create a file in the your web part's folder called `validator.d.ts` and add the following:
23
-
22
+
24
23
>**Note:** Some libraries do not have typings. While the Validator library does have a [community provided typings file](https://www.npmjs.com/package/@types/validator), for this scenario let's assume it does not. In this case you would want to define your own typings definition `.d.ts` file for the library. The following code shows an example.
Your client-side solution might include multiple web parts. These web parts might need to
48
-
import or share the same library. In such cases, instead of bundling the library, you should include it in a separate JavaScript file to improve performance and caching. This is especially true of larger libraries.
46
+
Your client-side solution might include multiple web parts. These web parts might need to import or share the same library. In such cases, instead of bundling the library, you should include it in a separate JavaScript file to improve performance and caching. This is especially true of larger libraries.
49
47
50
48
### Example
51
49
52
50
In this example, you will share the [marked](https://www.npmjs.com/package/marked) package - a Markdown compiler - in a separate bundle.
53
51
54
52
Download the **marked** package from npm:
55
-
56
-
```
53
+
54
+
```sh
57
55
npm install marked --save
58
56
```
59
-
57
+
60
58
Download the typings:
61
-
59
+
62
60
```
63
61
npm install @types/marked --save
64
62
```
65
-
63
+
66
64
Edit the **config/config.json** and add an entry to the **externals** map. This is what tells the bundler to put this in a separate file. This prevents the bundler from bundling this library:
67
-
65
+
68
66
```json
69
67
"marked": "node_modules/marked/marked.min.js"
70
68
```
71
-
69
+
72
70
Add the statement to import the `marked` library in your web part now that we have added the package and typings for the library:
73
-
71
+
74
72
```typescript
75
73
import * as marked from 'marked';
76
74
```
77
-
75
+
78
76
Use the library in your web part:
79
-
77
+
80
78
```typescript
81
79
console.log(marked('I am using __markdown__.'));
82
80
```
@@ -87,36 +85,37 @@ Instead of loading the library from a npm package, you might want to load a scri
87
85
88
86
### Example
89
87
90
-
In this example, you will load jQuery from CDN. You don't need to install the npm package. However, you still need to install the typings.
88
+
In this example, you will load jQuery from CDN. You don't need to install the npm package. However, you still need to install the typings.
91
89
92
90
Install the typings for jQuery:
93
-
94
-
```
91
+
92
+
```sh
95
93
npm install --save @types/jquery
96
94
```
97
-
95
+
98
96
Update the `config.json` in the `config` folder to load jQuery from CDN. Add an entry to the `externals` field:
Some scripts follow the legacy JavaScript pattern of storing the library on the global namespace. This pattern
119
-
is now deprecated in favor of [Universal Module Definitions (UMD)](https://github.com/umdjs/umd)/[Asynchronous Module Definitions (AMD)](https://en.wikipedia.org/wiki/Asynchronous_module_definition) or [ES6 modules](https://github.com/lukehoban/es6features/blob/master/README.md#modules). However, you might need to load such libraries in your web part.
116
+
Some scripts follow the legacy JavaScript pattern of storing the library on the global namespace. This pattern is now deprecated in favor of [Universal Module Definitions (UMD)](https://github.com/umdjs/umd)/[Asynchronous Module Definitions (AMD)](https://en.wikipedia.org/wiki/Asynchronous_module_definition) or [ES6 modules](https://github.com/lukehoban/es6features/blob/master/README.md#modules). However, you might need to load such libraries in your web part.
117
+
118
+
> **Tip:** It's hard to determine manually whether the script that you're trying to load is an AMD or a non-AMD script. This is especially the case if the script that you're trying to load is minified. If your script is hosted on a publicly accessible URL, you can use the free [Rencore SharePoint Framework Script Check](https://rencore.com/sharepoint-framework/script-check/) tool to determine the type of script for you. Additionally, this tool will let you know whether the hosting ___location from which you're loading the script is properly configured.
120
119
121
120
To load a non-AMD module, you add an additional property to the entry in your **config.json** file.
122
121
@@ -133,39 +132,38 @@ var ContosoJS = {
133
132
};
134
133
```
135
134
136
-
137
135
Create typings for the script in a file called **contoso.d.ts** in the web part folder.
138
-
136
+
139
137
```typescript
140
138
declare module "contoso" {
141
-
interface IContoso {
142
-
say(text: string): void;
143
-
sayHello(name: string): void;
144
-
}
145
-
var contoso: IContoso;
146
-
export = contoso;
139
+
interface IContoso {
140
+
say(text: string): void;
141
+
sayHello(name: string): void;
142
+
}
143
+
var contoso: IContoso;
144
+
export = contoso;
147
145
}
148
146
```
149
-
147
+
150
148
Update the **config.json** file to include this script. Add an entry to the **externals** map:
151
-
149
+
152
150
```json
153
151
{
154
-
"contoso": {
155
-
"path": "https://contoso.com/contoso.js",
156
-
"globalName": "ContosoJS"
157
-
}
152
+
"contoso": {
153
+
"path": "https://contoso.com/contoso.js",
154
+
"globalName": "ContosoJS"
155
+
}
158
156
}
159
157
```
160
-
158
+
161
159
Add an import to your web part code:
162
-
160
+
163
161
```typescript
164
162
import contoso from 'contoso';
165
163
```
166
-
164
+
167
165
Use the contoso library in your code:
168
-
166
+
169
167
```typescript
170
168
contoso.sayHello(username);
171
169
```
@@ -174,11 +172,11 @@ contoso.sayHello(username);
174
172
175
173
Many libraries have dependencies on another library. You can specify such dependencies in the **config.json** file using the **globalDependencies** property.
176
174
177
-
Note that you don't have to specify this field for non-AMD modules; they will properly import each other. However, it is important to note that a non-AMD module have a AMD module as a dependency.
175
+
> **Important:** Note, that you don't have to specify this field for AMD modules; they will properly import each other. However, a non-AMD module cannot have an AMD module as a dependency. In some cases, it is possible to load an AMD script as a non-AMD script. This is often required when working with jQuery, which by itself is an AMD script, and jQuery plugins which most of the times are distributed as non-AMD scripts and which depend on jQuery.
178
176
179
177
There are two examples of this.
180
178
181
-
#### Non-AMD module has a non-AMD module dependency
179
+
### Non-AMD module has a non-AMD module dependency
182
180
183
181
This example involves two fictional scripts. These are in the **src/** folder, although they can also be loaded from a CDN.
184
182
@@ -205,8 +203,8 @@ var Contoso = {
205
203
};
206
204
```
207
205
208
-
Add or create tpyings for this class. In this case, you will create `Contoso.d.ts`, which contains typings for both JavaScript files.
209
-
206
+
Add or create typings for this class. In this case, you will create `Contoso.d.ts`, which contains typings for both JavaScript files.
207
+
210
208
**contoso.d.ts**
211
209
212
210
```typescript
@@ -224,7 +222,7 @@ declare module "contoso" {
224
222
```
225
223
226
224
Update the **config.json** file. Add two entries to **externals**:
227
-
225
+
228
226
```json
229
227
{
230
228
"contoso": {
@@ -238,16 +236,16 @@ Update the **config.json** file. Add two entries to **externals**:
238
236
}
239
237
}
240
238
```
241
-
239
+
242
240
Add imports for Contoso and ContosoUI:
243
-
241
+
244
242
```typescript
245
243
import contoso = require('contoso');
246
244
require('contoso-ui');
247
245
```
248
246
249
247
Use the libraries in your code:
250
-
248
+
251
249
```typescript
252
250
contoso.EventList.alert();
253
251
```
@@ -256,17 +254,19 @@ contoso.EventList.alert();
256
254
257
255
Loading SharePoint JSOM is essentially the same scenario as loading non-AMD scripts that have dependencies. This means using both the **globalName** and **globalDependency** options.
258
256
257
+
> **Important:** Please note, that the following approach will cause error in classic SharePoint pages, where SharePoint JSOM is already loaded. If you require your web part to work with both classic and modern pages then you should first check if SharePoint JSOM is already available and if it isn't, load it dynamically using the **SPComponentLoader**.
258
+
259
259
Install typings for Microsoft Ajax which is a dependency for JSOM typings:
260
260
261
-
```
261
+
```sh
262
262
npm install @types/microsoft-ajax --save
263
263
```
264
264
265
265
Install typings for the JSOM:
266
266
267
-
```
267
+
```sh
268
268
npm install @types/sharepoint --save
269
-
```
269
+
```
270
270
271
271
Add entries to the `config.json`:
272
272
@@ -295,7 +295,7 @@ Add entries to the `config.json`:
295
295
```
296
296
297
297
In your web part, add the require statements:
298
-
298
+
299
299
```typescript
300
300
require('sp-init');
301
301
require('microsoft-ajax');
@@ -305,7 +305,7 @@ require('sharepoint');
305
305
306
306
## Load localized resources
307
307
308
-
Loading localized resources is simple. There is a map in **config.json** called **localizedResources** with which you can describe how to load localized resources. Paths in this map are relative to the **lib** folder and must not contain a leading slash (**/**).
308
+
There is a map in **config.json** called **localizedResources** with which you can describe how to load localized resources. Paths in this map are relative to the **lib** folder and must not contain a leading slash (**/**).
309
309
310
310
In this example, you have a folder **src/strings/**. In this folder are several JavaScript files with names such as **en-us.js**, **fr-fr.js**, **de-de.js**. Because each of these files must be loadable by the module loader, they must contain a CommonJS wrapper. For example, in **en-us.js**:
311
311
@@ -326,7 +326,7 @@ Edit the **config.json** file. Add an entry to **localizedResources**. The **{lo
326
326
"strings": "strings/{locale}.js"
327
327
}
328
328
```
329
-
329
+
330
330
Add typings for your strings. In this case, you have a file **MyStrings.d.ts**:
0 commit comments