Note: I am going to write some dark magic with PCF. It is not currently supported by Microsoft. These are my personal findings. Views are my own. But if you want to dismantle PCF and want to test the boundary, then read further.
The source code can be downloaded at my github.
Microsoft has announced public preview of PowerApps Component Framework (PCF) and its Command Line Interface (CLI) in April 2019. Many developers, including me, have been anticipating this new feature. I find the documentation useful. A few people has started building and sharing new controls. No disappointment from this beautiful community.
Beyond absolute need
There is no doubt you can build any control following Microsoft documentation. But, I don’t want to stop here. I want to push its boundary and limits. So far, I recommend adding 3 more types of knowledge.
2. React for UI components
When the sample from Microsoft documentation shows building the UI using TypeScript, it reminds me of my student days when I had an assignment task asking to build a UI in C++. I could not force myself to create UI in C++ program.
There are many libraries to choose. However, you can find react and react-dom packges under node_modules after executing “npm install” at step 4.. So, React is the choice. You can also use JSX which makes life easier when building DOMs.
2. NPM for packages
3. Webpack for bundling
A module bundler. It will bundle all your resources, and packages from NPM, as a single package. Referring to CDN will only work when there is connectivity (or it is dependency). If we want to use it offline in mobile devices, we may encounter some issues.
The good news is that you don’t need to worry about NPM and webpack. PCF CLI handles them gracefully in most cases. I did however notice that there are some issues with adding large NPM packages (>500kb). The bundling tends to behave weirdly.
Create a new PCF Project
You use “pac pcf init” command to create a new PCF project. It will pre-create ControlManifest.Input.xml and index.ts.
Create a react component
Let’s create DemoComponent.tsx and write some scripts. The sample source can be found at my github repository.
Run ‘npm run build’ command. You will get an error saying ‘–jsx’ is not set. To resolve this, you need to add “jsx”: “react” in tsconfig.json located under your root folder. After this, you will be able to run ‘npm run build’ command without any issue.
Now, let’s render DemoControl in index.ts. You will see an error and you will not be able to run ‘npm run build’ command.
Now, let’s change index.ts to index.tsx and you will see the error is gone from IDE. You also need to update the code file extension in ControlManifest.Input.xml.
But, when you run ‘npm run build’, you run into another issue saying ‘Code file needs to be a typescript (.ts)’.
Looking at the error, it comes from pcf-scripts module which is located under node_modules folder in your PCF project.
A quick search using the error message brings me to line 84 at controlcontext.js under pcf-scripts module.
You simply need to update ‘if’ statement to allow .tsx extension and save it. And Magic happens.
Now, if you run ‘npm run build’, it will succeed. Next, it’s time to execute ‘npm start’. When the browser is launched, you will see the error ‘React’ is not defined.
To address this error, open index.html under your pcf root folder\node_modules\pcf-start and add following two script tags.
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
As soon as you save your changes, your react component will be rendered.
Even though you can deploy React control to model-driven app, this approach is not currently supported by Microsoft. I am sharing this only for experimental purposes and showing how PCF can be extended using modern libraries. I am not a pro-React developer nor a TypeScript developer, therefore, I may have approached this the wrong way. If you have any comments, please share them below.