-
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
BREAKING CHANGE: drop Node.js < 18.19.0 support eggjs/egg#5257
- Loading branch information
Showing
24 changed files
with
378 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
{ | ||
"name": "@eggjs/utils", | ||
"version": "3.0.1", | ||
"engine": { | ||
"node": ">=18.19.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Utils for all egg projects", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"lint": "eslint .", | ||
"pretest": "npm run lint && npm run tsc", | ||
"test": "egg-bin test", | ||
"preci": "npm run lint && npm run tsc", | ||
"ci": "egg-bin cov", | ||
"tsc": "tsc", | ||
"clean": "tsc --build --clean", | ||
"prepublishOnly": "npm run clean && npm run tsc", | ||
"contributor": "git-contributor" | ||
"lint": "eslint src test --ext ts", | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test", | ||
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly", | ||
"contributor": "git-contributor", | ||
"prepublishOnly": "tshy && tshy-after" | ||
}, | ||
"keywords": [ | ||
"egg", | ||
|
@@ -25,29 +23,51 @@ | |
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/eggjs/egg-utils.git" | ||
"url": "git://github.com/eggjs/egg-utils.git" | ||
}, | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@eggjs/tsconfig": "^1.3.3", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.2.5", | ||
"coffee": "^5.5.0", | ||
"egg-bin": "^6.4.0", | ||
"eslint": "^8.41.0", | ||
"eslint-config-egg": "^12.2.1", | ||
"git-contributor": "^2.1.5", | ||
"mm": "^3.3.0", | ||
"npm": "^9.6.7", | ||
"npminstall": "^7.9.0", | ||
"runscript": "^1.5.3", | ||
"typescript": "^5.0.4" | ||
"@eggjs/tsconfig": "1", | ||
"@types/mocha": "10", | ||
"@types/node": "20", | ||
"coffee": "5", | ||
"egg-bin": "6", | ||
"eslint": "8", | ||
"eslint-config-egg": "13", | ||
"git-contributor": "2", | ||
"mm": "3", | ||
"runscript": "1", | ||
"tshy": "1", | ||
"tshy-after": "1", | ||
"typescript": "5" | ||
}, | ||
"engine": { | ||
"node": ">=16.13.0" | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"type": "module", | ||
"tshy": { | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": "./src/index.ts" | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"import": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/esm/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"require": { | ||
"source": "./src/index.ts", | ||
"types": "./dist/commonjs/index.d.ts", | ||
"default": "./dist/commonjs/index.js" | ||
} | ||
} | ||
}, | ||
"main": "./dist/commonjs/index.js", | ||
"types": "./dist/commonjs/index.d.ts" | ||
} |
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.