This guide walks through how to set up an blank app from vite and turn it into a Power Apps Code Apps.
This guide covers:
- Configuring a TypeScript app using the Power Platform SDK
Prerequisites for Power Apps Code Apps
- Power Platform environment with Code Apps enabled
- Visual Studio Code
- Node.js (LTS version)
- Power Platform Tools for VS Code
Initialize your Vite App
-
Open Visual Studio Code and open a new PowerShell terminal and enter:
mkdir C:\CodeApps -Force cd C:\CodeApps npm create vite@latest AppFromScratch -- --template react-ts cd C:\CodeApps\AppFromScratch npm install
-
If you are asked, agree to install
create-vite -
Accept the default package name
appfromscratchby pressing Enter. -
If you are asked to select a framework, select React.
-
If you are asked to select a variant, select TypeScript.
-
At this time, the Power SDK requires the port to be 3000 in the default configuration.
Install the node type defintions using:
npm i --save-dev @types/node
Open the
vite.config.ts, and update to be:import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import * as path from 'path' // https://vite.dev/config/ export default defineConfig({ base: "./", server: { host: "::", port: 3000, }, plugins: [react()], resolve: { alias: { "@": path.resolve(__dirname, "./src"), }, }, });
-
Save the file.
-
Enter the following to test your vite app:
npm run dev
[!NOTE] If you are developing on macOS, you may need to update package.json to not reference ‘start vite’. For instance you’d change the dev entry from
"scripts": { "dev": "start vite && start pac code run", "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview" }to
"scripts": { "dev": "vite && pac code run", "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview" }
- The template project will start and run locally. Browse to the http://localhost:3000 address given.
[!IMPORTANT] If you do not see the port 3000, then revisit the steps above.
- Press
Ctrl + Cto stop the local server when you have checked it runs ok.
Initialize your Code App
-
Authenticate the Power Platform CLI against your Power Platform tenant:
pac auth create
Login as your Power Platform account when prompted.
[!NOTE] You can also use the Power Platform Tools VS Code Extension to do this.
-
Select your environment using:
pac env select -env <URL of your environment>
You can also use the Power Platform Tools VS Code Extension to select the Environment
-
Initialize your code app using:
pac code init --displayName "App From Scratch"
Notice that there is now a
power.config.jsonfile in your project. -
Install the Power SDK using:
npm install --save-dev "@microsoft/power-apps@https://github.com/microsoft/PowerAppsCodeApps/releases/download/v0.0.4/7-31-pa-client-power-code-sdk-0.0.1.tgz"
[!IMPORTANT] This SDK is currently not yet available on
npmjs.comand must be installed from the GitHub release.
-
Open the
package.json, and update the existing line:"dev": "vite"
to be:
"dev": "start pac code run && vite",
[!NOTE]
If you are developing on macOS, use:"dev": "pac code run & vite"
Save the updated
package.json. -
Add a new file under the
srcfolder namedPowerProvider.tsxand grab the code from PowerProvider.tsx -
Save the file.
-
Open
main.tsxand add the following import under the existing imports:import PowerProvider from './PowerProvider.tsx' -
Update
main.tsx<StrictMode> <App /> </StrictMode>,to be
<StrictMode> <PowerProvider> <App /> </PowerProvider> </StrictMode>, -
Save the file
-
You can now test the Code App by using:
npm run devThis will run the vite server, but also start the Power SDK server:
-
Open the URL provided by the Power SDK Server. Important: Open the url in the same browser profile as your power platform tenant.