Skip to content

Commit

Permalink
Merge pull request #177 from davidchambers/import-meta-resolve
Browse files Browse the repository at this point in the history
use `import.meta.resolve` to support relative and bare specifiers
  • Loading branch information
davidchambers authored Dec 26, 2023
2 parents 26be516 + 47ecd91 commit b785745
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.0.0, 18, 20]
node-version: [16.2.0, 18, 20]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
3 changes: 2 additions & 1 deletion bin/doctest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const flags = idx >= 0 && idx < args.length - 1;
require ('child_process')
.spawn (
process.execPath,
['--experimental-vm-modules',
['--experimental-import-meta-resolve',
'--experimental-vm-modules',
...(flags ? args[idx + 1].split (/\s+/) : []),
'--',
path.resolve (__dirname, '..', 'lib', 'command.js'),
Expand Down
11 changes: 7 additions & 4 deletions lib/doctest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import fs from 'node:fs/promises';
import {dirname, resolve} from 'node:path';
import url from 'node:url';
import vm from 'node:vm';

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

const evaluateModule = async source => {
const evaluateModule = moduleUrl => async source => {
const queue = [];
const enqueue = io => { queue.push (io); };
const __doctest = {enqueue};
const context = vm.createContext ({...global, __doctest});
const module = new vm.SourceTextModule (source, {context});
await module.link (async (specifier, referencingModule) => {
const entries = Object.entries (await import (specifier));
// import.meta.resolve returned a promise prior to Node.js v20.0.0.
const importUrl = await import.meta.resolve (specifier, moduleUrl);
const entries = Object.entries (await import (importUrl));
const module = new vm.SyntheticModule (
entries.map (([name]) => name),
() => {
Expand Down Expand Up @@ -496,18 +499,18 @@ const test = options => path => rewrite => async evaluate => {
};

export default options => async path => {
const __filename = resolve (process.cwd (), path);
let context = {};
switch (options.module) {
case 'esm': {
return test (options)
(path)
(rewriteJs ('module'))
(evaluateModule);
(evaluateModule (url.pathToFileURL (__filename)));
}
case 'commonjs': {
const exports = {};
const module = {exports};
const __filename = resolve (process.cwd (), path);
const __dirname = dirname (__filename);
context = {process, exports, module, require, __dirname, __filename};
} // fall through
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"url": "git://github.com/davidchambers/doctest.git"
},
"engines": {
"node": ">=16.0.0"
"node": ">=16.2.0"
},
"dependencies": {
"acorn": "8.11.x",
Expand Down

0 comments on commit b785745

Please sign in to comment.