Skip to content

Commit

Permalink
fix: vercel/nft and webpack compatible issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Nov 11, 2021
1 parent 80de08a commit ffeaa0c
Show file tree
Hide file tree
Showing 4 changed files with 1,945 additions and 1,154 deletions.
20 changes: 6 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
const { platform, homedir } = require('os')
const { join } = require('path')

const { loadBinding } = require('@node-rs/helper')

/**
* __dirname means load native addon from current dir
* 'skia' means native addon name is `skia`
* the first arguments was decided by `napi.name` field in `package.json`
* the second arguments was decided by `name` field in `package.json`
* loadBinding helper will load `skia.[PLATFORM].node` from `__dirname` first
* If failed to load addon, it will fallback to load from `@napi-rs/skia-[PLATFORM]`
*/
const {
CanvasRenderingContext2D,
CanvasElement,
Expand All @@ -22,9 +12,9 @@ const {
CanvasPattern,
GlobalFonts,
convertSVGTextToPath: _convertSVGTextToPath,
} = loadBinding(__dirname, 'skia', '@napi-rs/canvas')
} = require('./js-binding')

const Geometry = require('./geometry')
const { DOMPoint, DOMMatrix, DOMRect } = require('./geometry')

const StrokeJoin = {
Miter: 0,
Expand Down Expand Up @@ -335,7 +325,7 @@ if (!process.env.DISABLE_SYSTEM_FONTS_LOAD) {
}

function convertSVGTextToPath(input) {
return _convertSVGTextToPath(Buffer.from(input), GlobalFontsSingleton)
return _convertSVGTextToPath(Buffer.isBuffer(input) ? input : Buffer.from(input), GlobalFontsSingleton)
}

module.exports = {
Expand All @@ -349,7 +339,9 @@ module.exports = {
StrokeCap,
StrokeJoin,
SvgExportFlag,
...Geometry,
GlobalFonts: GlobalFontsSingleton,
convertSVGTextToPath,
DOMPoint,
DOMMatrix,
DOMRect,
}
220 changes: 220 additions & 0 deletions js-binding.js
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
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"files": [
"index.d.ts",
"index.js",
"geometry.js"
"geometry.js",
"js-binding.js"
],
"napi": {
"name": "skia",
Expand Down Expand Up @@ -94,9 +95,6 @@
"skia-canvas": "^0.9.25",
"typescript": "^4.4.4"
},
"dependencies": {
"@node-rs/helper": "^1.2.1"
},
"lint-staged": {
"*.@(js|ts|tsx)": [
"prettier --write",
Expand Down
Loading

0 comments on commit ffeaa0c

Please sign in to comment.