Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

"use strict";

const fs = require("graceful-fs");
const CachedInputFileSystem = require("./CachedInputFileSystem");
const ResolverFactory = require("./ResolverFactory");
const memoize = require("./util/memoize");

/** @typedef {import("./CachedInputFileSystem").BaseFileSystem} BaseFileSystem */
/** @typedef {import("./PnpPlugin").PnpApiImpl} PnpApi */
Expand Down Expand Up @@ -36,17 +34,28 @@ const ResolverFactory = require("./ResolverFactory");
* }} ResolveFunction
*/

const nodeFileSystem = new CachedInputFileSystem(fs, 4000);
const getCachedFileSystem = memoize(() => require("./CachedInputFileSystem"));

const nodeContext = {
environments: ["node+es3+es5+process+native"],
};
const getNodeFileSystem = memoize(() => {
const fs = require("graceful-fs");

const CachedInputFileSystem = getCachedFileSystem();

const asyncResolver = ResolverFactory.createResolver({
conditionNames: ["node"],
extensions: [".js", ".json", ".node"],
fileSystem: nodeFileSystem,
return new CachedInputFileSystem(fs, 4000);
});
const getNodeContext = memoize(() => ({
environments: ["node+es3+es5+process+native"],
}));

const getResolverFactory = memoize(() => require("./ResolverFactory"));

const getAsyncResolver = memoize(() =>
getResolverFactory().createResolver({
conditionNames: ["node"],
extensions: [".js", ".json", ".node"],
fileSystem: getNodeFileSystem(),
}),
);

/**
* @type {ResolveFunctionAsync}
Expand All @@ -65,12 +74,12 @@ const resolve =
resolveContext = /** @type {ResolveContext} */ (request);
request = path;
path = context;
context = nodeContext;
context = getNodeContext();
}
if (typeof callback !== "function") {
callback = /** @type {ResolveCallback} */ (resolveContext);
}
asyncResolver.resolve(
getAsyncResolver().resolve(
context,
path,
/** @type {string} */ (request),
Expand All @@ -79,12 +88,14 @@ const resolve =
);
};

const syncResolver = ResolverFactory.createResolver({
conditionNames: ["node"],
extensions: [".js", ".json", ".node"],
useSyncFileSystemCalls: true,
fileSystem: nodeFileSystem,
});
const getSyncResolver = memoize(() =>
getResolverFactory().createResolver({
conditionNames: ["node"],
extensions: [".js", ".json", ".node"],
useSyncFileSystemCalls: true,
fileSystem: getNodeFileSystem(),
}),
);

/**
* @type {ResolveFunction}
Expand All @@ -100,9 +111,9 @@ const resolveSync =
if (typeof context === "string") {
request = path;
path = context;
context = nodeContext;
context = getNodeContext();
}
return syncResolver.resolveSync(
return getSyncResolver().resolveSync(
context,
path,
/** @type {string} */ (request),
Expand All @@ -116,8 +127,8 @@ const resolveSync =
* @returns {ResolveFunctionAsync} Resolver function
*/
function create(options) {
const resolver = ResolverFactory.createResolver({
fileSystem: nodeFileSystem,
const resolver = getResolverFactory().createResolver({
fileSystem: getNodeFileSystem(),
...options,
});
/**
Expand All @@ -133,7 +144,7 @@ function create(options) {
resolveContext = /** @type {ResolveContext} */ (request);
request = path;
path = context;
context = nodeContext;
context = getNodeContext();
}
if (typeof callback !== "function") {
callback = /** @type {ResolveCallback} */ (resolveContext);
Expand All @@ -153,9 +164,9 @@ function create(options) {
* @returns {ResolveFunction} Resolver function
*/
function createSync(options) {
const resolver = ResolverFactory.createResolver({
const resolver = getResolverFactory().createResolver({
useSyncFileSystemCalls: true,
fileSystem: nodeFileSystem,
fileSystem: getNodeFileSystem(),
...options,
});
/**
Expand All @@ -168,7 +179,7 @@ function createSync(options) {
if (typeof context === "string") {
request = path;
path = context;
context = nodeContext;
context = getNodeContext();
}
return resolver.resolveSync(context, path, /** @type {string} */ (request));
};
Expand Down Expand Up @@ -196,8 +207,12 @@ module.exports = mergeExports(resolve, {
return createSync;
},
}),
ResolverFactory,
CachedInputFileSystem,
get ResolverFactory() {
return getResolverFactory();
},
get CachedInputFileSystem() {
return getCachedFileSystem();
},
get CloneBasenamePlugin() {
return require("./CloneBasenamePlugin");
},
Expand Down
37 changes: 37 additions & 0 deletions lib/util/memoize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

"use strict";

/**
* @template T
* @typedef {() => T} FunctionReturning
*/

/**
* @template T
* @param {FunctionReturning<T>} fn memorized function
* @returns {FunctionReturning<T>} new function
*/
const memoize = (fn) => {
let cache = false;
/** @type {T | undefined} */
let result;
return () => {
if (cache) {
return /** @type {T} */ (result);
}

result = fn();
cache = true;
// Allow to clean up memory for fn
// and all dependent resources
/** @type {FunctionReturning<T> | undefined} */
(fn) = undefined;
return /** @type {T} */ (result);
};
};

module.exports = memoize;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"prepare": "husky install",
"lint": "yarn lint:code && yarn lint:types && yarn lint:types-test && yarn lint:special && yarn fmt:check && yarn lint:spellcheck",
"lint:code": "eslint --cache .",
"lint:special": "node node_modules/tooling/lockfile-lint && node node_modules/tooling/inherit-types && node node_modules/tooling/format-file-header && node node_modules/tooling/generate-types",
"lint:special": "node node_modules/tooling/lockfile-lint && node node_modules/tooling/inherit-types && node node_modules/tooling/generate-types",
"lint:types": "tsc",
"lint:types-test": "tsc -p tsconfig.types.test.json",
"lint:spellcheck": "cspell --no-must-find-files \"**/*.*\"",
Expand All @@ -63,7 +63,7 @@
"fmt:base": "node_modules/prettier/bin/prettier.cjs --cache --ignore-unknown .",
"fix": "yarn fix:code && yarn fix:special",
"fix:code": "yarn lint:code --fix",
"fix:special": "node node_modules/tooling/inherit-types --write && node node_modules/tooling/format-file-header --write && node node_modules/tooling/generate-types --write",
"fix:special": "node node_modules/tooling/inherit-types --write && node node_modules/tooling/generate-types --write",
"type-report": "rimraf coverage && yarn cover:types && yarn cover:report && open-cli coverage/lcov-report/index.html",
"pretest": "yarn lint",
"test": "yarn test:coverage",
Expand Down
Loading