The Microsoft SharePoint Framework (SPFX) v1.13.1 does not have an option for Vue in their Yeoman generator. The PnP/SPFX generator has not been updated to support Vue 3 nor SPFX 1.13.1. This project serves as a brute-force sample of how you could develop in Vue 3 (and optionally Vite as your build environment) and then incorporate it into the latest version of SPFX webparts.
- You want to develop SPFX webparts (or application extensions) in Vue 3
- During development you want to take advantage of TDD and hot reloading that Webpack or Vite provides, which tends to be much faster than the gulp-based SharePoint workbench.
- You want to use Node v14. Previous versions of SPFX require Node v10 and do not work on later versions, whereas SPFX v1.13.1 works on Node v14.
- Automated the embedding process (post-build)
- Allowed for unique app IDs for each instance of the webpart so that multiple instances of the same webpart can be used on the same SharePoint page simultaneously.
- Synchronized the package.json version of the main web app with the webpart's package version and webpart version in webpart/config/package-solution.json
- Create a script to scaffold a new project based on this structure
- Consider loading Vue3 from cloud rather than embedding in each webpart
- Clone this repo to use for your webpart as a template
- Delete the .git folder and re-initialize this as your own git repo
- Create your Vue 3 web app in your normal, preferred way, within the top-level of this folder
- The index.html will allow you to develop and test the webpart web app inside of a simple container to mimick a SharePoint page
- Delete the contents of the webpart/ folder
- Generate the SPFX webpart inside of the webpart folder, selecting "No javascript framework":
yo @microsoft/sharepoint
- If using Vite, modify /vite.config.ts to set the output directory by adding the following build and assetsDir options:
export default defineConfig({
plugins: [vue()],
build: {
outDir: "webpart/src/webparts/assets",
assetsDir: "appcode"
},
})
- Replace your webpart.ts file contents with the one from the webpart-template/src/YOURWebpart/ folder
- Copy the webpart-template/src/lib/ folder into webpart/src/
- Replace all occurences of UNIQUECLIENTAPP with an ID name representing your web part
- Test your web part locally:
npm run dev
- Confirm that you can build the webpart, which should populate the webpart/src/webparts/assets/ folder with the distribution files. This process is repeated when run the make script but it's good to do a test build the first time. Once it appears to build correctly, use the make script going forward to build AND ship the webpart (see next step).
npm run build
- Build and ship the webpart using:
./make
- This should create the webpart sharepoint/solution/...sppkg package that you can upload to the SharePoint site's App Catalog
- Follow the normal procedure for deploying your webpart to SharePoint
- When the webpart is initialized, it will:
- run the render function
- create an app element
- call the Vue app scripts to run and populate that element with the Vue app
- optionally, pass editable properties into the Vue app (see Editable properties below)
If using @pnp/sp to connect with the SharePoint site:
- Install it at the project web app level (not the webpart/ sub-project):
npm install @pnp/sp
- Configure 'sp' in src/App.vue with the base path of the SharePoint site containing the web part. See example code in src/App.vue and src/lib/SharePointTools.ts
- You do not need to initialize 'sp' in the webpart. Just use it directly from the Vue 3 web app.
- This project is using @pnp/sp v2, not v3 which is the latest
- Define your editable properties in the webpart/src/webparts/...WebPart.ts file
- Pass them to the Vue 3 web app in one of several ways:
- data-attributes on the containing app element that the Vue app can retrieve
- sessionStorage
Make changes to the Vue3 app in your normal way. Then:
- Update package.json version (in the project directory, NOT the webpart/ subdirectory)
- From the project directory, run:
./make
- A SharePoint webpart package file with the new version number will be created that you can drag into the site's App Catalog: webpart/sharepoint/solution/
These settings in tsconfig.ts may be helpful:
- "skipLibCheck": true // to ignore type errors in packages such as @pnp/sp
- "exclude": ["**/*.stories.ts"] // to ignore react JSX types that don't apply to a Vue app
@LOLCATATONIA added these to webpart/package.json but this created build errors for me:
"dependencies": {
"@types/react-dom": "16.8.0",
"@types/react": "16.8.0"
}