Skip to content

Commit

Permalink
fix: debug dont print full import object (#29)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Introduced a new JavaScript module for configuration.
  
- **Bug Fixes**
	- Enhanced error handling for module resolution.
	- Improved clarity of debug logging during import processes.

- **Tests**
- Added new test cases for `importResolve` to verify module path
resolution and error handling.
- Confirmed existing tests for `importModule` across various module
types.

- **Chores**
- Updated dependency versions in the `package.json` for the `egg-app`
project.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 24, 2024
1 parent 57d5863 commit daa5edf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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)) {
throw new TypeError(`Cannot find module ${filepath}, because ${moduleFilePath} does not exists`);
}
} else {
moduleFilePath = getRequire().resolve(filepath);
}
Expand All @@ -188,7 +191,8 @@ export function importResolve(filepath: string, options?: ImportResolveOptions)
paths,
});
}
debug('[importResolve] %o, options: %o => %o', filepath, options, moduleFilePath);
debug('[importResolve] %o, options: %o => %o, isESM: %s',
filepath, options, moduleFilePath, isESM);
return moduleFilePath;
}

Expand All @@ -198,9 +202,8 @@ export async function importModule(filepath: string, options?: ImportModuleOptio
if (isESM) {
// esm
const fileUrl = pathToFileURL(moduleFilePath).toString();
debug('[importModule] await import start: %o', fileUrl);
obj = await import(fileUrl);
debug('[importModule] await import end: %o => %o', filepath, obj);
debug('[importModule] await import %o', fileUrl);
// {
// default: { foo: 'bar', one: 1 },
// foo: 'bar',
Expand Down Expand Up @@ -243,7 +246,7 @@ export async function importModule(filepath: string, options?: ImportModuleOptio
} else {
// commonjs
obj = require(moduleFilePath);
debug('[importModule] require %o => %o', filepath, obj);
debug('[importModule] require %o', moduleFilePath);
if (obj?.__esModule === true && 'default' in obj) {
// 兼容 cjs 模拟 esm 的导出格式
// {
Expand All @@ -253,6 +256,8 @@ export async function importModule(filepath: string, options?: ImportModuleOptio
obj = obj.default;
}
}
debug('[importModule] return %o => %o', filepath, obj);
if (debug.enabled) {
debug('[importModule] return %o => keys: %j', filepath, obj ? Object.keys(obj) : obj);
}
return obj;
}
4 changes: 2 additions & 2 deletions test/fixtures/egg-app/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "egg-app",
"dependencies": {
"egg": "*",
"@eggjs/core": "*",
"egg": "beta",
"@eggjs/core": "6",
"framework-demo": "^1.0.1"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/esm/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
foo: 'bar',
};
4 changes: 4 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('test/import.test.ts', () => {

it('should work on esm', () => {
assert.equal(importResolve(getFilepath('esm')), getFilepath('esm/index.js'));
assert.equal(importResolve(getFilepath('esm/config/plugin')), getFilepath('esm/config/plugin.js'));
assert.throws(() => {
importResolve(getFilepath('esm/config/plugin.default'));
}, /Cannot find module/);
});

it('should work on ts-module', () => {
Expand Down

0 comments on commit daa5edf

Please sign in to comment.