Skip to content

Commit b785745

Browse files
Merge pull request #177 from davidchambers/import-meta-resolve
use `import.meta.resolve` to support relative and bare specifiers
2 parents 26be516 + 47ecd91 commit b785745

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
node-version: [16.0.0, 18, 20]
12+
node-version: [16.2.0, 18, 20]
1313
steps:
1414
- uses: actions/checkout@v3
1515
- name: Use Node.js ${{ matrix.node-version }}

bin/doctest

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const flags = idx >= 0 && idx < args.length - 1;
1111
require ('child_process')
1212
.spawn (
1313
process.execPath,
14-
['--experimental-vm-modules',
14+
['--experimental-import-meta-resolve',
15+
'--experimental-vm-modules',
1516
...(flags ? args[idx + 1].split (/\s+/) : []),
1617
'--',
1718
path.resolve (__dirname, '..', 'lib', 'command.js'),

lib/doctest.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import fs from 'node:fs/promises';
1212
import {dirname, resolve} from 'node:path';
13+
import url from 'node:url';
1314
import vm from 'node:vm';
1415

1516
import * as acorn from 'acorn';
@@ -418,14 +419,16 @@ const run = queue =>
418419
return [{lines: {input: input.lines, output: output.lines}, comparison}];
419420
});
420421

421-
const evaluateModule = async source => {
422+
const evaluateModule = moduleUrl => async source => {
422423
const queue = [];
423424
const enqueue = io => { queue.push (io); };
424425
const __doctest = {enqueue};
425426
const context = vm.createContext ({...global, __doctest});
426427
const module = new vm.SourceTextModule (source, {context});
427428
await module.link (async (specifier, referencingModule) => {
428-
const entries = Object.entries (await import (specifier));
429+
// import.meta.resolve returned a promise prior to Node.js v20.0.0.
430+
const importUrl = await import.meta.resolve (specifier, moduleUrl);
431+
const entries = Object.entries (await import (importUrl));
429432
const module = new vm.SyntheticModule (
430433
entries.map (([name]) => name),
431434
() => {
@@ -496,18 +499,18 @@ const test = options => path => rewrite => async evaluate => {
496499
};
497500

498501
export default options => async path => {
502+
const __filename = resolve (process.cwd (), path);
499503
let context = {};
500504
switch (options.module) {
501505
case 'esm': {
502506
return test (options)
503507
(path)
504508
(rewriteJs ('module'))
505-
(evaluateModule);
509+
(evaluateModule (url.pathToFileURL (__filename)));
506510
}
507511
case 'commonjs': {
508512
const exports = {};
509513
const module = {exports};
510-
const __filename = resolve (process.cwd (), path);
511514
const __dirname = dirname (__filename);
512515
context = {process, exports, module, require, __dirname, __filename};
513516
} // fall through

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"url": "git://github.com/davidchambers/doctest.git"
2121
},
2222
"engines": {
23-
"node": ">=16.0.0"
23+
"node": ">=16.2.0"
2424
},
2525
"dependencies": {
2626
"acorn": "8.11.x",

0 commit comments

Comments
 (0)