-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support @eggjs/core next version (#21)
BREAKING CHANGE: drop Node.js < 18.19.0 support eggjs/egg#5257 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced new utility functions for resolving and importing modules with support for CommonJS and ESM formats. - Added new test fixtures for CommonJS and ESM modules to validate module import functionality. - **Refactor** - Updated import statements to include file extensions (`.js`) for consistency and compatibility. - Refactored code to use async/await for asynchronous operations. - Improved path handling in tests with helper functions. - **Documentation** - Updated `package.json` with new scripts, dependencies, and module management configurations. - **Chores** - Enhanced `.gitignore` to exclude `.tshy*` files and `dist/` directory. - Modified GitHub Actions workflows for Node.js and release processes. - **Tests** - Added tests for new module import functions. - Updated existing tests to reflect new import paths and async changes. - **Configuration** - Updated `tsconfig.json` for stricter TypeScript settings and modern module resolution. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
28 changed files
with
444 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ test/fixtures/**/*.yml | |
.nyc_output/ | ||
test/fixtures/tmp/ | ||
lib/ | ||
.tshy* | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { debuglog } from 'node:util'; | ||
import { createRequire } from 'node:module'; | ||
import { pathToFileURL } from 'node:url'; | ||
|
||
const debug = debuglog('@eggjs/utils:loader'); | ||
|
||
let _customRequire: NodeRequire; | ||
|
||
export interface ImportResolveOptions { | ||
paths?: string[]; | ||
} | ||
|
||
export interface ImportModuleOptions extends ImportResolveOptions { | ||
// only import export default object | ||
importDefaultOnly?: boolean; | ||
} | ||
|
||
export function importResolve(filepath: string, options?: ImportResolveOptions) { | ||
if (!_customRequire) { | ||
if (typeof require !== 'undefined') { | ||
_customRequire = require; | ||
} else { | ||
_customRequire = createRequire(process.cwd()); | ||
} | ||
} | ||
const moduleFilePath = _customRequire.resolve(filepath, options); | ||
debug('[importResolve] %o, options: %o => %o', filepath, options, moduleFilePath); | ||
return moduleFilePath; | ||
} | ||
|
||
export async function importModule(filepath: string, options?: ImportModuleOptions) { | ||
const moduleFilePath = importResolve(filepath, options); | ||
let obj: any; | ||
if (typeof require === 'function') { | ||
// commonjs | ||
obj = require(moduleFilePath); | ||
debug('[importModule] require %o => %o', filepath, obj); | ||
if (obj?.__esModule === true && obj?.default) { | ||
// 兼容 cjs 模拟 esm 的导出格式 | ||
// { | ||
// __esModule: true, | ||
// default: { fn: [Function: fn], foo: 'bar', one: 1 } | ||
// } | ||
obj = obj.default; | ||
} | ||
} else { | ||
// esm | ||
debug('[importModule] await import start: %o', filepath); | ||
const fileUrl = pathToFileURL(moduleFilePath).toString(); | ||
obj = await import(fileUrl); | ||
debug('[importModule] await import end: %o => %o', filepath, obj); | ||
// { | ||
// default: { foo: 'bar', one: 1 }, | ||
// foo: 'bar', | ||
// one: 1, | ||
// [Symbol(Symbol.toStringTag)]: 'Module' | ||
// } | ||
if (obj?.__esModule === true && obj?.default?.__esModule === true) { | ||
// 兼容 cjs 模拟 esm 的导出格式 | ||
// { | ||
// __esModule: true, | ||
// default: { | ||
// __esModule: true, | ||
// default: { | ||
// fn: [Function: fn] { [length]: 0, [name]: 'fn' }, | ||
// foo: 'bar', | ||
// one: 1 | ||
// } | ||
// }, | ||
// [Symbol(Symbol.toStringTag)]: 'Module' | ||
// } | ||
obj = obj.default; | ||
} | ||
if (options?.importDefaultOnly) { | ||
if (obj.default) { | ||
obj = obj.default; | ||
} | ||
} | ||
} | ||
debug('[importModule] return %o => %o', filepath, obj); | ||
return obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.