generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
vercel/nft
and webpack
compatible issue
- Loading branch information
1 parent
80de08a
commit f2e23f5
Showing
4 changed files
with
317 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
const { existsSync, readFileSync } = require('fs') | ||
const { join } = require('path') | ||
|
||
const { platform, arch } = process | ||
|
||
let nativeBinding = null | ||
let localFileExisted = false | ||
let isMusl = false | ||
let loadError = null | ||
|
||
switch (platform) { | ||
case 'android': | ||
if (arch !== 'arm64') { | ||
throw new Error(`Unsupported architecture on Android ${arch}`) | ||
} | ||
localFileExisted = existsSync(join(__dirname, 'skia.android-arm64.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.android-arm64.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-android-arm64') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
case 'win32': | ||
// eslint-disable-next-line sonarjs/no-nested-switch | ||
switch (arch) { | ||
case 'x64': | ||
localFileExisted = existsSync(join(__dirname, 'skia.win32-x64-msvc.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.win32-x64-msvc.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-win32-x64-msvc') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
case 'ia32': | ||
localFileExisted = existsSync(join(__dirname, 'skia.win32-ia32-msvc.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.win32-ia32-msvc.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-win32-ia32-msvc') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
case 'arm64': | ||
localFileExisted = existsSync(join(__dirname, 'skia.win32-arm64-msvc.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.win32-arm64-msvc.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-win32-arm64-msvc') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
default: | ||
throw new Error(`Unsupported architecture on Windows: ${arch}`) | ||
} | ||
break | ||
case 'darwin': | ||
// eslint-disable-next-line sonarjs/no-nested-switch | ||
switch (arch) { | ||
case 'x64': | ||
localFileExisted = existsSync(join(__dirname, 'skia.darwin-x64.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.darwin-x64.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-darwin-x64') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
case 'arm64': | ||
localFileExisted = existsSync(join(__dirname, 'skia.darwin-arm64.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.darwin-arm64.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-darwin-arm64') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
default: | ||
throw new Error(`Unsupported architecture on macOS: ${arch}`) | ||
} | ||
break | ||
case 'freebsd': | ||
if (arch !== 'x64') { | ||
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`) | ||
} | ||
localFileExisted = existsSync(join(__dirname, 'skia.freebsd-x64.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.freebsd-x64.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-freebsd-x64') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
case 'linux': | ||
// eslint-disable-next-line sonarjs/no-nested-switch | ||
switch (arch) { | ||
case 'x64': | ||
isMusl = readFileSync('/usr/bin/ldd', 'utf8').includes('musl') | ||
if (isMusl) { | ||
localFileExisted = existsSync(join(__dirname, 'skia.linux-x64-musl.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.linux-x64-musl.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-linux-x64-musl') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
} else { | ||
localFileExisted = existsSync(join(__dirname, 'skia.linux-x64-gnu.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.linux-x64-gnu.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-linux-x64-gnu') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
} | ||
break | ||
case 'arm64': | ||
isMusl = readFileSync('/usr/bin/ldd', 'utf8').includes('musl') | ||
if (isMusl) { | ||
localFileExisted = existsSync(join(__dirname, 'skia.linux-arm64-musl.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.linux-arm64-musl.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-linux-arm64-musl') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
} else { | ||
localFileExisted = existsSync(join(__dirname, 'skia.linux-arm64-gnu.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.linux-arm64-gnu.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-linux-arm64-gnu') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
} | ||
break | ||
case 'arm': | ||
localFileExisted = existsSync(join(__dirname, 'skia.linux-arm-gnueabihf.node')) | ||
try { | ||
if (localFileExisted) { | ||
nativeBinding = require('./skia.linux-arm-gnueabihf.node') | ||
} else { | ||
nativeBinding = require('@napi-rs/canvas-linux-arm-gnueabihf') | ||
} | ||
} catch (e) { | ||
loadError = e | ||
} | ||
break | ||
default: | ||
throw new Error(`Unsupported architecture on Linux: ${arch}`) | ||
} | ||
break | ||
default: | ||
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) | ||
} | ||
|
||
if (!nativeBinding) { | ||
if (loadError) { | ||
throw loadError | ||
} | ||
throw new Error(`Failed to load native binding`) | ||
} | ||
|
||
const { | ||
CanvasRenderingContext2D, | ||
CanvasElement, | ||
createContext, | ||
SVGCanvas, | ||
Path2D, | ||
ImageData, | ||
Image, | ||
CanvasPattern, | ||
GlobalFonts, | ||
convertSVGTextToPath, | ||
} = nativeBinding | ||
|
||
module.exports.CanvasRenderingContext2D = CanvasRenderingContext2D | ||
module.exports.CanvasElement = CanvasElement | ||
module.exports.createContext = createContext | ||
module.exports.SVGCanvas = SVGCanvas | ||
module.exports.Path2D = Path2D | ||
module.exports.ImageData = ImageData | ||
module.exports.Image = Image | ||
module.exports.CanvasPattern = CanvasPattern | ||
module.exports.GlobalFonts = GlobalFonts | ||
module.exports.convertSVGTextToPath = convertSVGTextToPath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.