From 18ff6d9f3aafd990d7fdf4ee79c21c2bfefb03b2 Mon Sep 17 00:00:00 2001 From: bvanjoi Date: Mon, 11 Jul 2022 20:11:58 +0800 Subject: [PATCH] release: 0.0.19 --- Cargo.toml | 2 +- __test__/.gitignore | 1 + __test__/fixture/node_modules/a/package.json | 3 ++ __test__/index.spec.ts | 11 +++++-- index.d.ts | 6 ++++ index.js | 3 +- package.json | 3 +- src/lib.rs | 33 +++++++++++++++++++- yarn.lock | 8 +++++ 9 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 __test__/.gitignore create mode 100644 __test__/fixture/node_modules/a/package.json diff --git a/Cargo.toml b/Cargo.toml index a6cd449..b3339ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/__test__/.gitignore b/__test__/.gitignore new file mode 100644 index 0000000..cf4bab9 --- /dev/null +++ b/__test__/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/__test__/fixture/node_modules/a/package.json b/__test__/fixture/node_modules/a/package.json new file mode 100644 index 0000000..a438291 --- /dev/null +++ b/__test__/fixture/node_modules/a/package.json @@ -0,0 +1,3 @@ +{ + "sideEffects": false +} diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 98375a0..fc0174f 100644 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -1,7 +1,5 @@ import path from 'path' - import test from 'ava' - import factory, { RawResolverOptions } from '../index' test('sync function from native code', (t) => { @@ -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: [ @@ -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")) +}) diff --git a/index.d.ts b/index.d.ts index d8975a0..d8f7970 100644 --- a/index.d.ts +++ b/index.d.ts @@ -33,3 +33,9 @@ export interface ResolverInternal { export function create(options: RawResolverOptions): ExternalObject export function createResolverAndInheritUnsafeCacheFromAnother(options: RawResolverOptions, another: ExternalObject): ExternalObject export function resolve(resolver: ExternalObject, base_dir: string, id: string): string +export interface SideEffectsStats { + boolVal?: boolean + arrayVal?: Array + pkgFilePath: string +} +export function loadSideEffects(resolver: ExternalObject, path: string): {boolVal?: boolean, arrayVal?: string[], pkgFilePath: string} | undefined diff --git a/index.js b/index.js index 9e371a0..906fe04 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index 9a12aae..159144a 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/lib.rs b/src/lib.rs index 0818dfe..fb74fc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}, @@ -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, + pub array_val: Option>, + pub pkg_file_path: String, +} + +#[napi( + ts_args_type = "resolver: ExternalObject, path: string", + ts_return_type = "{boolVal?: boolean, arrayVal?: string[], pkgFilePath: string} | undefined" +)] +pub fn load_side_effects( + resolver: External, + path: String, +) -> Result, 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)), + } +} diff --git a/yarn.lock b/yarn.lock index 5bea5d4..edbc2d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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