Skip to content

Commit

Permalink
add napi module
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 6, 2023
1 parent ce96ee6 commit 5ab7554
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 0 deletions.
2 changes: 2 additions & 0 deletions napi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
*.node
23 changes: 23 additions & 0 deletions napi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "oxc_napi_resolver"
version = "0.0.0"
publish = false

[lints]
workspace = true

[lib]
crate-type = ["cdylib"]
test = false
doctest = false

[dependencies]
oxc_resolver = { workspace = true }
napi = { version = "2", features = ["serde-json", "async"] }
napi-derive = { version = "2" }

[build-dependencies]
napi-build = "2"

[package.metadata.cargo-machete]
ignored = ["napi"]
18 changes: 18 additions & 0 deletions napi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Installation

```bash
corepack enable
```

# Build

```bash
pnpm install
pnpm run build
```

# Test

```bash
pnpm test
```
3 changes: 3 additions & 0 deletions napi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
napi_build::setup();
}
14 changes: 14 additions & 0 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* tslint:disable */
/* eslint-disable */

/* auto-generated by NAPI-RS */

export interface ResolveResult {
path?: string
error?: string
}
export function sync(path: string, request: string): ResolveResult
export class ResolverFactory {
constructor()
sync(path: string, request: string): ResolveResult
}
258 changes: 258 additions & 0 deletions napi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */

/* auto-generated by NAPI-RS */

const { existsSync, readFileSync } = require('fs')
const { join } = require('path')

const { platform, arch } = process

let nativeBinding = null
let localFileExisted = false
let loadError = null

function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim();
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}

switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'resolver.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./resolver.android-arm64.node')
} else {
nativeBinding = require('@oxc-resolver/binding-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'resolver.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./resolver.android-arm-eabi.node')
} else {
nativeBinding = require('@oxc-resolver/binding-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'resolver.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.win32-x64-msvc.node')
} else {
nativeBinding = require('@oxc-resolver/binding-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'resolver.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.win32-ia32-msvc.node')
} else {
nativeBinding = require('@oxc-resolver/binding-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'resolver.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.win32-arm64-msvc.node')
} else {
nativeBinding = require('@oxc-resolver/binding-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'resolver.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./resolver.darwin-universal.node')
} else {
nativeBinding = require('@oxc-resolver/binding-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'resolver.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./resolver.darwin-x64.node')
} else {
nativeBinding = require('@oxc-resolver/binding-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'resolver.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.darwin-arm64.node')
} else {
nativeBinding = require('@oxc-resolver/binding-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, 'resolver.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./resolver.freebsd-x64.node')
} else {
nativeBinding = require('@oxc-resolver/binding-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'resolver.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.linux-x64-musl.node')
} else {
nativeBinding = require('@oxc-resolver/binding-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'resolver.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.linux-x64-gnu.node')
} else {
nativeBinding = require('@oxc-resolver/binding-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'resolver.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.linux-arm64-musl.node')
} else {
nativeBinding = require('@oxc-resolver/binding-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'resolver.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.linux-arm64-gnu.node')
} else {
nativeBinding = require('@oxc-resolver/binding-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
localFileExisted = existsSync(
join(__dirname, 'resolver.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resolver.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@oxc-resolver/binding-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 { ResolverFactory, sync } = nativeBinding

module.exports.ResolverFactory = ResolverFactory
module.exports.sync = sync
29 changes: 29 additions & 0 deletions napi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@oxc-resolver/binding",
"private": true,
"scripts": {
"build": "napi build --platform --release",
"test": "node test.mjs"
},
"devDependencies": {
"@napi-rs/cli": "^2.15.2"
},
"engines": {
"node": ">=14.*"
},
"packageManager": "pnpm@8.2.0",
"napi": {
"name": "resolver",
"triples": {
"defaults": false,
"additional": [
"x86_64-pc-windows-msvc",
"aarch64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
"x86_64-apple-darwin",
"aarch64-apple-darwin"
]
}
}
}
14 changes: 14 additions & 0 deletions napi/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ab7554

Please sign in to comment.