-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:privatenumber/fs-require
- Loading branch information
Showing
8 changed files
with
271 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { FileSystemLike, implicitExtensions } from '../types'; | ||
|
||
export const hasValidExtensionPattern = new RegExp( | ||
`(${ | ||
implicitExtensions | ||
.map(extension => extension.replace(/\./g, '\\$&')) | ||
.join('|') | ||
})$`, | ||
); | ||
|
||
export const isDirectory = ( | ||
fs: FileSystemLike, | ||
directoryPath: string, | ||
) => fs.lstatSync(directoryPath).isDirectory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Options, FileSystemLike } from '../types'; | ||
|
||
const isFilePathPattern = /^[./]/; | ||
export const isBareSpecifier = (modulePath: string) => !isFilePathPattern.test(modulePath); | ||
|
||
const specifierPattern = /^(?:node:)?((?:@[\da-z][\w.-]+\/)?[\da-z][\w.-]+)(\/.+)?$/; | ||
const parseBareSpecifier = ( | ||
modulePath: string, | ||
) => modulePath.match(specifierPattern)?.slice(1, 3); | ||
|
||
const realRequire = require; | ||
|
||
export function resolveBareSpecifier( | ||
mfs: FileSystemLike, | ||
modulePath: string, | ||
options?: Options, | ||
) { | ||
const [moduleName, moduleSubpath] = parseBareSpecifier(modulePath) ?? []; | ||
|
||
if (moduleName === 'fs') { | ||
const { fs } = 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import path from 'path'; | ||
import Module from 'module'; | ||
import { | ||
FileSystemLike, | ||
implicitExtensions, | ||
loaderTypes, | ||
} from '../types'; | ||
import { | ||
hasValidExtensionPattern, | ||
isDirectory, | ||
} from '.'; | ||
|
||
type Resolved = { | ||
extension: (typeof loaderTypes)[number]; | ||
filePath: string; | ||
} | ||
|
||
function resolveModuleSafe( | ||
mfs: FileSystemLike, | ||
parentModule: Module, | ||
modulePath: string, | ||
): Resolved | null { | ||
// Absolute path | ||
modulePath = path.resolve(path.dirname(parentModule.filename), modulePath); | ||
|
||
// Exact match | ||
if (mfs.existsSync(modulePath)) { | ||
if (isDirectory(mfs, modulePath)) { | ||
return ( | ||
resolveModuleSafe(mfs, parentModule, path.join(modulePath, 'index.js')) | ||
|| resolveModuleSafe(mfs, parentModule, path.join(modulePath, 'index.json')) | ||
); | ||
} | ||
|
||
const extension = (modulePath.match(hasValidExtensionPattern)?.[0] ?? '') as Resolved['extension']; | ||
return { | ||
extension, | ||
filePath: modulePath, | ||
}; | ||
} | ||
|
||
// Try extensions | ||
for (const extension of implicitExtensions) { | ||
const filePathWithExtension = modulePath + extension; | ||
if (mfs.existsSync(filePathWithExtension)) { | ||
return { | ||
extension, | ||
filePath: filePathWithExtension, | ||
}; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function resolveModule( | ||
mfs: FileSystemLike, | ||
parentModule: Module, | ||
modulePath: string, | ||
) { | ||
const resolved = resolveModuleSafe(mfs, parentModule, modulePath); | ||
|
||
if (!resolved) { | ||
throw new Error(`Cannot find module '${modulePath}'`); | ||
} | ||
|
||
return resolved; | ||
} |
Oops, something went wrong.