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 f2e23f5
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 109 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
28 changes: 13 additions & 15 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 @@ -66,37 +67,34 @@
"@jimp/custom": "^0.16.1",
"@jimp/jpeg": "^0.16.1",
"@jimp/png": "^0.16.1",
"@napi-rs/cli": "^1.3.3",
"@napi-rs/cli": "^1.3.5",
"@octokit/rest": "^18.12.0",
"@swc-node/register": "^1.3.6",
"@types/lodash": "^4.14.175",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@swc-node/register": "^1.4.0",
"@types/lodash": "^4.14.176",
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
"ava": "^3.15.0",
"benny": "^3.7.0",
"benny": "^3.7.1",
"canvas": "^2.8.0",
"canvaskit-wasm": "^0.30.0",
"chalk": "^4.1.2",
"conventional-changelog-cli": "^2.1.1",
"eslint": "^8.0.1",
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-sonarjs": "^0.10.0",
"husky": "^7.0.2",
"lint-staged": "^11.2.3",
"husky": "^7.0.4",
"lint-staged": "^11.2.6",
"lodash": "^4.17.21",
"npm-run-all": "^4.1.5",
"pinst": "^2.1.6",
"png.js": "^0.2.1",
"prettier": "^2.4.1",
"putasset": "^5.0.3",
"skia-canvas": "^0.9.25",
"skia-canvas": "^0.9.27",
"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 f2e23f5

Please sign in to comment.