Skip to content

Commit

Permalink
Merge pull request #40 from iambumblehead/return-encoded-url-same-as-…
Browse files Browse the repository at this point in the history
…import.meta.resolve

return encoded url same as import.meta.resolve
  • Loading branch information
iambumblehead authored Oct 20, 2022
2 parents 8fd70bc + 5cbf5fb commit c27c3c4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 2.0.0 _Oct.19.2022_
* return encoded url [same as import.meta.resolve](https://github.com/iambumblehead/resolvewithplus/pull/40)
* 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_
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": "1.0.2",
"version": "2.0.0",
"engines" : {
"node" : ">=12.16.0"
},
Expand Down
3 changes: 1 addition & 2 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const packageNameRe = /(^@[^/]*\/[^/]*|^[^/]*)\/?(.*)$/
const isESMImportSubpathRe = /^#/
const esmStrGlobRe = /(\*)/g
const esmStrPathCharRe = /([./])/g
const rootDirSlashRe = /^\//
const protocolNode = /^node:/
const protocolFile = /^file:/
const supportedExtensions = [ '.js', '.mjs', '.ts', '.tsx', '.json', '.node' ]
Expand All @@ -33,7 +32,7 @@ const isobj = o => o && typeof o === 'object'
const cache = {}

const addprotocolnode = p => protocolNode.test(p) ? p : `node:${p}`
const addprotocolfile = p => p && ('file:///' + p.replace(rootDirSlashRe, ''))
const addprotocolfile = p => p && url.pathToFileURL(p).href
const iscoremodule = p => isBuiltinRe.test(p)
const getaspath = p => protocolFile.test(p) ? url.fileURLToPath(p) : p
const getasdirname = p =>
Expand Down
1 change: 1 addition & 0 deletions tests/testfiles/path/to/indexfile/file name with spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'hellow'
5 changes: 5 additions & 0 deletions tests/tests-basic/tests-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ test('should return fileurl paths, as import.meta.resolve', async () => {
const fullpath = path.resolve('../testfiles') + '/'
const fullpathfileurl = tofileurl(fullpath)
const relpathtoindex = '../testfiles/path/to/indexfile/index.js'
const relpathspace = '../testfiles/path/to/indexfile/file name with spaces.js'
const metaresolve = import.meta.resolve

assert.strictEqual(
Expand Down Expand Up @@ -98,6 +99,10 @@ test('should return fileurl paths, as import.meta.resolve', async () => {
assert.strictEqual(
await metaresolve(relpathtoindex, import.meta.url),
resolvewithplus(relpathtoindex, import.meta.url))

assert.strictEqual(
await metaresolve(relpathspace, import.meta.url),
resolvewithplus(relpathspace, import.meta.url))
})

test('should return a full path when given relative path to index file', () => {
Expand Down

0 comments on commit c27c3c4

Please sign in to comment.