I have a TypeScript React project that is built in Vite. This is an Ionic React project that I build apps for Android and iOS as well as a PWA for the web.
I'm trying to use the latest version (13) of the cordova-purchase-plugin in my app. This version adds TypeScript support but it is not a module, so I'm confused about how to input it correctly (everything else in my app that I import is a module).
A very simple code example:
import 'cordova-plugin-purchase';
const store = new CdvPurchase.Store();
When I build this in Vite, it compiles with no errors. In VSCode, I can manipulate the store object and the plugin's built-in types are shown correctly.
However, when I open the PWA in my web browser, I get an error:
Can't find variable: CdvPurchase
So the import is failing somehow.
cordova-plugin-purchase includes a single JS file, store.js.
To get my compiled app to load, I can copy this store.js file into the assets directory of my app and then add it via the <script> tag in index.html. This puts CdvPurchase in global scope and allows my app to load. However, I obviously don't want to be manually adding scripts from node_modules to index.html-- that's what a build tool is for.
So how can I make sure the variable is imported/resolve this error?
More background
Previously, I was using the awesome-cordova-plugins wrapper to install the cordova-purchase-plugin. This works, but awesome-cordova-plugins is limited to cordova-purchase-plugin version 11, and I am trying to find a way to use version 13 in my app.
Here's how to use cordova-purchase-plugin with React 18 and Ionic React 7, TypeScript 5.
Partial example: