Skip to content
New issue

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

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

Already on GitHub? # to your account

fix #14: make resolve synchronous #15

Merged
merged 10 commits into from
Apr 24, 2023
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ jobs:
- ubuntu-latest
- windows-latest
node:
- lts/hydrogen
- node
- 18
- latest
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*.log
coverage/
/node_modules
test/baseline.js
test/baseline*.js
yarn.lock
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {defaultResolve} from './lib/resolve.js'
* @param {string} parent
* The absolute parent module URL to resolve from.
* You should pass `import.meta.url` or something else.
* @returns {Promise<string>}
* Returns a promise that resolves to a full `file:`, `data:`, or `node:` URL
* @returns {string}
* Returns a string to a full `file:`, `data:`, or `node:` URL
* to the found thing.
*/
export async function resolve(specifier, parent) {
export function resolve(specifier, parent) {
if (!parent) {
throw new Error(
'Please pass `parent`: `import-meta-resolve` cannot ponyfill that'
Expand All @@ -31,10 +31,14 @@ export async function resolve(specifier, parent) {
} catch (error) {
const exception = /** @type {ErrnoException} */ (error)

return exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' &&
if (
exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' &&
typeof exception.url === 'string'
? exception.url
: Promise.reject(error)
) {
return exception.url
}

throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"generate": "node --conditions development script.js",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --experimental-import-meta-resolve test/baseline.js && node test/index.js",
"test-api": "node --experimental-import-meta-resolve test/baseline.js && node --experimental-import-meta-resolve test/baseline-async.js && node test/index.js",
"test-coverage": "c8 --check-coverage --branches 75 --functions 75 --lines 75 --statements 75 --reporter lcov npm run test-api",
"test": "npm run generate && npm run build && npm run format && npm run test-coverage"
},
Expand Down
32 changes: 27 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import path from 'node:path'
import fs from 'node:fs/promises'

const base = await fs.readFile(path.join('test', 'core.js'))
const base = await fs.readFile(path.join('test', 'core.js'), 'utf8')

const lines = String(base)
.replace(/\bresolve(?=\(|,)/g, 'import.meta.resolve')
.split('\n')
const asyncLines = base
// Special baseline test for Node < 20, that doesn't support sync `import.meta.resolve`
.replace(/\bresolve(?=\()/g, 'await import.meta.resolve')
.replace(/\bresolve(?=,)/g, 'import.meta.resolve')
.replace(
/const run = .*$/g,
'const run = async (/** @type {() => Promise<void>} */ f) => f()'
)
.replace(/run\(/g, 'await run(async ')

await fs.writeFile(path.join('test', 'baseline.js'), lines.join('\n'))
await fs.writeFile(path.join('test', 'baseline-async.js'), asyncLines)

const syncLines = base
// Node < 20 does not support sync import.meta.resolve, so skipping these tests if so
.replace(/\bresolve(?=\()/g, 'import.meta.resolve')
.replace(/\bresolve(?=,)/g, 'import.meta.resolve')
.replace(
'{skip: false}',
"{skip: semver.lt(process.versions.node, '20.0.0')}"
)
.replace(
/const run = .*$/g,
'const run = (/** @type {() => void} */ f) => f()'
)
.replace(/run\(/g, 'run(async ')

await fs.writeFile(path.join('test', 'baseline.js'), syncLines)
Loading