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

fix: make "url" optional in resolver created with createResolve #44

Merged
merged 1 commit into from Jun 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/resolve.ts
Expand Up @@ -118,7 +118,7 @@ export function resolvePath (id: string, opts?: ResolveOptions) {
}

export function createResolve (defaults?: ResolveOptions) {
return (id, url) => {
return (id: string, url?: ResolveOptions['url']) => {
return resolve(id, { url, ...defaults })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also noticed that the logic here is incorrect as the defaults will override the url even when it is explicitly provided. I didn't want to mix that fix in into this one but I can create a follow-up PR for that.

}
}
4 changes: 3 additions & 1 deletion test/fixture/eval-err.mjs
@@ -1,4 +1,6 @@
async function test() {
// @ts-nocheck
// eslint-disable-next-line require-await
async function test () {
throw new Error('Something went wrong in eval-err module!')
}

Expand Down
6 changes: 3 additions & 3 deletions test/fixture/resolve.mjs
@@ -1,9 +1,9 @@
import { resolvePath, createResolve, resolveImports } from 'mlly'

import.meta.resolve = createResolve({ url: import.meta.url })
console.log(await import.meta.resolve('./cjs.mjs'))
const resolve = createResolve({ url: import.meta.url })
console.log(await resolve('./cjs.mjs'))
Comment on lines +3 to +4
Copy link
Contributor Author

@rchl rchl Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that those test files are not really used for anything right now (which is a shame because it would have caught an issue otherwise) but I've changed this to create a local variable rather than assigning to import.meta.resolve as import.meta.resolve has its own type already (coming from Node) so even though it's our custom resolver, it has still used the Node type and thus wouldn't check type issues if those would be inconsistent.


console.log(await resolvePath('./cjs.mjs', { url: import.meta.url }))
console.log(await resolvePath('./foo', { url: import.meta.url }))

console.log(await resolveImports(`import foo from './eval.mjs'`, { url: import.meta.url }))
console.log(await resolveImports('import foo from \'./eval.mjs\'', { url: import.meta.url }))
5 changes: 5 additions & 0 deletions tsconfig.json
Expand Up @@ -3,7 +3,12 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"esModuleInterop": true,
"checkJs": true,
"paths": {
"mlly": ["./"]
},
"types": [
"node"
]
Expand Down