Skip to content

Commit dcaeda3

Browse files
authored
Change to make resolve sync
Browsers, deno, and now Node.js too (since v20) all use a sync resolve. This change matches that. This *probably* does not break code, because the following two lines are equal: ```js const a = await 'string' const b = 'string' ``` Closes GH-14. Closes GH-15.
1 parent 4831612 commit dcaeda3

7 files changed

Lines changed: 591 additions & 550 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ jobs:
2020
- ubuntu-latest
2121
- windows-latest
2222
node:
23-
- lts/hydrogen
23+
- 18
2424
- node

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
*.log
44
coverage/
55
/node_modules
6-
test/baseline.js
6+
test/baseline*.js
77
yarn.lock

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import {defaultResolve} from './lib/resolve.js'
1515
* @param {string} parent
1616
* The absolute parent module URL to resolve from.
1717
* You should pass `import.meta.url` or something else.
18-
* @returns {Promise<string>}
19-
* Returns a promise that resolves to a full `file:`, `data:`, or `node:` URL
18+
* @returns {string}
19+
* Returns a string to a full `file:`, `data:`, or `node:` URL
2020
* to the found thing.
2121
*/
22-
export async function resolve(specifier, parent) {
22+
export function resolve(specifier, parent) {
2323
if (!parent) {
2424
throw new Error(
2525
'Please pass `parent`: `import-meta-resolve` cannot ponyfill that'
@@ -31,10 +31,14 @@ export async function resolve(specifier, parent) {
3131
} catch (error) {
3232
const exception = /** @type {ErrnoException} */ (error)
3333

34-
return exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' &&
34+
if (
35+
exception.code === 'ERR_UNSUPPORTED_DIR_IMPORT' &&
3536
typeof exception.url === 'string'
36-
? exception.url
37-
: Promise.reject(error)
37+
) {
38+
return exception.url
39+
}
40+
41+
throw error
3842
}
3943
}
4044

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"generate": "node --conditions development script.js",
4646
"build": "tsc --build --clean && tsc --build && type-coverage",
4747
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
48-
"test-api": "node --experimental-import-meta-resolve test/baseline.js && node test/index.js",
48+
"test-api": "node --experimental-import-meta-resolve test/baseline.js && node --experimental-import-meta-resolve test/baseline-async.js && node test/index.js",
4949
"test-coverage": "c8 --check-coverage --branches 75 --functions 75 --lines 75 --statements 75 --reporter lcov npm run test-api",
5050
"test": "npm run generate && npm run build && npm run format && npm run test-coverage"
5151
},

script.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
import path from 'node:path'
22
import fs from 'node:fs/promises'
33

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

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

10-
await fs.writeFile(path.join('test', 'baseline.js'), lines.join('\n'))
16+
await fs.writeFile(path.join('test', 'baseline-async.js'), asyncLines)
17+
18+
const syncLines = base
19+
// Node < 20 does not support sync import.meta.resolve, so skipping these tests if so
20+
.replace(/\bresolve(?=\()/g, 'import.meta.resolve')
21+
.replace(/\bresolve(?=,)/g, 'import.meta.resolve')
22+
.replace(
23+
'{skip: false}',
24+
"{skip: semver.lt(process.versions.node, '20.0.0')}"
25+
)
26+
.replace(
27+
/const run = .*$/g,
28+
'const run = (/** @type {() => void} */ f) => f()'
29+
)
30+
.replace(/run\(/g, 'run(async ')
31+
32+
await fs.writeFile(path.join('test', 'baseline.js'), syncLines)

0 commit comments

Comments
 (0)