Skip to content

Commit 89399a6

Browse files
waldekmastykarzVesaJuvonen
authored andcommitted
Updated SPFx Basics guidance (SharePoint#803)
* 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
1 parent 6be3e34 commit 89399a6

7 files changed

+237
-207
lines changed

docs/spfx/web-parts/basics/add-an-external-library.md

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
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.
44

5-
65
## Bundling a script
76

87
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
1211
Include the string validating library [validator](https://www.npmjs.com/package/validator) package into a web part.
1312

1413
Download the validator package from npm:
15-
16-
```
14+
15+
```sh
1716
npm install validator --save
1817
```
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+
2221
Create a file in the your web part's folder called `validator.d.ts` and add the following:
23-
22+
2423
>**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.
25-
24+
2625
```typescript
2726
declare module "validator" {
28-
export function isEmail(email: string): boolean;
29-
export function isAscii(text: string): boolean;
27+
export function isEmail(email: string): boolean;
28+
export function isAscii(text: string): boolean;
3029
}
3130
```
32-
31+
3332
In your web part file, import the typings:
34-
33+
3534
```typescript
3635
import * as validator from 'validator';
3736
```
38-
37+
3938
Use the validator library in your web part code:
40-
39+
4140
```typescript
4241
validator.isEmail('[email protected]');
4342
```
4443

4544
## Sharing a library among multiple WebParts
4645

47-
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.
4947

5048
### Example
5149

5250
In this example, you will share the [marked](https://www.npmjs.com/package/marked) package - a Markdown compiler - in a separate bundle.
5351

5452
Download the **marked** package from npm:
55-
56-
```
53+
54+
```sh
5755
npm install marked --save
5856
```
59-
57+
6058
Download the typings:
61-
59+
6260
```
6361
npm install @types/marked --save
6462
```
65-
63+
6664
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+
6866
```json
6967
"marked": "node_modules/marked/marked.min.js"
7068
```
71-
69+
7270
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+
7472
```typescript
7573
import * as marked from 'marked';
7674
```
77-
75+
7876
Use the library in your web part:
79-
77+
8078
```typescript
8179
console.log(marked('I am using __markdown__.'));
8280
```
@@ -87,36 +85,37 @@ Instead of loading the library from a npm package, you might want to load a scri
8785

8886
### Example
8987

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.
9189

9290
Install the typings for jQuery:
93-
94-
```
91+
92+
```sh
9593
npm install --save @types/jquery
9694
```
97-
95+
9896
Update the `config.json` in the `config` folder to load jQuery from CDN. Add an entry to the `externals` field:
99-
97+
10098
```json
10199
"jquery": "https://code.jquery.com/jquery-3.1.0.min.js"
102100
```
103-
101+
104102
Import jQuery in your web part:
105-
103+
106104
```typescript
107105
import * as $ from 'jquery';
108106
```
109-
107+
110108
Use jQuery in your web part:
111-
109+
112110
```javascript
113111
alert( $('#foo').val() );
114112
```
115113

116114
## Loading a non-AMD module
117115

118-
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.
120119
121120
To load a non-AMD module, you add an additional property to the entry in your **config.json** file.
122121

@@ -133,39 +132,38 @@ var ContosoJS = {
133132
};
134133
```
135134

136-
137135
Create typings for the script in a file called **contoso.d.ts** in the web part folder.
138-
136+
139137
```typescript
140138
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;
147145
}
148146
```
149-
147+
150148
Update the **config.json** file to include this script. Add an entry to the **externals** map:
151-
149+
152150
```json
153151
{
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+
}
158156
}
159157
```
160-
158+
161159
Add an import to your web part code:
162-
160+
163161
```typescript
164162
import contoso from 'contoso';
165163
```
166-
164+
167165
Use the contoso library in your code:
168-
166+
169167
```typescript
170168
contoso.sayHello(username);
171169
```
@@ -174,11 +172,11 @@ contoso.sayHello(username);
174172

175173
Many libraries have dependencies on another library. You can specify such dependencies in the **config.json** file using the **globalDependencies** property.
176174

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.
178176
179177
There are two examples of this.
180178

181-
#### Non-AMD module has a non-AMD module dependency
179+
### Non-AMD module has a non-AMD module dependency
182180

183181
This example involves two fictional scripts. These are in the **src/** folder, although they can also be loaded from a CDN.
184182

@@ -205,8 +203,8 @@ var Contoso = {
205203
};
206204
```
207205

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+
210208
**contoso.d.ts**
211209

212210
```typescript
@@ -224,7 +222,7 @@ declare module "contoso" {
224222
```
225223

226224
Update the **config.json** file. Add two entries to **externals**:
227-
225+
228226
```json
229227
{
230228
"contoso": {
@@ -238,16 +236,16 @@ Update the **config.json** file. Add two entries to **externals**:
238236
}
239237
}
240238
```
241-
239+
242240
Add imports for Contoso and ContosoUI:
243-
241+
244242
```typescript
245243
import contoso = require('contoso');
246244
require('contoso-ui');
247245
```
248246

249247
Use the libraries in your code:
250-
248+
251249
```typescript
252250
contoso.EventList.alert();
253251
```
@@ -256,17 +254,19 @@ contoso.EventList.alert();
256254

257255
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.
258256

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+
259259
Install typings for Microsoft Ajax which is a dependency for JSOM typings:
260260

261-
```
261+
```sh
262262
npm install @types/microsoft-ajax --save
263263
```
264264

265265
Install typings for the JSOM:
266266

267-
```
267+
```sh
268268
npm install @types/sharepoint --save
269-
```
269+
```
270270

271271
Add entries to the `config.json`:
272272

@@ -295,7 +295,7 @@ Add entries to the `config.json`:
295295
```
296296

297297
In your web part, add the require statements:
298-
298+
299299
```typescript
300300
require('sp-init');
301301
require('microsoft-ajax');
@@ -305,7 +305,7 @@ require('sharepoint');
305305

306306
## Load localized resources
307307

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 (**/**).
309309

310310
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**:
311311

@@ -326,7 +326,7 @@ Edit the **config.json** file. Add an entry to **localizedResources**. The **{lo
326326
"strings": "strings/{locale}.js"
327327
}
328328
```
329-
329+
330330
Add typings for your strings. In this case, you have a file **MyStrings.d.ts**:
331331

332332
```typescript
@@ -341,13 +341,13 @@ declare module 'mystrings' {
341341
export = strings;
342342
}
343343
```
344-
344+
345345
Add imports for the strings in your project:
346-
346+
347347
```typescript
348-
import * as strings from 'strings';
348+
import * as strings from 'mystrings';
349349
```
350-
350+
351351
Use the strings in your project:
352352

353353
```typescript

0 commit comments

Comments
 (0)