diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f13a1b..1425927 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 1.0.2 _Sep.24.2022_ + * add test and changes to [support import.meta.resolve](https://github.com/iambumblehead/resolvewithplus/pull/39) * 1.0.1 _Sep.15.2022_ * reduce size of minified resolvewithplus.js file [by 50%](https://github.com/iambumblehead/resolvewithplus/pull/38) * 1.0.0 _Aug.28.2022_ diff --git a/package.json b/package.json index ee59885..46395f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resolvewithplus", - "version": "1.0.1", + "version": "1.0.2", "engines" : { "node" : ">=12.16.0" }, diff --git a/resolvewithplus.js b/resolvewithplus.js index 0eab172..d178e05 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -1,4 +1,5 @@ import fs from 'fs' +import url from 'url' import path from 'path' import module from 'module' @@ -20,6 +21,7 @@ const esmStrGlobRe = /(\*)/g const esmStrPathCharRe = /([./])/g const rootDirSlashRe = /^\// const protocolNode = /^node:/ +const protocolFile = /^file:/ const supportedExtensions = [ '.js', '.mjs', '.ts', '.tsx', '.json', '.node' ] const node_modules = 'node_modules' const packagejson = 'package.json' @@ -33,7 +35,9 @@ const cache = {} const addprotocolnode = p => protocolNode.test(p) ? p : `node:${p}` const addprotocolfile = p => p && ('file:///' + p.replace(rootDirSlashRe, '')) const iscoremodule = p => isBuiltinRe.test(p) -const getasdirname = p => path.resolve(path.extname(p) ? path.dirname(p) : p) +const getaspath = p => protocolFile.test(p) ? url.fileURLToPath(p) : p +const getasdirname = p => + path.resolve(path.extname(p) ? path.dirname(p) : p) + '/' // ex, D:\\a\\resolvewithplus\\pathto\\testfiles\\testscript.js // -> D:/a/resolvewithplus/pathto/testfiles/testscript.js @@ -409,7 +413,7 @@ const begin = (requirepath, withpath, opts) => { var fullpath = null withpath = typeof withpath === 'string' - ? getasdirname(decodeURI(withpath)) + ? getasdirname(getaspath(decodeURI(withpath))) : process.cwd() if (isBuiltinRe.test(requirepath)) { diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index ac4c209..8f830a6 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -94,6 +94,10 @@ test('should return fileurl paths, as import.meta.resolve', async () => { assert.strictEqual( // relpath await metaresolve(relpathtoindex, fullpathfileurl), resolvewithplus(relpathtoindex, fullpath)) + + assert.strictEqual( + await metaresolve(relpathtoindex, import.meta.url), + resolvewithplus(relpathtoindex, import.meta.url)) }) test('should return a full path when given relative path to index file', () => {