Skip to content

Commit

Permalink
Merge pull request #27 from bvanjoi/next
Browse files Browse the repository at this point in the history
release: 0.0.22
  • Loading branch information
bvanjoi authored Jul 14, 2022
2 parents 69265dc + 2c2fb52 commit cd647b0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 36 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
napi = "2"
napi-derive = "2"
nodejs-resolver = "0.0.30"
serde = { version = "1.0.138", features = ["derive"] }
napi = "2.6.3"
napi-derive = "2.6.0"
nodejs-resolver = "0.0.32"
serde = { version = "1.0.139", features = ["derive"] }

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

[profile.release]
lto = true
1 change: 1 addition & 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.

23 changes: 1 addition & 22 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,14 @@ test('alias options', (t) => {
t.is(result2, "false")
})

test('load sideeffects', (t) => {
test('load side effects', (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"))
})

test("shared cache speedy ensure", (t) => {
const sharedCache = factory.createExternalCache();
const resolver1 = factory.createWithExternalCache({}, sharedCache);
const resolver2 = factory.createWithExternalCache({}, sharedCache);

const uncachedStart = process.hrtime.bigint();
factory.loadSideEffects(resolver1, path.resolve(__dirname, "./fixture/node_modules/a"));
const uncachedEnd = process.hrtime.bigint();
const uncachedDuration = uncachedEnd - uncachedStart;

const cachedStart = process.hrtime.bigint();
factory.loadSideEffects(resolver2, path.resolve(__dirname, "./fixture/node_modules/a"));
const cachedEnd = process.hrtime.bigint();
const cachedDuration = cachedEnd - cachedStart;
console.log('uncached: ', uncachedDuration, 'cached: ', cachedDuration);
// maybe expose content in cache and ensure it is not empty may be a better choice.
// but I think the following statement will usefully.
t.is(cachedDuration - uncachedDuration < 0, true)
})


test("without cache", (t) => {
const resolver1 = factory.create({
browserField: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-resolver",
"version": "0.0.21",
"version": "0.0.22",
"description": "node binding for nodejs-resolver",
"main": "index.js",
"license": "MIT",
Expand Down
19 changes: 11 additions & 8 deletions 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, SideEffects};
use nodejs_resolver::{AliasMap, Resolver, ResolverOptions, ResolverCache, SideEffects};
use serde::Deserialize;
use std::{
path::{Path, PathBuf},
Expand Down Expand Up @@ -34,13 +34,16 @@ pub struct RawResolverOptions {
}

impl RawResolverOptions {
pub fn normalized(&self, unsafe_cache: Option<Arc<ResolverUnsafeCache>>) -> ResolverOptions {
pub fn normalized(&self, external_cache: Option<Arc<ResolverCache>>) -> ResolverOptions {
let default = ResolverOptions::default();
ResolverOptions {
enforce_extension: self.enforce_extension.to_owned(),
extensions: self.extensions.to_owned().unwrap_or(default.extensions),
alias: self.alias.to_owned().map_or(default.alias, parse_alias),
browser_field: self.browser_field.to_owned().unwrap_or(default.browser_field),
browser_field: self
.browser_field
.to_owned()
.unwrap_or(default.browser_field),
condition_names: self
.condition_names
.to_owned()
Expand All @@ -54,7 +57,7 @@ impl RawResolverOptions {
main_fields: self.main_fields.to_owned().unwrap_or(default.main_fields),
prefer_relative: self.prefer_relative.unwrap_or(default.prefer_relative),
tsconfig: self.tsconfig_path.to_owned().map(PathBuf::from),
unsafe_cache,
external_cache,
}
}
}
Expand Down Expand Up @@ -85,9 +88,9 @@ pub fn create(options: RawResolverOptions) -> Result<External<Resolver>, napi::E
pub struct ResolverCacheInternal {}

#[napi(ts_return_type = "ExternalObject<ResolverCacheInternal>")]
pub fn create_external_cache() -> Result<External<Arc<ResolverUnsafeCache>>, napi::Error> {
pub fn create_external_cache() -> Result<External<Arc<ResolverCache>>, napi::Error> {
Ok(External::new(
Resolver::new(Default::default()).unsafe_cache.unwrap(),
Arc::new(ResolverCache::default()),
))
}

Expand All @@ -97,7 +100,7 @@ pub fn create_external_cache() -> Result<External<Arc<ResolverUnsafeCache>>, nap
)]
pub fn create_with_external_cache(
options: RawResolverOptions,
external_cache: External<Arc<ResolverUnsafeCache>>,
external_cache: External<Arc<ResolverCache>>,
) -> Result<External<Resolver>, napi::Error> {
let external_cache = external_cache.as_ref().clone();
let options = options.normalized(Some(external_cache));
Expand Down Expand Up @@ -146,7 +149,7 @@ pub fn load_side_effects(
resolver: External<Resolver>,
path: String,
) -> Result<Option<SideEffectsStats>, napi::Error> {
match (*resolver).load_sideeffects(&Path::new(&path)) {
match (*resolver).load_side_effects(&Path::new(&path)) {
Ok(val) => Ok(val.map(|val| {
let (bool_val, array_val) = val
.1
Expand Down

0 comments on commit cd647b0

Please sign in to comment.