Update Linux package build command and enhance PDF utility imports
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good
- Modify the Linux package build command in package.json to include the --no-bytecode flag for improved compatibility with ESM packages. - Refactor pdfUtils.js to handle dynamic imports more robustly, ensuring compatibility with pkg snapshots and improving error handling for ESM modules. - Simplify image processing logic by removing unnecessary default property access for the sharp module.
This commit is contained in:
parent
5fe8d76310
commit
01b3cc1aa1
@ -17,7 +17,7 @@
|
||||
"build:electron:mac": "pnpm build && electron-builder --mac dmg --arm64 --x64 && electron-builder --mac pkg --arm64 && electron-builder --mac pkg --x64",
|
||||
"build:linux": "pnpm run cleanBuild && pnpm run build:server && pnpm run build:linux-bundle && pnpm run build:linux-pkg && pnpm run build:linux-packages",
|
||||
"build:linux-bundle": "node scripts/build-linux-bundle.mjs",
|
||||
"build:linux-pkg": "shx mkdir -p app_dist/linux && pnpm exec pkg build/pkg-entry.cjs --config package.json --targets node18-linux-x64 --public-packages \"pdf-to-img,pdfjs-dist,sharp,canvas,@img/*,@napi-rs/*\" --output app_dist/linux/farmcontrol-server",
|
||||
"build:linux-pkg": "shx mkdir -p app_dist/linux && pnpm exec pkg build/pkg-entry.cjs --config package.json --targets node18-linux-x64 --no-bytecode --public-packages \"pdf-to-img,pdfjs-dist,sharp,canvas,@img/*,@napi-rs/*\" --output app_dist/linux/farmcontrol-server",
|
||||
"build:linux-packages": "bash scripts/build-linux-packages.sh",
|
||||
"cleanBuild": "rimraf build"
|
||||
},
|
||||
|
||||
@ -7,17 +7,36 @@ import { pathToFileURL } from 'node:url'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
// Bare dynamic import() fails in pkg snapshots with "Invalid host defined options".
|
||||
// Dynamic import() from pkg snapshot bytecode throws "Invalid host defined options"
|
||||
// (nodejs/node#43663). Use require() for CJS packages in pkg; ESM packages need
|
||||
// --no-bytecode on the pkg build so file-URL import() works.
|
||||
async function importPackage(name) {
|
||||
const packagePath = require.resolve(name)
|
||||
|
||||
if (typeof process.pkg !== 'undefined') {
|
||||
try {
|
||||
return require(packagePath)
|
||||
} catch (error) {
|
||||
if (error?.code !== 'ERR_REQUIRE_ESM') {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return import(pathToFileURL(packagePath).href)
|
||||
}
|
||||
|
||||
function resolveDefault(module) {
|
||||
return module?.default ?? module
|
||||
}
|
||||
|
||||
export async function convertPDFToImage(pdfInput, options = {}) {
|
||||
const [{ pdf }, sharp] = await Promise.all([
|
||||
const [pdfToImgModule, sharpModule] = await Promise.all([
|
||||
importPackage('pdf-to-img'),
|
||||
importPackage('sharp')
|
||||
])
|
||||
const { pdf } = pdfToImgModule
|
||||
const sharp = resolveDefault(sharpModule)
|
||||
|
||||
try {
|
||||
const { width, height, page_numbers, scale = 2, ...pdfOnlyOptions } =
|
||||
@ -43,7 +62,7 @@ export async function convertPDFToImage(pdfInput, options = {}) {
|
||||
const resizeOptions = {}
|
||||
if (width) resizeOptions.width = width
|
||||
if (height) resizeOptions.height = height
|
||||
image = await sharp.default(image).resize(resizeOptions).toBuffer()
|
||||
image = await sharp(image).resize(resizeOptions).toBuffer()
|
||||
}
|
||||
|
||||
outputImages.push(image)
|
||||
@ -56,7 +75,7 @@ export async function convertPDFToImage(pdfInput, options = {}) {
|
||||
const resizeOptions = {}
|
||||
if (width) resizeOptions.width = width
|
||||
if (height) resizeOptions.height = height
|
||||
processedImage = await sharp.default(image)
|
||||
processedImage = await sharp(image)
|
||||
.resize(resizeOptions)
|
||||
.toBuffer()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user