Skip to content

Commit

Permalink
Merge pull request #31 from iambumblehead/re-30-handle-windows-style-…
Browse files Browse the repository at this point in the history
…module-paths

handle windows style module path
  • Loading branch information
iambumblehead authored Aug 17, 2022
2 parents 665380d + cf2a8c2 commit 7624f9e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 0.8.8 _Aug.16.2022_
* support win32 [drive-style module-path](https://github.com/iambumblehead/resolvewithplus/pull/31)
* 0.8.7 _Aug.15.2022_
* support core modules [w/ node: prefix](https://github.com/iambumblehead/resolvewithplus/pull/27), credit @gmahomarf
* 0.8.6 _Aug.01.2022_
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": "resolvewithplus",
"version": "0.8.7",
"version": "0.8.8",
"engines" : {
"node" : ">=12.16.0"
},
Expand Down
13 changes: 11 additions & 2 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ const require = module.createRequire(import.meta.url);
const realpath = fs.realpathSync.native;
const isBuiltinRe = new RegExp(
'^(?:node:)?('+module.builtinModules.join('|').replace('/', '\/')+')$');
const isDirPathRe = /^\.?\.?(\/|\\)/;
const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/;
const isRelPathRe = /^.\.?(?=\/|\\)/;
const isWin32PathRe = /\\/g;
const isWin32DriveRe = /^[a-zA-Z]:/;
const isSupportedIndexRe = /index.[tj]sx?$/;
const isResolveWithPathRe = /[\\/]resolvewithplus[\\/]/;
const supportedExtensions = [ '.js', '.mjs', '.ts', '.tsx', '.json', '.node' ];
const node_modules = 'node_modules';
const packagejson = 'package.json';


export default (o => {
o = (requirepath, withpath, opts) => {
let resolvedpath = o.cache[requirepath+withpath];
Expand All @@ -28,6 +31,12 @@ export default (o => {
};

o.cache = {};

// ex, D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js
// -> /a/resolvewithplus/pathto/testfiles/testscript.js
o.pathToPosix = pathany => isWin32PathRe.test(pathany)
? pathany.replace(isWin32DriveRe, '').replace(isWin32PathRe, path.posix.sep)
: pathany

// https://nodejs.org/api/modules.html#modules_module_require_id
//
Expand All @@ -51,7 +60,7 @@ export default (o => {
fullpath = requirepath;
} else {
fullpath = isDirPathRe.test(requirepath)
? o.getasfileordir(requirepath, withpath, opts)
? o.getasfileordir(o.pathToPosix(requirepath), withpath, opts)
: o.getasnode_module(requirepath, withpath);

fullpath = fullpath && realpath(fullpath);
Expand Down
36 changes: 35 additions & 1 deletion tests/tests-basic/tests-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,46 @@
// Timestamp: 2017.04.23-23:31:33 (last modified)
// Author(s): bumblehead <[email protected]>

import url from 'url';
import url, { fileURLToPath } from 'url';
import os from 'os';
import path from 'path';
import test from 'node:test'
import assert from 'node:assert/strict'
import resolvewithplus from '../../resolvewithplus.js';

test('should convert win32 path to node-friendly posix path', () => {
const win32Path = 'D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js';
const posixPath = '/a/resolvewithplus/pathto/testfiles/testscript.js';
const returnPath = resolvewithplus.pathToPosix(win32Path);

assert.strictEqual(returnPath, posixPath);
})

test('should pass windows and posix system-specific module path', () => {
const modulePath = fileURLToPath(
new URL('../testfiles/testscript.js', import.meta.url))
const calleePath = import.meta.url;
const returnPath = resolvewithplus(modulePath, calleePath)
// posix modulePath
// /root/pathto/testfiles/testscript.js
// posix calleePath
// file:///root/pathto/tests-basic/tests-basic.test.js
// posix returnPath
// /root/pathto/testfiles/testscript.js
//
// win32 modulePath
// D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js
// win32 calleePath eslint-disable-next-line max-len
// file:///D:/a/resolvewithplus/pathto/tests-basic/tests-basic.test.js
// returnPath
// D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js
assert.ok(typeof returnPath === 'string')
assert.ok(returnPath.endsWith(
os.platform() === 'win32'
? '\\tests\\testfiles\\testscript.js'
: '/tests/testfiles/testscript.js'))
});

test('should return a core module reference as require.resolve id', () => {
assert.strictEqual(resolvewithplus('path'), 'path');
});
Expand Down

0 comments on commit 7624f9e

Please sign in to comment.