From 0c10ede1caedd8731aca17bf95dee655e55ec46d Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 04:10:17 +1100 Subject: [PATCH 1/9] Convert package from CommonJS to ES Modules Revert conversion of preload to esm - Fixes preload script loading issues - Electron has better support for CommonJS in preload scripts --- package.json | 1 + scripts/launchdev.js | 6 +++--- src/main-process/appWindow.ts | 2 +- tsconfig.json | 2 +- vite.main.config.ts | 2 +- vite.preload.config.ts | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c35c011e..6f1f00fc 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "description": "The best modular GUI to run AI diffusion models.", "main": ".vite/build/main.js", "packageManager": "yarn@4.5.0", + "type": "module", "config": { "frontendVersion": "1.5.14", "comfyVersion": "0.3.7", diff --git a/scripts/launchdev.js b/scripts/launchdev.js index 8850afbd..77832307 100644 --- a/scripts/launchdev.js +++ b/scripts/launchdev.js @@ -1,6 +1,6 @@ -const {build} = require('vite'); -const electronPath = require('electron'); -const {spawn} = require('child_process'); +import { build } from 'vite' +import electronPath from 'electron' +import { spawn } from 'child_process' /** @type 'production' | 'development'' */ const mode = (process.env.MODE = process.env.MODE || 'development') diff --git a/src/main-process/appWindow.ts b/src/main-process/appWindow.ts index 3304f0fa..d051224c 100644 --- a/src/main-process/appWindow.ts +++ b/src/main-process/appWindow.ts @@ -45,7 +45,7 @@ export class AppWindow { x: storedX, y: storedY, webPreferences: { - preload: path.join(__dirname, '../build/preload.js'), + preload: path.join(import.meta.dirname, '../build/preload.cjs'), nodeIntegration: true, contextIsolation: true, webviewTag: true, diff --git a/tsconfig.json b/tsconfig.json index db4a872d..5f13ddcc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "jsx": "react", "target": "ESNext", - "module": "commonjs", + "module": "ES2022", "allowJs": true, "strict": true, "skipLibCheck": true, diff --git a/vite.main.config.ts b/vite.main.config.ts index bdc7a79b..af9a5a51 100644 --- a/vite.main.config.ts +++ b/vite.main.config.ts @@ -14,7 +14,7 @@ export default defineConfig((env) => { lib: { entry: './src/main.ts', fileName: () => '[name].js', - formats: ['cjs'], + formats: ['es'], }, rollupOptions: { external, diff --git a/vite.preload.config.ts b/vite.preload.config.ts index 3f14b8b4..7968173a 100644 --- a/vite.preload.config.ts +++ b/vite.preload.config.ts @@ -14,8 +14,8 @@ export default defineConfig((env: ConfigEnv) => { format: 'cjs', // It should not be split chunks. inlineDynamicImports: true, - entryFileNames: '[name].js', - chunkFileNames: '[name].js', + entryFileNames: '[name].cjs', + chunkFileNames: '[name].cjs', assetFileNames: '[name].[ext]', }, }, From f5e9b11c487ce9a15d0439f2698f0f8eedfa3690 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 05:22:32 +1100 Subject: [PATCH 2/9] Fix CommonJS files treated as ESM --- builder-debug.config.ts | 2 +- scripts/{preMake.js => preMake.cjs} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename scripts/{preMake.js => preMake.cjs} (100%) diff --git a/builder-debug.config.ts b/builder-debug.config.ts index 777d31c8..17f68322 100644 --- a/builder-debug.config.ts +++ b/builder-debug.config.ts @@ -8,7 +8,7 @@ const debugConfig: Configuration = { { from: './assets/uv/uvx', to: 'uv/uvx' }, { from: './assets/UI', to: 'UI' }, ], - beforeBuild: './scripts/preMake.js', + beforeBuild: './scripts/preMake.cjs', win: { icon: './assets/UI/Comfy_Logo.ico', target: 'zip', diff --git a/scripts/preMake.js b/scripts/preMake.cjs similarity index 100% rename from scripts/preMake.js rename to scripts/preMake.cjs From 62b86b13e0714bab958b332660ecf6ba05bf65e0 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 05:27:15 +1100 Subject: [PATCH 3/9] Convert CommonJS scripts to ES modules --- scripts/downloadFrontend.js | 12 +++++++----- scripts/downloadUV.js | 16 +++++++++------- scripts/getPackage.js | 7 +++++++ scripts/makeComfy.js | 8 ++++---- scripts/prepareTypes.js | 10 ++++------ scripts/releaseTypes.js | 4 ++-- scripts/resetInstall.js | 10 +++++----- scripts/updateFrontend.js | 4 ++-- 8 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 scripts/getPackage.js diff --git a/scripts/downloadFrontend.js b/scripts/downloadFrontend.js index faffb9ef..21866846 100644 --- a/scripts/downloadFrontend.js +++ b/scripts/downloadFrontend.js @@ -1,10 +1,12 @@ -const axios = require('axios'); -const extract = require('extract-zip'); -const fs = require('fs/promises'); -const path = require('path'); +import * as axios from 'axios' +import * as extract from 'extract-zip' +import fs from 'fs/promises' +import path from 'path' + +import packageJson from './getPackage.js' // Example "v1.3.34" -const version = process.argv[2] || require('../package.json').config.frontendVersion; +const version = process.argv[2] || packageJson.config.frontendVersion; if (!version) { console.error('No version specified'); process.exit(1); diff --git a/scripts/downloadUV.js b/scripts/downloadUV.js index 42db97e3..0b917263 100644 --- a/scripts/downloadUV.js +++ b/scripts/downloadUV.js @@ -1,10 +1,12 @@ -const path = require("path"); -const os = require('os'); -const fs = require('fs-extra'); -const axios = require('axios'); -const tar = require('tar'); -const extractZip = require('extract-zip'); -const uvVer = require('../package.json').config.uvVersion; +import path from "path" +import os from 'os' +import * as fs from 'fs-extra' +import * as axios from 'axios' +import * as tar from 'tar' +import * as extractZip from 'extract-zip' +import packageJson from './getPackage.js' + +const uvVer = packageJson.config.uvVersion; const options = { win32: { diff --git a/scripts/getPackage.js b/scripts/getPackage.js new file mode 100644 index 00000000..be959dde --- /dev/null +++ b/scripts/getPackage.js @@ -0,0 +1,7 @@ +// Read the main package.json +import { createRequire } from "module"; + +/** @type {import('../package.json')} */ +const packageJson = createRequire(import.meta.url)("../package.json"); + +export default packageJson; diff --git a/scripts/makeComfy.js b/scripts/makeComfy.js index e747e11e..4d3fde00 100644 --- a/scripts/makeComfy.js +++ b/scripts/makeComfy.js @@ -1,6 +1,6 @@ -const { execSync } = require('child_process'); -const pkg = require('../package.json'); -const fs = require('fs'); +import * as child_process from 'child_process' +import pkg from './getPackage.js' +import fs from 'fs' function makeAssets(gpuFlag) { const baseCommand = [ @@ -17,7 +17,7 @@ function makeAssets(gpuFlag) { ].join(' '); try { - execSync(baseCommand, { stdio: 'inherit' }); + child_process.execSync(baseCommand, { stdio: 'inherit' }); } catch (error) { console.error('Failed to make assets:', error); process.exit(1); diff --git a/scripts/prepareTypes.js b/scripts/prepareTypes.js index 32d6f241..8961876b 100644 --- a/scripts/prepareTypes.js +++ b/scripts/prepareTypes.js @@ -1,8 +1,6 @@ -const fs = require('fs'); -const path = require('path'); - -// Read the main package.json -const mainPackage = require('../package.json'); +import fs from 'fs' +import path from 'path' +import mainPackage from './getPackage.js' // Create the types-only package.json const typesPackage = { @@ -25,7 +23,7 @@ const typesPackage = { }; // Ensure dist directory exists -const distDir = path.join(__dirname, '../dist'); +const distDir = path.join(import.meta.dirname, '../dist'); if (!fs.existsSync(distDir)) { fs.mkdirSync(distDir, { recursive: true }); } diff --git a/scripts/releaseTypes.js b/scripts/releaseTypes.js index 0cde6213..a58622a1 100644 --- a/scripts/releaseTypes.js +++ b/scripts/releaseTypes.js @@ -1,5 +1,5 @@ -const { execSync } = require('child_process') -const { readFileSync } = require('fs') +import { execSync } from 'child_process' +import { readFileSync } from 'fs' try { // Create a new branch with version-bump prefix diff --git a/scripts/resetInstall.js b/scripts/resetInstall.js index 4d82b0e3..4548b26d 100644 --- a/scripts/resetInstall.js +++ b/scripts/resetInstall.js @@ -1,8 +1,8 @@ -const fs = require('fs'); -const path = require('path'); -const os = require('os'); -const yaml = require('yaml'); -const readline = require('readline'); +import fs from 'fs' +import path from 'path' +import os from 'os' +import * as yaml from 'yaml' +import * as readline from 'readline' /** * Get the path to the extra_models_config.yaml file based on the platform. diff --git a/scripts/updateFrontend.js b/scripts/updateFrontend.js index 70c311ec..a29f5b92 100644 --- a/scripts/updateFrontend.js +++ b/scripts/updateFrontend.js @@ -1,5 +1,5 @@ -const { execSync } = require('child_process') -const { readFileSync, writeFileSync } = require('fs') +import { execSync } from 'child_process' +import { readFileSync, writeFileSync } from 'fs' async function main() { try { From 8b2d5b835625c02dfb5bac8b5ca551043d7376e4 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 06:25:26 +1100 Subject: [PATCH 4/9] Fix type errors - missing node.js types --- package.json | 2 +- yarn.lock | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6f1f00fc..364281aa 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "@types/adm-zip": "^0.5.5", "@types/electron-squirrel-startup": "^1.0.2", "@types/jest": "^29.5.14", - "@types/node": "^22.5.0", + "@types/node": "^22.10.2", "@types/tar": "6.1.13", "@types/wait-on": "^5.3.4", "@typescript-eslint/eslint-plugin": "^5.0.0", diff --git a/yarn.lock b/yarn.lock index bd9100e0..d03f94e1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -551,7 +551,7 @@ __metadata: "@types/adm-zip": "npm:^0.5.5" "@types/electron-squirrel-startup": "npm:^1.0.2" "@types/jest": "npm:^29.5.14" - "@types/node": "npm:^22.5.0" + "@types/node": "npm:^22.10.2" "@types/tar": "npm:6.1.13" "@types/wait-on": "npm:^5.3.4" "@typescript-eslint/eslint-plugin": "npm:^5.0.0" @@ -3500,7 +3500,7 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:*, @types/node@npm:>=13.7.0, @types/node@npm:^22.5.0": +"@types/node@npm:*, @types/node@npm:>=13.7.0": version: 22.7.9 resolution: "@types/node@npm:22.7.9" dependencies: @@ -3518,6 +3518,15 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:^22.10.2": + version: 22.10.2 + resolution: "@types/node@npm:22.10.2" + dependencies: + undici-types: "npm:~6.20.0" + checksum: 10c0/2c7b71a040f1ef5320938eca8ebc946e6905caa9bbf3d5665d9b3774a8d15ea9fab1582b849a6d28c7fc80756a62c5666bc66b69f42f4d5dafd1ccb193cdb4ac + languageName: node + linkType: hard + "@types/pg-pool@npm:2.0.6": version: 2.0.6 resolution: "@types/pg-pool@npm:2.0.6" @@ -12757,6 +12766,13 @@ __metadata: languageName: node linkType: hard +"undici-types@npm:~6.20.0": + version: 6.20.0 + resolution: "undici-types@npm:6.20.0" + checksum: 10c0/68e659a98898d6a836a9a59e6adf14a5d799707f5ea629433e025ac90d239f75e408e2e5ff086afc3cace26f8b26ee52155293564593fbb4a2f666af57fc59bf + languageName: node + linkType: hard + "unique-filename@npm:^2.0.0": version: 2.0.1 resolution: "unique-filename@npm:2.0.1" From bbc23aea94ef38b71ad30327740cfeecb44a2bd4 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 06:43:13 +1100 Subject: [PATCH 5/9] Remove unnecessary type assertions --- vite.main.config.ts | 4 +--- vite.preload.config.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/vite.main.config.ts b/vite.main.config.ts index af9a5a51..3c1b8c33 100644 --- a/vite.main.config.ts +++ b/vite.main.config.ts @@ -6,8 +6,6 @@ import { version } from './package.json'; // https://vitejs.dev/config export default defineConfig((env) => { - const forgeEnv = env as ConfigEnv; - const config: UserConfig = { build: { outDir: '.vite/build', @@ -46,5 +44,5 @@ export default defineConfig((env) => { }, }; - return mergeConfig(getBuildConfig(forgeEnv), config); + return mergeConfig(getBuildConfig(env), config); }); diff --git a/vite.preload.config.ts b/vite.preload.config.ts index 7968173a..11ae331b 100644 --- a/vite.preload.config.ts +++ b/vite.preload.config.ts @@ -3,7 +3,7 @@ import { defineConfig, mergeConfig } from 'vite'; import { getBuildConfig, external } from './vite.base.config'; // https://vitejs.dev/config -export default defineConfig((env: ConfigEnv) => { +export default defineConfig((env) => { const config: UserConfig = { build: { rollupOptions: { From cfff98025c467bdb66118d62da0f1a6af35beade Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 07:46:21 +1100 Subject: [PATCH 6/9] Fix broken imports --- scripts/downloadFrontend.js | 4 ++-- scripts/downloadUV.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/downloadFrontend.js b/scripts/downloadFrontend.js index 21866846..ff415b09 100644 --- a/scripts/downloadFrontend.js +++ b/scripts/downloadFrontend.js @@ -1,5 +1,5 @@ -import * as axios from 'axios' -import * as extract from 'extract-zip' +import axios from 'axios' +import extract from 'extract-zip' import fs from 'fs/promises' import path from 'path' diff --git a/scripts/downloadUV.js b/scripts/downloadUV.js index 0b917263..c96d2f7d 100644 --- a/scripts/downloadUV.js +++ b/scripts/downloadUV.js @@ -1,9 +1,9 @@ import path from "path" import os from 'os' import * as fs from 'fs-extra' -import * as axios from 'axios' -import * as tar from 'tar' -import * as extractZip from 'extract-zip' +import axios from 'axios' +import tar from 'tar' +import extractZip from 'extract-zip' import packageJson from './getPackage.js' const uvVer = packageJson.config.uvVersion; From 45cb61e083f12814246d52ab723777874d6b5cbd Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 08:01:31 +1100 Subject: [PATCH 7/9] Rename CommonJS build / test scripts to .cjs --- jest.config.js => jest.config.cjs | 0 package.json | 6 +++--- scripts/todesktop/{afterPack.js => afterPack.cjs} | 0 scripts/todesktop/{beforeInstall.js => beforeInstall.cjs} | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename jest.config.js => jest.config.cjs (100%) rename scripts/todesktop/{afterPack.js => afterPack.cjs} (100%) rename scripts/todesktop/{beforeInstall.js => beforeInstall.cjs} (100%) diff --git a/jest.config.js b/jest.config.cjs similarity index 100% rename from jest.config.js rename to jest.config.cjs diff --git a/package.json b/package.json index 364281aa..039b2b4d 100644 --- a/package.json +++ b/package.json @@ -43,10 +43,10 @@ "sign": "node debug/sign.js", "start": "node ./scripts/launchdev.js", "test:e2e": "npx playwright test", - "test:unit": "jest --config jest.config.js", + "test:unit": "jest --config jest.config.cjs", "test:update-snapshots": "npx playwright test --update-snapshots", - "todesktop:afterPack": "./scripts/todesktop/afterPack.js", - "todesktop:beforeInstall": "./scripts/todesktop/beforeInstall.js", + "todesktop:afterPack": "./scripts/todesktop/afterPack.cjs", + "todesktop:beforeInstall": "./scripts/todesktop/beforeInstall.cjs", "typescript": "yarn run tsc", "vite:compile": "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", diff --git a/scripts/todesktop/afterPack.js b/scripts/todesktop/afterPack.cjs similarity index 100% rename from scripts/todesktop/afterPack.js rename to scripts/todesktop/afterPack.cjs diff --git a/scripts/todesktop/beforeInstall.js b/scripts/todesktop/beforeInstall.cjs similarity index 100% rename from scripts/todesktop/beforeInstall.js rename to scripts/todesktop/beforeInstall.cjs From e413f83c6f28186773b8e637b8aac3b4d51392dc Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 08:12:29 +1100 Subject: [PATCH 8/9] Fix playwright using cjs require --- playwright.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playwright.config.ts b/playwright.config.ts index 56d275bf..c07c405f 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -3,5 +3,5 @@ import { defineConfig } from '@playwright/test'; export default defineConfig({ testDir: './tests/integration', /* Run local instance before starting the tests */ - globalSetup: require.resolve('./playwright.setup'), + globalSetup: './playwright.setup', }); From 17deaa4877f9ccf0b79dbc2a4d052680a18b81c1 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 16 Dec 2024 22:48:08 +1100 Subject: [PATCH 9/9] Fix UV download script - ESM conversion error --- scripts/downloadUV.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/downloadUV.js b/scripts/downloadUV.js index c96d2f7d..9c72a2e1 100644 --- a/scripts/downloadUV.js +++ b/scripts/downloadUV.js @@ -1,8 +1,8 @@ import path from "path" import os from 'os' -import * as fs from 'fs-extra' +import fs from 'fs-extra' import axios from 'axios' -import tar from 'tar' +import * as tar from 'tar' import extractZip from 'extract-zip' import packageJson from './getPackage.js' @@ -82,4 +82,4 @@ async function downloadAndExtract(baseURL, options) { } //** Download and Extract UV. Default uses OS.Platfrom. Add 'all' will download all. Add 'none' will skip */ -downloadUV(); +await downloadUV();