Skip to content

Commit

Permalink
Build types for ElectronAPI (#203)
Browse files Browse the repository at this point in the history
* Add vite-dts

* Build types for ElectronAPI

* prefix @comfyorg on package name

* Add publish config

* Regen lock

* Add prepare types script

* nit
  • Loading branch information
huchenlei authored Nov 7, 2024
1 parent 3d451ed commit ed73db3
Show file tree
Hide file tree
Showing 5 changed files with 677 additions and 66 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "comfyui-electron",
"name": "@comfyorg/comfyui-electron",
"productName": "ComfyUI",
"repository": "github:comfy-org/electron",
"copyright": "Copyright © 2024 Comfy Org",
Expand Down Expand Up @@ -44,7 +44,8 @@
"todesktop:afterPack": "./scripts/todesktop/afterPack.js",
"todesktop:beforeInstall": "./scripts/todesktop/beforeInstall.js",
"typescript": "yarn run tsc",
"vite:compile": "vite build --config vite.renderer.config.ts && vite build --config vite.main.config.ts && vite build --config vite.preload.config.ts"
"vite:compile": "vite build --config vite.renderer.config.ts && vite build --config vite.main.config.ts && vite build --config vite.preload.config.ts",
"vite:types": "vite build --config vite.types.config.ts && node scripts/prepareTypes.js"
},
"devDependencies": {
"@electron/fuses": "^1.8.0",
Expand Down Expand Up @@ -73,7 +74,8 @@
"ts-jest": "^29.2.5",
"ts-node": "^10.0.0",
"typescript": "~5.5.4",
"vite": "^5.0.12"
"vite": "^5.0.12",
"vite-plugin-dts": "^4.3.0"
},
"keywords": [],
"author": {
Expand Down
45 changes: 45 additions & 0 deletions scripts/prepareTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const fs = require('fs');
const path = require('path');

// Read the main package.json
const mainPackage = require('../package.json');

// Create the types-only package.json
const typesPackage = {
name: `${mainPackage.name}-types`,
version: mainPackage.version,
types: './index.d.ts',
exports: {
'.': {
types: './index.d.ts'
}
},
files: [
'index.d.ts'
],
publishConfig: {
access: 'public'
},
repository: mainPackage.repository,
homepage: mainPackage.homepage,
description: `TypeScript definitions for ${mainPackage.name}`,
author: mainPackage.author,
license: mainPackage.license,
};

// Ensure dist directory exists
const distDir = path.join(__dirname, '../dist');
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir, { recursive: true });
}

// Write the new package.json to the dist directory
fs.writeFileSync(
path.join(distDir, 'package.json'),
JSON.stringify(typesPackage, null, 2)
);

// Create an empty yarn.lock file
fs.writeFileSync(path.join(distDir, 'yarn.lock'), '');

console.log('Types package.json and yarn.lock have been prepared in the dist directory');
9 changes: 9 additions & 0 deletions src/main_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './constants';
export * from './preload';
import { ElectronAPI } from './preload';

declare global {
interface Window {
electronAPI: ElectronAPI;
}
}
22 changes: 22 additions & 0 deletions vite.types.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { resolve } from 'path';

export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/main_types.ts'),
name: 'comfyui-electron-api',
fileName: 'index',
formats: ['es'],
},
rollupOptions: {
external: ['electron'],
},
},
plugins: [
dts({
rollupTypes: true,
}),
],
});
Loading

0 comments on commit ed73db3

Please sign in to comment.