Skip to content

Commit 3cf37f6

Browse files
committed
Update the Webpack requirements for various SPFx versions
1 parent 33a7b00 commit 3cf37f6

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

concepts/toolkit/get-started/build-a-sharepoint-web-part.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ This article covers how to use Microsoft Graph Toolkit components in a [SharePoi
2424

2525
Follow the steps to [Set up your SharePoint Framework development environment](/sharepoint/dev/spfx/set-up-your-development-environment).
2626

27-
2827
# [React based web parts](#tab/react)
2928

3029
## Create your web part project
@@ -49,7 +48,7 @@ The Microsoft Graph Toolkit providers enable authentication and access to Micros
4948
First, add the provider to your web part. Locate the `src\webparts\<your-project>\<your-web-part>.ts` file in your project folder, and add the following line to the top of your file, right below the existing `import` statements:
5049

5150
```ts
52-
import { Providers } from '@microsoft/mgt-element';
51+
import { Providers } from "@microsoft/mgt-element";
5352
import { SharePointProvider } from "@microsoft/mgt-sharepoint-provider";
5453
```
5554

@@ -93,7 +92,9 @@ First, convert the import of the component to use [`React.lazy`](https://react.d
9392
```ts
9493
const MgtComponent = React.lazy(
9594
() =>
96-
import(/* webpackChunkName: 'mgt-react-component' */ "./components/<WebPartName>")
95+
import(
96+
/* webpackChunkName: 'mgt-react-component' */ "./components/<WebPartName>"
97+
)
9798
);
9899
```
99100

@@ -174,30 +175,31 @@ export default class HelloWorldWebPart extends BaseClientSideWebPart<IHelloWorld
174175
Add the components to the React component. Locate and open the `src\webparts\<your-project>\components\<your-component>.tsx` file and add the import for the component you want to use - in this case, the `Person` component - and then update the `render()` method to use the Person component. Now your component should look like this:
175176

176177
```tsx
177-
import * as React from 'react';
178-
import type { IHelloWorldProps } from './IHelloWorldProps';
179-
import { Person } from '@microsoft/mgt-react';
178+
import * as React from "react";
179+
import type { IHelloWorldProps } from "./IHelloWorldProps";
180+
import { Person } from "@microsoft/mgt-react";
180181
181182
export default class HelloWorld extends React.Component<IHelloWorldProps, {}> {
182183
public render(): React.ReactElement<IHelloWorldProps> {
183-
return (<Person personQuery="me" view="twolines" />);
184+
return <Person personQuery="me" view="twolines" />;
184185
}
185186
}
186187
```
187188

188189
Or if you prefer to use React Functional Components:
189190

190191
```tsx
191-
import * as React from 'react';
192-
import type { IHelloWorldProps } from './IHelloWorldProps';
193-
import { Person, ViewType } from '@microsoft/mgt-react';
192+
import * as React from "react";
193+
import type { IHelloWorldProps } from "./IHelloWorldProps";
194+
import { Person, ViewType } from "@microsoft/mgt-react";
194195
195-
const HelloWorld = (props: IHelloWorldProps): React.ReactElement => <Person personQuery="me" view={ViewType.twolines} />;
196+
const HelloWorld = (props: IHelloWorldProps): React.ReactElement => (
197+
<Person personQuery="me" view={ViewType.twolines} />
198+
);
196199
197200
export default HelloWorld;
198201
```
199202

200-
201203
# [No framework web parts](#tab/html)
202204

203205
## Create your web part project
@@ -219,7 +221,7 @@ The Microsoft Graph Toolkit providers enable authentication and access to Micros
219221
First, add the provider to your web part. Locate the `src\webparts\<your-project>\<your-web-part>.ts` file in your project folder, and add the following line to the top of your file, right below the existing `import` statements:
220222

221223
```ts
222-
import { Providers } from '@microsoft/mgt-element';
224+
import { Providers } from "@microsoft/mgt-element";
223225
import { SharePointProvider } from "@microsoft/mgt-sharepoint-provider";
224226
```
225227

@@ -240,7 +242,7 @@ To ensure that your web part works if there are multiple web part solutions usin
240242
First, update your imports from `@microsoft/mgt-element`
241243

242244
```ts
243-
import { Providers, customElementHelper } from '@microsoft/mgt-element';
245+
import { Providers, customElementHelper } from "@microsoft/mgt-element";
244246
```
245247

246248
Next, update the `onInit()` method to set up disambiguation. The string used for disambiguation must be unique to your SharePoint Framework solution:
@@ -259,7 +261,7 @@ protected async onInit() {
259261
Now, you can start adding components to your web part. First import the relevant register functions:
260262

261263
```ts
262-
import { registerMgtPersonComponent } from '@microsoft/mgt-components';
264+
import { registerMgtPersonComponent } from "@microsoft/mgt-components";
263265
```
264266

265267
> [!NOTE]
@@ -334,6 +336,8 @@ npm i --save-dev babel-loader@8.3.0 @babel/plugin-transform-optional-chaining @b
334336

335337
### Modify the webpack configuration
336338

339+
#### Webpack version <=4
340+
337341
SharePoint Framework provides an extensibility model to [modify the webpack configuration](/sharepoint/dev/spfx/toolchain/extending-webpack-in-build-pipeline) used to bundle the web parts. Locate and open `gulpfile.js`. Add the following code above the line containing `build.initialize(require('gulp'));`
338342

339343
```JavaScript
@@ -348,7 +352,7 @@ build.configureWebpack.mergeConfig({
348352
generatedConfiguration.module.rules.push({
349353
test: /\.js$/,
350354
// only run on lit packages
351-
include: resourcePath =>
355+
include: resourcePath =>
352356
litFolders.some(litFolder => resourcePath.includes(litFolder)),
353357
use: {
354358
loader: 'babel-loader',
@@ -366,7 +370,11 @@ build.configureWebpack.mergeConfig({
366370
});
367371
```
368372

369-
This ensures that the code from the `lit` library is correctly processed by the SharePoint Framework build chain.
373+
This ensures that the code from the `lit` library is correctly processed by the SharePoint Framework build chain. This is required for Sharepoint provider `<=v1.18`.
374+
375+
#### Webpack version 5+
376+
377+
There is no additional configuration when you're using version 5+ of webpack. This is supported with the Sharepoint provider `v1.19+`.
370378

371379
## Build and deploy your web part
372380

0 commit comments

Comments
 (0)