Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return encoded url same as import.meta.resolve #40

Merged
merged 3 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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