Skip to content

Commit

Permalink
fix: use importResolve to get framework path
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 27, 2024
1 parent a625246 commit 474f6d4
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions src/framework.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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`),

Check warning on line 73 in src/framework.ts

View check run for this annotation

Codecov / codecov/patch

src/framework.ts#L73

Added line #L73 was not covered by tests
'../../..',
);
} 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);
Expand Down
4 changes: 3 additions & 1 deletion src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/cjs-index/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
foo: 'bar',
};

module.exports.one = 1;
3 changes: 3 additions & 0 deletions test/fixtures/cjs-index/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
5 changes: 5 additions & 0 deletions test/fixtures/cjs/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { importResolve } = require('../../../');

console.log('%o', importResolve(__dirname, {
paths: __dirname,
}));
2 changes: 0 additions & 2 deletions test/fixtures/egg-app/config/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const path = require('path');

exports.p = {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/egg-app/get_loadunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})();
2 changes: 1 addition & 1 deletion test/fixtures/egg-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "egg-app",
"dependencies": {
"egg": "beta",
"@eggjs/core": "6",
"@eggjs/core": "beta",
"framework-demo": "^1.0.1"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/esm-index/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
foo: 'bar',
};

export const one = 1;
3 changes: 3 additions & 0 deletions test/fixtures/esm-index/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
10 changes: 10 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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'));
Expand Down

0 comments on commit 474f6d4

Please sign in to comment.