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

Support module resolution for directories #28

Merged
merged 1 commit into from
Jun 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node:
- '14'
- '16'
- '17'
- '18'
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
14 changes: 12 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import process from 'process';
import {createRequire} from 'module';
import {fileURLToPath} from 'url';

/**
Expand All @@ -16,18 +17,27 @@ export function stripExt(name) {
return name.slice(0, -extension.length);
}

/**
* @type {NodeRequire}
*/
let require;

/**
* Check if a module was run directly with node as opposed to being
* imported from another module.
* @param {ImportMeta} meta The `import.meta` object.
* @return {boolean} The module was run directly with node.
*/
export default function esMain(meta) {
const scriptPath = process.argv[1];
if (!meta || !scriptPath) {
if (!meta || !process.argv[1]) {
return false;
}

if (!require) {
require = createRequire(meta.url);
}
const scriptPath = require.resolve(process.argv[1]);

const modulePath = fileURLToPath(meta.url);

const extension = path.extname(scriptPath);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"test:with-extension": "node test.js",
"test:without-extension": "node test",
"test:without-node": "./test.js",
"test:resolve-index": "node test-resolve-index",
"test:resolve-main": "node test-resolve-main",
"test:repl": "node --eval \"import('./main.js').then(mod => {if (mod.default({})) throw new Error('expected false')})\"",
"test:types": "npx tsc --noEmit",
"test": "npm-run-all test:*"
Expand Down
4 changes: 4 additions & 0 deletions test-resolve-index/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import esMain from '../main.js';
import {strictEqual} from 'assert';

strictEqual(esMain(import.meta), true, 'run as main');
4 changes: 4 additions & 0 deletions test-resolve-main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import esMain from '../main.js';
import {strictEqual} from 'assert';

strictEqual(esMain(import.meta), true, 'run as main');
4 changes: 4 additions & 0 deletions test-resolve-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module",
"main": "main.js"
}