diff --git a/src/import.ts b/src/import.ts index 1486971..9d9da38 100644 --- a/src/import.ts +++ b/src/import.ts @@ -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); } @@ -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; } @@ -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', @@ -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 的导出格式 // { @@ -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; } diff --git a/test/fixtures/egg-app/package.json b/test/fixtures/egg-app/package.json index d2923b3..bf00e68 100644 --- a/test/fixtures/egg-app/package.json +++ b/test/fixtures/egg-app/package.json @@ -1,8 +1,8 @@ { "name": "egg-app", "dependencies": { - "egg": "*", - "@eggjs/core": "*", + "egg": "beta", + "@eggjs/core": "6", "framework-demo": "^1.0.1" } } diff --git a/test/fixtures/esm/config/plugin.js b/test/fixtures/esm/config/plugin.js new file mode 100644 index 0000000..5532a6a --- /dev/null +++ b/test/fixtures/esm/config/plugin.js @@ -0,0 +1,3 @@ +export default { + foo: 'bar', +}; diff --git a/test/import.test.ts b/test/import.test.ts index d35d6a3..5d440fc 100644 --- a/test/import.test.ts +++ b/test/import.test.ts @@ -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', () => {