Comparing version 1.3.0 to 1.4.0
@@ -1,6 +0,3 @@ | ||
import { FileSystemLike, fsRequire } from './types'; | ||
import { Options, FileSystemLike, fsRequire } from './types'; | ||
export type { FileSystemLike as FileSystem }; | ||
declare type Options = { | ||
fs?: boolean | FileSystemLike; | ||
}; | ||
export declare const createFsRequire: (mfs: FileSystemLike, options?: Options | undefined) => fsRequire; |
@@ -10,4 +10,4 @@ "use strict"; | ||
const module_1 = __importDefault(require("module")); | ||
const types_1 = require("./types"); | ||
const utils_1 = require("./utils"); | ||
const resolve_bare_specifier_1 = require("./utils/resolve-bare-specifier"); | ||
const resolve_module_1 = require("./utils/resolve-module"); | ||
const loaders = { | ||
@@ -34,29 +34,2 @@ '': undefined, | ||
}; | ||
function resolve(fs, filePath) { | ||
var _a, _b; | ||
// Exact match | ||
if (fs.existsSync(filePath)) { | ||
if ((0, utils_1.isDirectory)(fs, filePath)) { | ||
return (resolve(fs, path_1.default.join(filePath, 'index.js')) | ||
|| resolve(fs, path_1.default.join(filePath, 'index.json'))); | ||
} | ||
const extension = ((_b = (_a = filePath.match(utils_1.hasValidExtensionPattern)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : ''); | ||
return { | ||
extension, | ||
filePath, | ||
}; | ||
} | ||
// Try extensions | ||
for (const extension of types_1.implicitExtensions) { | ||
const filePathWithExtension = filePath + extension; | ||
if (fs.existsSync(filePathWithExtension)) { | ||
return { | ||
extension, | ||
filePath: filePathWithExtension, | ||
}; | ||
} | ||
} | ||
return null; | ||
} | ||
const realRequire = require; | ||
let idCounter = 0; | ||
@@ -68,28 +41,16 @@ const createFsRequire = (mfs, options) => { | ||
function makeRequireFunction(parentModule) { | ||
const resolve = (modulePath) => { | ||
if ((0, resolve_bare_specifier_1.isBareSpecifier)(modulePath)) { | ||
return modulePath; | ||
} | ||
const resolved = (0, resolve_module_1.resolveModule)(mfs, parentModule, modulePath); | ||
return resolved.filePath; | ||
}; | ||
const require = (modulePath) => { | ||
var _a, _b; | ||
if (!utils_1.isFilePathPattern.test(modulePath)) { | ||
const [moduleName, moduleSubpath] = (_a = (0, utils_1.getBareSpecifier)(modulePath)) !== null && _a !== void 0 ? _a : []; | ||
if (moduleName === 'fs') { | ||
const { fs } = options !== null && options !== void 0 ? options : {}; | ||
// If true, use native fs (can still be truthy) | ||
if (fs !== true) { | ||
const shimFs = fs || mfs; | ||
if (!moduleSubpath) { | ||
return shimFs; | ||
} | ||
if (moduleSubpath === '/promises' && ('promises' in shimFs)) { | ||
return shimFs.promises; | ||
} | ||
throw new Error(`Cannot find module '${modulePath}'`); | ||
} | ||
} | ||
return realRequire(modulePath); | ||
var _a; | ||
if ((0, resolve_bare_specifier_1.isBareSpecifier)(modulePath)) { | ||
return (0, resolve_bare_specifier_1.resolveBareSpecifier)(mfs, modulePath, options); | ||
} | ||
let filePath = path_1.default.resolve(path_1.default.dirname(parentModule.filename), modulePath); | ||
const resolvedPath = resolve(mfs, filePath); | ||
if (!resolvedPath) { | ||
throw new Error(`Cannot find module '${modulePath}'`); | ||
} | ||
filePath = resolvedPath.filePath; | ||
const resolvedPath = (0, resolve_module_1.resolveModule)(mfs, parentModule, modulePath); | ||
const { filePath } = resolvedPath; | ||
let importedModule = moduleCache[filePath]; | ||
@@ -100,3 +61,3 @@ if (!importedModule) { | ||
const sourceCode = mfs.readFileSync(filePath).toString(); | ||
(_b = loaders[resolvedPath.extension]) === null || _b === void 0 ? void 0 : _b.call(loaders, importedModule, sourceCode, makeRequireFunction, filePath, fsRequireId); | ||
(_a = loaders[resolvedPath.extension]) === null || _a === void 0 ? void 0 : _a.call(loaders, importedModule, sourceCode, makeRequireFunction, filePath, fsRequireId); | ||
moduleCache[filePath] = importedModule; | ||
@@ -107,2 +68,3 @@ } | ||
require.id = fsRequireId; | ||
require.resolve = resolve; | ||
require.cache = moduleCache; | ||
@@ -109,0 +71,0 @@ return require; |
/// <reference types="node" /> | ||
import Module from 'module'; | ||
export declare type Options = { | ||
fs?: boolean | FileSystemLike; | ||
}; | ||
export interface FileSystemLike { | ||
@@ -14,2 +17,3 @@ readFileSync: (path: string, options?: Record<string, unknown>) => string | Buffer; | ||
(modulePath: string): any; | ||
resolve: (modulePath: string) => string; | ||
id: number; | ||
@@ -16,0 +20,0 @@ cache: ModuleCache; |
{ | ||
"name": "fs-require", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Create a require() function from any file-system", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14100
13
202