Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: defer image tooling for metadata commands

NgoQuocViet20016880225c1206
diff --git a/bin/options/icon.ts b/bin/options/icon.ts
index 5033a5d7..1a8e9ffa 100644
--- a/bin/options/icon.ts
+++ b/bin/options/icon.ts
@@ -3,8 +3,6 @@ import fsExtra from 'fs-extra';
import chalk from 'chalk';
import { dir } from 'tmp-promise';
import { fileTypeFromBuffer } from 'file-type';
-import icongen from 'icon-gen';
-import sharp from 'sharp';
import logger from './logger';
import { getSpinner } from '@/utils/info';
@@ -29,6 +27,13 @@ type PlatformIconConfig = {
sizes?: number[];
size?: number;
};
+
+type Sharp = (typeof import('sharp'))['default'];
+
+async function loadSharp(): Promise<Sharp> {
+ return (await import('sharp')).default;
+}
+
const ICON_CONFIG = {
minFileSize: 100,
supportedFormats: [
@@ -134,6 +139,7 @@ async function preprocessIcon(inputPath: string): Promise<string> {
return inputPath;
}
+ const sharp = await loadSharp();
const { path: tempDir } = await dir();
const outputPath = path.join(tempDir, 'icon-normalized.png');
@@ -153,6 +159,7 @@ async function preprocessIcon(inputPath: string): Promise<string> {
*/
async function applyMacOSMask(inputPath: string): Promise<string> {
try {
+ const sharp = await loadSharp();
const { path: tempDir } = await dir();
const outputPath = path.join(tempDir, 'icon-macos-rounded.png');
@@ -221,6 +228,7 @@ async function convertIconFormat(
// Generate platform-specific format
if (IS_WIN) {
+ const sharp = await loadSharp();
const icoPath = path.join(
platformOutputDir,
`${iconName}_256${PLATFORM_CONFIG.win.format}`,
@@ -245,6 +253,7 @@ async function convertIconFormat(
}
if (IS_LINUX) {
+ const sharp = await loadSharp();
const outputPath = path.join(
platformOutputDir,
`${iconName}_${PLATFORM_CONFIG.linux.size}${PLATFORM_CONFIG.linux.format}`,
@@ -265,6 +274,7 @@ async function convertIconFormat(
// macOS
const macIconPath = await applyMacOSMask(processedInputPath);
+ const { default: icongen } = await import('icon-gen');
await icongen(macIconPath, platformOutputDir, {
report: false,
icns: { name: iconName, sizes: PLATFORM_CONFIG.macos.sizes },
@@ -288,6 +298,7 @@ async function isLinuxBundleIconReady(iconPath: string): Promise<boolean> {
}
try {
+ const sharp = await loadSharp();
const { width, height } = await sharp(iconPath).metadata();
return (
width === PLATFORM_CONFIG.linux.size &&
diff --git a/bin/utils/ico.ts b/bin/utils/ico.ts
index 182d51cf..7732113e 100644
--- a/bin/utils/ico.ts
+++ b/bin/utils/ico.ts
@@ -1,6 +1,11 @@
import path from 'path';
import fsExtra from 'fs-extra';
-import sharp from 'sharp';
+
+type Sharp = (typeof import('sharp'))['default'];
+
+async function loadSharp(): Promise<Sharp> {
+ return (await import('sharp')).default;
+}
const ICO_HEADER_SIZE = 6;
const ICO_DIR_ENTRY_SIZE = 16;
@@ -186,6 +191,7 @@ async function pickLargestFrameAsPng(
// Fallback: let sharp render directly from the ICO buffer. sharp picks the
// largest embedded frame on its own.
try {
+ const sharp = await loadSharp();
return await sharp(buffer).png().toBuffer();
} catch {
return null;
@@ -218,6 +224,7 @@ export async function ensureMultiResolutionIco(
);
}
+ const sharp = await loadSharp();
const frames = await Promise.all(
desiredSizes.map(async (size) => {
// Reuse an existing exact-size PNG frame when possible to keep any
diff --git a/dist/cli.js b/dist/cli.js
index fa8474b4..9fb4b673 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -14,8 +14,6 @@ import fs from 'fs';
import fs$1 from 'fs/promises';
import { dir } from 'tmp-promise';
import { fileTypeFromBuffer } from 'file-type';
-import icongen from 'icon-gen';
-import sharp from 'sharp';
import * as psl from 'psl';
import { InvalidArgumentError, program as program$1, Option } from 'commander';
@@ -2114,6 +2112,9 @@ function getIconSourcePriority(url, appName) {
: ['domain', 'dashboard'];
}
+async function loadSharp$1() {
+ return (await import('sharp')).default;
+}
const ICO_HEADER_SIZE = 6;
const ICO_DIR_ENTRY_SIZE = 16;
const ICO_TYPE_ICON = 1;
@@ -2247,6 +2248,7 @@ async function pickLargestFrameAsPng(buffer, entries) {
// Fallback: let sharp render directly from the ICO buffer. sharp picks the
// largest embedded frame on its own.
try {
+ const sharp = await loadSharp$1();
return await sharp(buffer).png().toBuffer();
}
catch {
@@ -2268,6 +2270,7 @@ async function ensureMultiResolutionIco(sourcePath, outputPath, preferredSize =
if (!sourcePng) {
return await writeIcoWithPreferredSize(sourcePath, outputPath, preferredSize);
}
+ const sharp = await loadSharp$1();
const frames = await Promise.all(desiredSizes.map(async (size) => {
// Reuse an existing exact-size PNG frame when possible to keep any
// hand-tuned small icon (e.g. a 16x16 with deliberate pixel hinting).
@@ -2334,6 +2337,9 @@ function buildIcoFromPngBuffers(frames) {
return output;
}
+async function loadSharp() {
+ return (await import('sharp')).default;
+}
const ICON_CONFIG = {
minFileSize: 100,
supportedFormats: [
@@ -2414,6 +2420,7 @@ async function preprocessIcon(inputPath) {
if (!shouldNormalize) {
return inputPath;
}
+ const sharp = await loadSharp();
const { path: tempDir } = await dir();
const outputPath = path.join(tempDir, 'icon-normalized.png');
await sharp(inputPath).ensureAlpha().png().toFile(outputPath);
@@ -2431,6 +2438,7 @@ async function preprocessIcon(inputPath) {
*/
async function applyMacOSMask(inputPath) {
try {
+ const sharp = await loadSharp();
const { path: tempDir } = await dir();
const outputPath = path.join(tempDir, 'icon-macos-rounded.png');
// 1. Create a 1024x1024 rounded rect mask
@@ -2488,6 +2496,7 @@ async function convertIconFormat(inputPath, appName) {
const iconName = getIconBaseName(appName);
// Generate platform-specific format
if (IS_WIN) {
+ const sharp = await loadSharp();
const icoPath = path.join(platformOutputDir, `${iconName}_256${PLATFORM_CONFIG.win.format}`);
const sourceBuffer = await fsExtra.readFile(processedInputPath);
const frames = await Promise.all(PLATFORM_CONFIG.win.sizes.map(async (size) => {
@@ -2506,6 +2515,7 @@ async function convertIconFormat(inputPath, appName) {
return icoPath;
}
if (IS_LINUX) {
+ const sharp = await loadSharp();
const outputPath = path.join(platformOutputDir, `${iconName}_${PLATFORM_CONFIG.linux.size}${PLATFORM_CONFIG.linux.format}`);
// Ensure we convert to proper PNG format with correct size
await sharp(processedInputPath)
@@ -2520,6 +2530,7 @@ async function convertIconFormat(inputPath, appName) {
}
// macOS
const macIconPath = await applyMacOSMask(processedInputPath);
+ const { default: icongen } = await import('icon-gen');
await icongen(macIconPath, platformOutputDir, {
report: false,
icns: { name: iconName, sizes: PLATFORM_CONFIG.macos.sizes },
@@ -2539,6 +2550,7 @@ async function isLinuxBundleIconReady(iconPath) {
return false;
}
try {
+ const sharp = await loadSharp();
const { width, height } = await sharp(iconPath).metadata();
return (width === PLATFORM_CONFIG.linux.size &&
height === PLATFORM_CONFIG.linux.size);
diff --git a/tests/index.js b/tests/index.js
index 63c5be29..cea133d3 100644
--- a/tests/index.js
+++ b/tests/index.js
@@ -7,12 +7,21 @@
* test files with a single, easy-to-use interface.
*/
-import { execSync, spawn } from "child_process";
+import { execSync, spawn, spawnSync } from "child_process";
import fs from "fs";
import path from "path";
import ora from "ora";
import config, { TIMEOUTS, TEST_URLS } from "./config.js";
+const rejectImageDependenciesLoader = `data:text/javascript,${encodeURIComponent(`
+ export async function resolve(specifier, context, nextResolve) {
+ if (specifier === "sharp" || specifier === "icon-gen") {
+ throw new Error("Image dependency loaded during CLI startup: " + specifier);
+ }
+ return nextResolve(specifier, context);
+ }
+`)}`;
+
class PakeTestRunner {
constructor() {
this.results = [];
@@ -200,6 +209,31 @@ class PakeTestRunner {
TIMEOUTS.QUICK,
);
+ // Metadata-only commands must not initialize native image tooling.
+ await this.runTest(
+ "Version Command Without Image Dependencies",
+ () => {
+ const result = spawnSync(
+ process.execPath,
+ [
+ "--no-warnings",
+ "--experimental-loader",
+ rejectImageDependenciesLoader,
+ config.CLI_PATH,
+ "--version",
+ ],
+ {
+ encoding: "utf8",
+ timeout: TIMEOUTS.QUICK,
+ },
+ );
+ return (
+ result.status === 0 && /^\d+\.\d+\.\d+/.test(result.stdout.trim())
+ );
+ },
+ TIMEOUTS.QUICK,
+ );
+
// Help command test
await this.runTest(
"Help Command",