From 474f6d4ef10ed4b86bae9b0834ee5e3de5f6990e Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 27 Dec 2024 23:33:29 +0800 Subject: [PATCH] fix: use importResolve to get framework path --- package.json | 2 +- src/framework.ts | 9 +++++++-- src/import.ts | 4 +++- test/fixtures/cjs-index/index.cjs | 5 +++++ test/fixtures/cjs-index/package.json | 3 +++ test/fixtures/cjs/run.js | 5 +++++ test/fixtures/egg-app/config/plugin.test.js | 2 -- test/fixtures/egg-app/get_loadunit.js | 1 + test/fixtures/egg-app/package.json | 2 +- test/fixtures/esm-index/index.mjs | 5 +++++ test/fixtures/esm-index/package.json | 3 +++ test/import.test.ts | 10 ++++++++++ 12 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/cjs-index/index.cjs create mode 100644 test/fixtures/cjs-index/package.json create mode 100644 test/fixtures/cjs/run.js create mode 100644 test/fixtures/esm-index/index.mjs create mode 100644 test/fixtures/esm-index/package.json diff --git a/package.json b/package.json index b4e0e07..28ff2ca 100644 --- a/package.json +++ b/package.json @@ -31,11 +31,11 @@ "dependencies": {}, "devDependencies": { "@arethetypeswrong/cli": "^0.17.2", + "@eggjs/bin": "^7.0.0", "@eggjs/tsconfig": "1", "@types/mocha": "10", "@types/node": "22", "coffee": "5", - "egg-bin": "^6.13.0", "eslint": "8", "eslint-config-egg": "14", "mm": "4", diff --git a/src/framework.ts b/src/framework.ts index 9286b95..0c803cb 100644 --- a/src/framework.ts +++ b/src/framework.ts @@ -1,7 +1,11 @@ +import { debuglog } from 'node:util'; import path from 'node:path'; import assert from 'node:assert'; import { existsSync } from 'node:fs'; import { readJSONSync } from './utils.js'; +import { importResolve } from './import.js'; + +const debug = debuglog('@eggjs/utils/framework'); const initCwd = process.cwd(); @@ -66,18 +70,19 @@ function assertAndReturn(frameworkName: string, moduleDir: string) { // if frameworkName is scoped package, like @ali/egg if (frameworkName.startsWith('@') && frameworkName.includes('/')) { globalModuleDir = path.join( - require.resolve(`${frameworkName}/package.json`), + importResolve(`${frameworkName}/package.json`), '../../..', ); } else { globalModuleDir = path.join( - require.resolve(`${frameworkName}/package.json`), + importResolve(`${frameworkName}/package.json`), '../..', ); } moduleDirs.add(globalModuleDir); } catch (err) { // ignore + debug('importResolve %s on %s error: %s', frameworkName, moduleDir, err); } for (const moduleDir of moduleDirs) { const frameworkPath = path.join(moduleDir, frameworkName); diff --git a/src/import.ts b/src/import.ts index a3eef0a..c8b0d0f 100644 --- a/src/import.ts +++ b/src/import.ts @@ -188,7 +188,9 @@ export function importResolve(filepath: string, options?: ImportResolveOptions) // resolve will return file:// URL on Linux and MacOS expect on Windows moduleFilePath = fileURLToPath(moduleFilePath); } - if (!fs.existsSync(moduleFilePath)) { + debug('[importResolve] import.meta.resolve %o => %o', filepath, moduleFilePath); + const stat = fs.statSync(moduleFilePath, { throwIfNoEntry: false }); + if (!stat?.isFile()) { throw new TypeError(`Cannot find module ${filepath}, because ${moduleFilePath} does not exists`); } } else { diff --git a/test/fixtures/cjs-index/index.cjs b/test/fixtures/cjs-index/index.cjs new file mode 100644 index 0000000..2bf2ae3 --- /dev/null +++ b/test/fixtures/cjs-index/index.cjs @@ -0,0 +1,5 @@ +module.exports = { + foo: 'bar', +}; + +module.exports.one = 1; diff --git a/test/fixtures/cjs-index/package.json b/test/fixtures/cjs-index/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/test/fixtures/cjs-index/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/test/fixtures/cjs/run.js b/test/fixtures/cjs/run.js new file mode 100644 index 0000000..e7949e6 --- /dev/null +++ b/test/fixtures/cjs/run.js @@ -0,0 +1,5 @@ +const { importResolve } = require('../../../'); + +console.log('%o', importResolve(__dirname, { + paths: __dirname, +})); diff --git a/test/fixtures/egg-app/config/plugin.test.js b/test/fixtures/egg-app/config/plugin.test.js index c7cc652..c340eb7 100644 --- a/test/fixtures/egg-app/config/plugin.test.js +++ b/test/fixtures/egg-app/config/plugin.test.js @@ -1,5 +1,3 @@ -'use strict'; - const path = require('path'); exports.p = { diff --git a/test/fixtures/egg-app/get_loadunit.js b/test/fixtures/egg-app/get_loadunit.js index 9362d7d..cd78dc0 100644 --- a/test/fixtures/egg-app/get_loadunit.js +++ b/test/fixtures/egg-app/get_loadunit.js @@ -4,6 +4,7 @@ const { getLoadUnits } = require('../../..'); console.log(process.argv[2]); const units = await getLoadUnits(JSON.parse(process.argv[2])); console.log('get %s plugin', units.filter(p => p.type === 'plugin').length); + // console.log(units.filter(p => p.type === 'plugin')); console.log('get %s framework', units.filter(p => p.type === 'framework').length); console.log('get %s app', units.filter(p => p.type === 'app').length); })(); diff --git a/test/fixtures/egg-app/package.json b/test/fixtures/egg-app/package.json index bf00e68..bbc54c0 100644 --- a/test/fixtures/egg-app/package.json +++ b/test/fixtures/egg-app/package.json @@ -2,7 +2,7 @@ "name": "egg-app", "dependencies": { "egg": "beta", - "@eggjs/core": "6", + "@eggjs/core": "beta", "framework-demo": "^1.0.1" } } diff --git a/test/fixtures/esm-index/index.mjs b/test/fixtures/esm-index/index.mjs new file mode 100644 index 0000000..d840cca --- /dev/null +++ b/test/fixtures/esm-index/index.mjs @@ -0,0 +1,5 @@ +export default { + foo: 'bar', +}; + +export const one = 1; diff --git a/test/fixtures/esm-index/package.json b/test/fixtures/esm-index/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/test/fixtures/esm-index/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/import.test.ts b/test/import.test.ts index d8b9b20..33bdde9 100644 --- a/test/import.test.ts +++ b/test/import.test.ts @@ -1,4 +1,5 @@ import { strict as assert } from 'node:assert'; +import coffee from 'coffee'; import { importResolve, importModule } from '../src/index.js'; import { getFilepath } from './helper.js'; @@ -7,10 +8,19 @@ describe('test/import.test.ts', () => { it('should work on cjs', () => { assert.equal(importResolve(getFilepath('cjs')), getFilepath('cjs/index.js')); assert.equal(importResolve(getFilepath('cjs/exports')), getFilepath('cjs/exports.js')); + assert.equal(importResolve(getFilepath('cjs-index')), getFilepath('cjs-index/index.cjs')); + }); + + it('should work on commonjs and require exists', () => { + return coffee.fork(getFilepath('cjs/run.js')) + // .debug() + .expect('stdout', /index\.js/) + .end(); }); it('should work on esm', () => { assert.equal(importResolve(getFilepath('esm')), getFilepath('esm/index.js')); + assert.equal(importResolve(getFilepath('esm-index')), getFilepath('esm-index/index.mjs')); assert.equal(importResolve(getFilepath('esm/config/plugin')), getFilepath('esm/config/plugin.js')); assert.throws(() => { importResolve(getFilepath('esm/config/plugin.default'));