Skip to content

Commit

Permalink
fix(helper): accept require from caller to solve yarn2 pnp issue
Browse files Browse the repository at this point in the history
Close #263
  • Loading branch information
Brooooooklyn committed Dec 11, 2020
1 parent d6afd95 commit 8fed489
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/bcrypt/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { loadBinding } = require('@node-rs/helper')

const binding = loadBinding(__dirname, 'bcrypt', '@node-rs/bcrypt')
const binding = loadBinding(require, __dirname, 'bcrypt', '@node-rs/bcrypt')

const DEFAULT_COST = 12

Expand Down
2 changes: 1 addition & 1 deletion packages/crc32/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { loadBinding } = require('@node-rs/helper')

const binding = loadBinding(__dirname, 'crc32', '@node-rs/crc32')
const binding = loadBinding(require, __dirname, 'crc32', '@node-rs/crc32')

module.exports = {
crc32: function crc32(input, crc = 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/deno-lint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { platform } = require('os')

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

const binding = loadBinding(__dirname, 'deno-lint', '@node-rs/deno-lint')
const binding = loadBinding(require, __dirname, 'deno-lint', '@node-rs/deno-lint')

module.exports = {
binding,
Expand Down
16 changes: 11 additions & 5 deletions packages/helper/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ import { join } from 'path'

import { platformArchTriples } from '@napi-rs/triples'

const ArchName = arch()
const PlatformName = platform()
/**
* @param requireFn NodeJS require function, you must pass your `require` in because `yarn pnp` need it, see https://github.com/napi-rs/node-rs/issues/263 for detail
* @param dirname The path to try to load native binding while developing in local
* @param filename napi.name field in your package.json
* @param packageName your package name, `@node-rs/helper` will try to load native package by using this name concat with `triple.platform-arch-abi`
*/
export function loadBinding(requireFn: typeof require, dirname: string, filename = 'index', packageName?: string) {
const ArchName = arch()
const PlatformName = platform()

export function loadBinding(dirname: string, filename = 'index', packageName?: string) {
const triples = platformArchTriples[PlatformName][ArchName]
for (const triple of triples) {
// resolve in node_modules
if (packageName) {
try {
return require(`${packageName}-${triple.platformArchABI}`)
return requireFn(`${packageName}-${triple.platformArchABI}`)
// eslint-disable-next-line no-empty
} catch (e) {}
}
const localFilePath = join(dirname, `${filename}.${triple.platformArchABI}.node`)
if (existsSync(localFilePath)) {
return require(localFilePath)
return requireFn(localFilePath)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jieba/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { loadBinding } = require('@node-rs/helper')

const native = loadBinding(__dirname, 'jieba', '@node-rs/jieba')
const native = loadBinding(require, __dirname, 'jieba', '@node-rs/jieba')

module.exports = {
...native,
Expand Down

0 comments on commit 8fed489

Please sign in to comment.