Skip to content

Commit 725336b

Browse files
committed
feat(capacitor): bundle store.js into ES module export
Users can now import the store directly: import { store, ProductType, Platform } from 'capacitor-plugin-cdv-purchase'; No more separate <script src="/store.js"> tag, no load ordering issues, no platform.ready() workaround needed. Implementation: - Rollup virtual module plugin inlines www/store.js into the bundle - Strips the Cordova setTimeout(initCDVPurchase) path so the store initializes synchronously (Capacitor has a Cordova shim that would otherwise delay it past module evaluation) - build/entry.mjs re-exports named conveniences (store, ProductType, Platform, LogLevel, ErrorCode, Logger, Iaptic, CdvPurchase) - types/index.d.ts provides typed declarations via the ambient CdvPurchase namespace from www/store.d.ts - package.json: types -> types/index.d.ts, added exports map
1 parent 8dd4d57 commit 725336b

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

capacitor/build/entry.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Capacitor bridge — sets CdvPurchaseCapacitor marker and registers native plugin
2+
export { PurchasePlugin } from '../dist/esm/index.js';
3+
4+
// Store runtime — executes the IIFE, creates window.CdvPurchase
5+
import { CdvPurchase } from 'virtual:store-js';
6+
7+
// Re-export the full namespace
8+
export { CdvPurchase };
9+
10+
// Convenience named exports
11+
export const store = CdvPurchase.store;
12+
export const Store = CdvPurchase.Store;
13+
export const ProductType = CdvPurchase.ProductType;
14+
export const Platform = CdvPurchase.Platform;
15+
export const LogLevel = CdvPurchase.LogLevel;
16+
export const ErrorCode = CdvPurchase.ErrorCode;
17+
export const Logger = CdvPurchase.Logger;
18+
export const Iaptic = CdvPurchase.Iaptic;

capacitor/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
"description": "In-App Purchases for Capacitor — iOS (StoreKit 2) and Android (Google Play Billing)",
55
"main": "dist/index.js",
66
"module": "dist/index.js",
7-
"types": "dist/index.d.ts",
7+
"types": "types/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./types/index.d.ts",
11+
"import": "./dist/index.js",
12+
"default": "./dist/index.js"
13+
}
14+
},
815
"capacitor": {
916
"ios": { "src": "ios" },
1017
"android": { "src": "android" }
@@ -19,12 +26,14 @@
1926
"typescript": "^5.0.0"
2027
},
2128
"scripts": {
22-
"build": "tsc && rollup -c rollup.config.mjs && cp dist/esm/*.d.ts dist/",
29+
"build": "tsc && rollup -c rollup.config.mjs",
2330
"prepublishOnly": "npm run build"
2431
},
2532
"files": [
2633
"dist/",
34+
"types/",
2735
"www/",
36+
"build/",
2837
"android/",
2938
"ios/"
3039
],

capacitor/rollup.config.mjs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
import resolve from '@rollup/plugin-node-resolve';
2+
import { readFileSync } from 'fs';
3+
import { fileURLToPath } from 'url';
4+
import { dirname, join } from 'path';
5+
6+
const __dirname = dirname(fileURLToPath(import.meta.url));
7+
8+
/**
9+
* Rollup plugin that serves www/store.js as a virtual ES module.
10+
* Appends `export { CdvPurchase }` so the IIFE-defined variable
11+
* can be imported by the entry file.
12+
*/
13+
function storeJsPlugin() {
14+
const VIRTUAL_ID = 'virtual:store-js';
15+
const RESOLVED_ID = '\0' + VIRTUAL_ID;
16+
17+
return {
18+
name: 'store-js',
19+
resolveId(id) {
20+
if (id === VIRTUAL_ID) return RESOLVED_ID;
21+
},
22+
load(id) {
23+
if (id === RESOLVED_ID) {
24+
let storeJs = readFileSync(join(__dirname, 'www', 'store.js'), 'utf-8');
25+
// In Capacitor, window.cordova exists (Cordova shim) which causes
26+
// initCDVPurchase to be called via setTimeout — too late for ES module
27+
// exports. Force synchronous initialization.
28+
storeJs = storeJs.replace(
29+
/if \(window\.cordova\) \{[\s\S]*?setTimeout\(initCDVPurchase.*?\)[\s\S]*?\}\s*else \{\s*initCDVPurchase\(\);\s*\}/,
30+
'initCDVPurchase();'
31+
);
32+
return storeJs + '\nexport { CdvPurchase };\n';
33+
}
34+
}
35+
};
36+
}
237

338
export default {
4-
input: 'dist/esm/index.js',
39+
input: 'build/entry.mjs',
540
output: {
641
file: 'dist/index.js',
742
format: 'es',
843
sourcemap: true
944
},
10-
plugins: [resolve()],
45+
plugins: [storeJsPlugin(), resolve()],
1146
external: ['@capacitor/core']
1247
};

capacitor/types/index.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="../www/store.d.ts" />
2+
3+
// Native bridge
4+
import type { PurchasePluginPlugin } from '../dist/esm/definitions';
5+
export declare const PurchasePlugin: PurchasePluginPlugin;
6+
export type { PurchasePluginPlugin };
7+
8+
// Full namespace
9+
export declare const CdvPurchase: typeof globalThis.CdvPurchase;
10+
11+
// Convenience value exports
12+
export declare const store: CdvPurchase.Store;
13+
export declare const Store: typeof CdvPurchase.Store;
14+
export declare const ProductType: typeof CdvPurchase.ProductType;
15+
export declare const Platform: typeof CdvPurchase.Platform;
16+
export declare const LogLevel: typeof CdvPurchase.LogLevel;
17+
export declare const ErrorCode: typeof CdvPurchase.ErrorCode;
18+
export declare const Logger: typeof CdvPurchase.Logger;
19+
export declare const Iaptic: typeof CdvPurchase.Iaptic;
20+
21+
// Convenience type re-exports
22+
export type IError = CdvPurchase.IError;
23+
export type Product = CdvPurchase.Product;
24+
export type Offer = CdvPurchase.Offer;
25+
export type Transaction = CdvPurchase.Transaction;
26+
export type Receipt = CdvPurchase.Receipt;
27+
export type VerifiedReceipt = CdvPurchase.VerifiedReceipt;

0 commit comments

Comments
 (0)