diff --git a/.github/workflows/nodejs-14.yml b/.github/workflows/nodejs-14.yml deleted file mode 100644 index 9626ae3..0000000 --- a/.github/workflows/nodejs-14.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Node.js 14 CI - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - name: Test - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Use Node.js - uses: irby/setup-node-nvm@master - with: - node-version: '16.x' - - run: npm install - - run: npm run prepublishOnly - - run: node -v - - run: . /home/runner/mynvm/nvm.sh && nvm install 14 && nvm use 14 && node -v && npm run test-local diff --git a/.github/workflows/nodejs-16.yml b/.github/workflows/nodejs-16.yml deleted file mode 100644 index 3fd0dd6..0000000 --- a/.github/workflows/nodejs-16.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Node.js 16 CI - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - name: Test - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Use Node.js - uses: irby/setup-node-nvm@master - with: - node-version: '16.x' - - run: npm install - - run: npm run prepublishOnly - - run: node -v - - run: npm run test-local diff --git a/package.json b/package.json index 6f0c87d..7caf4c6 100644 --- a/package.json +++ b/package.json @@ -35,10 +35,10 @@ "@types/mocha": "10", "@types/node": "22", "coffee": "5", - "egg-bin": "6", + "egg-bin": "^6.13.0", "eslint": "8", "eslint-config-egg": "14", - "mm": "3", + "mm": "4", "npminstall": "7", "rimraf": "6", "runscript": "2", diff --git a/src/import.ts b/src/import.ts index 9d9da38..a3eef0a 100644 --- a/src/import.ts +++ b/src/import.ts @@ -151,8 +151,10 @@ function tryToResolveByDirname(dirname: string): string | undefined { export function importResolve(filepath: string, options?: ImportResolveOptions) { let moduleFilePath: string | undefined; - if (path.isAbsolute(filepath)) { + const isAbsolute = path.isAbsolute(filepath); + if (isAbsolute) { const stat = fs.statSync(filepath, { throwIfNoEntry: false }); + // try to resolve from directory if (stat?.isDirectory()) { moduleFilePath = tryToResolveByDirname(filepath); if (moduleFilePath) { @@ -160,16 +162,24 @@ export function importResolve(filepath: string, options?: ImportResolveOptions) return moduleFilePath; } } - if (!stat) { - moduleFilePath = tryToResolveFromFile(filepath); - if (moduleFilePath) { - debug('[importResolve] %o => %o', filepath, moduleFilePath); - return moduleFilePath; - } + // try to resolve from file + moduleFilePath = tryToResolveFromFile(filepath); + if (moduleFilePath) { + debug('[importResolve] %o => %o', filepath, moduleFilePath); + return moduleFilePath; } } - if (isESM) { + const extname = path.extname(filepath); + if ((!isAbsolute && extname === '.json') || !isESM) { + // find *.json or CommonJS module by require.resolve + // e.g.: importResolve('egg/package.json', { paths }) + const cwd = process.cwd(); + const paths = options?.paths ?? [ cwd ]; + moduleFilePath = getRequire().resolve(filepath, { + paths, + }); + } else { if (supportImportMetaResolve) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -184,12 +194,6 @@ export function importResolve(filepath: string, options?: ImportResolveOptions) } else { moduleFilePath = getRequire().resolve(filepath); } - } else { - const cwd = process.cwd(); - const paths = options?.paths ?? [ cwd ]; - moduleFilePath = require.resolve(filepath, { - paths, - }); } debug('[importResolve] %o, options: %o => %o, isESM: %s', filepath, options, moduleFilePath, isESM); diff --git a/test/fixtures/framework-egg-default/app.js b/test/fixtures/framework-egg-default/app.js new file mode 100644 index 0000000..f053ebf --- /dev/null +++ b/test/fixtures/framework-egg-default/app.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/fixtures/framework-egg-default/app/router.js b/test/fixtures/framework-egg-default/app/router.js new file mode 100644 index 0000000..79229e3 --- /dev/null +++ b/test/fixtures/framework-egg-default/app/router.js @@ -0,0 +1,2 @@ +module.exports = app => { +}; diff --git a/test/framework.test.ts b/test/framework.test.ts index 17580d4..9f7f39d 100644 --- a/test/framework.test.ts +++ b/test/framework.test.ts @@ -1,12 +1,12 @@ import path from 'node:path'; import { strict as assert } from 'node:assert'; import fs from 'node:fs'; -import mm from 'mm'; +import { restore, mm } from 'mm'; import { getFrameworkPath } from '../src/index.js'; import { getFilepath, testDir } from './helper.js'; describe('test/framework.test.ts', () => { - afterEach(mm.restore); + afterEach(restore); it('should exist when specify baseDir', () => { it('should get egg by default but not exist', () => { diff --git a/test/import.test.ts b/test/import.test.ts index 5d440fc..d8b9b20 100644 --- a/test/import.test.ts +++ b/test/import.test.ts @@ -28,6 +28,16 @@ describe('test/import.test.ts', () => { it('should work on typescript with dist', () => { assert.equal(importResolve(getFilepath('tshy-dist')), getFilepath('tshy-dist/dist2/esm/index.js')); }); + + it('should work on {name}/package.json', () => { + assert.equal(importResolve('egg/package.json', { + paths: [ getFilepath('framework-egg-default') ], + }), getFilepath('framework-egg-default/node_modules/egg/package.json')); + }); + + it('should work on /path/app => /path/app.js', () => { + assert.equal(importResolve(getFilepath('framework-egg-default/app')), getFilepath('framework-egg-default/app.js')); + }); }); describe('importModule()', () => {