Skip to content

Commit

Permalink
Merge pull request #24 from bvanjoi/next
Browse files Browse the repository at this point in the history
release: 0.0.19
  • Loading branch information
bvanjoi authored Jul 11, 2022
2 parents d084933 + 18ff6d9 commit 57e03bd
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["cdylib"]
[dependencies]
napi = "2"
napi-derive = "2"
nodejs-resolver = "0.0.27"
nodejs-resolver = "0.0.28"
serde = { version = "1.0.138", features = ["derive"] }

[build-dependencies]
Expand Down
1 change: 1 addition & 0 deletions __test__/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
3 changes: 3 additions & 0 deletions __test__/fixture/node_modules/a/package.json

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

11 changes: 8 additions & 3 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import path from 'path'

import test from 'ava'

import factory, { RawResolverOptions } from '../index'

test('sync function from native code', (t) => {
Expand Down Expand Up @@ -38,7 +36,6 @@ test('extensions options', (t) => {
t.is(result2, path.resolve(__dirname, './fixture/lib.ts?query#fragment'))
})


test('alias options', (t) => {
const resolverOptions: RawResolverOptions = {
alias: [
Expand All @@ -59,3 +56,11 @@ test('alias options', (t) => {
const result2 = factory.resolve(resolver, __dirname, '@false/lib')
t.is(result2, "false")
})

test('load sideeffects', (t) => {
const resolver = factory.create({})
const result = factory.loadSideEffects(resolver, path.resolve(__dirname, "./fixture/node_modules/a"));
t.is(result?.boolVal, false)
t.is(result?.arrayVal, undefined)
t.is(result?.pkgFilePath, path.resolve(__dirname, "./fixture/node_modules/a/package.json"))
})
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ export interface ResolverInternal {
export function create(options: RawResolverOptions): ExternalObject<ResolverInternal>
export function createResolverAndInheritUnsafeCacheFromAnother(options: RawResolverOptions, another: ExternalObject<Resolver>): ExternalObject<ResolverInternal>
export function resolve(resolver: ExternalObject<ResolverInternal>, base_dir: string, id: string): string
export interface SideEffectsStats {
boolVal?: boolean
arrayVal?: Array<string>
pkgFilePath: string
}
export function loadSideEffects(resolver: ExternalObject<ResolverInternal>, path: string): {boolVal?: boolean, arrayVal?: string[], pkgFilePath: string} | undefined
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { create, createResolverAndInheritUnsafeCacheFromAnother, resolve } = nativeBinding
const { create, createResolverAndInheritUnsafeCacheFromAnother, resolve, loadSideEffects } = nativeBinding

module.exports.create = create
module.exports.createResolverAndInheritUnsafeCacheFromAnother = createResolverAndInheritUnsafeCacheFromAnother
module.exports.resolve = resolve
module.exports.loadSideEffects = loadSideEffects
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-resolver",
"version": "0.0.18",
"version": "0.0.19",
"description": "node binding for nodejs-resolver",
"main": "index.js",
"license": "MIT",
Expand Down Expand Up @@ -52,6 +52,7 @@
"devDependencies": {
"@napi-rs/cli": "^2.9.0",
"@swc-node/register": "^1.5.1",
"@types/node": "14.14.31",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"ava": "^4.3.0",
Expand Down
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use napi::bindgen_prelude::External;
use napi_derive::napi;
use nodejs_resolver::{AliasMap, Resolver, ResolverOptions, ResolverUnsafeCache};
use nodejs_resolver::{AliasMap, Resolver, ResolverOptions, ResolverUnsafeCache, SideEffects};
use serde::Deserialize;
use std::{
path::{Path, PathBuf},
Expand Down Expand Up @@ -116,3 +116,34 @@ pub fn resolve(
Err(err) => Err(napi::Error::new(napi::Status::GenericFailure, err)),
}
}

#[napi(object)]
pub struct SideEffectsStats {
pub bool_val: Option<bool>,
pub array_val: Option<Vec<String>>,
pub pkg_file_path: String,
}

#[napi(
ts_args_type = "resolver: ExternalObject<ResolverInternal>, path: string",
ts_return_type = "{boolVal?: boolean, arrayVal?: string[], pkgFilePath: string} | undefined"
)]
pub fn load_side_effects(
resolver: External<Resolver>,
path: String,
) -> Result<Option<SideEffectsStats>, napi::Error> {
match (*resolver).load_sideeffects(&Path::new(&path)) {
Ok(val) => Ok(val.map(|val| {
let (bool_val, array_val) = val.1.map(|side_effects| match side_effects {
SideEffects::Bool(bool) => (Some(bool), None),
SideEffects::Array(array) => (None, Some(array)),
}).unwrap_or((None, None));
SideEffectsStats {
pkg_file_path: val.0.display().to_string(),
bool_val,
array_val,
}
})),
Err(err) => Err(napi::Error::new(napi::Status::GenericFailure, err)),
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:14.14.31":
version: 14.14.31
resolution: "@types/node@npm:14.14.31"
checksum: 5b9ab3660ee63abc57affc836c0d50b2941a862c5b6c241c02762d8e1ad610f6b0d350d7426218dc401b5abc7d6294ee1b475da7c331e486d59a5608d1a0b777
languageName: node
linkType: hard

"@typescript-eslint/eslint-plugin@npm:^5.27.1":
version: 5.27.1
resolution: "@typescript-eslint/eslint-plugin@npm:5.27.1"
Expand Down Expand Up @@ -2992,6 +2999,7 @@ __metadata:
dependencies:
"@napi-rs/cli": ^2.9.0
"@swc-node/register": ^1.5.1
"@types/node": 14.14.31
"@typescript-eslint/eslint-plugin": ^5.27.1
"@typescript-eslint/parser": ^5.27.1
ava: ^4.3.0
Expand Down

0 comments on commit 57e03bd

Please sign in to comment.