Skip to content

Commit a37968c

Browse files
authored
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 -->
1 parent 3ceccab commit a37968c

28 files changed

Lines changed: 444 additions & 190 deletions

.github/workflows/nodejs.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ name: CI
33
on:
44
push:
55
branches: [ master ]
6-
76
pull_request:
87
branches: [ master ]
98

10-
workflow_dispatch: {}
11-
129
jobs:
1310
Job:
1411
name: Node.js
15-
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
12+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1613
with:
17-
os: 'ubuntu-latest'
18-
version: '16.13.0, 16, 18, 20'
14+
version: '18.19.0, 18, 20, 22'
15+
secrets:
16+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/release.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ on:
44
push:
55
branches: [ master ]
66

7-
workflow_dispatch: {}
8-
97
jobs:
108
release:
119
name: Node.js
12-
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
10+
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
1311
secrets:
1412
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1513
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
16-
with:
17-
checkTest: false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ test/fixtures/**/*.yml
1717
.nyc_output/
1818
test/fixtures/tmp/
1919
lib/
20+
.tshy*
21+
dist/

package.json

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"name": "@eggjs/utils",
33
"version": "3.0.1",
4+
"engine": {
5+
"node": ">=18.19.0"
6+
},
7+
"publishConfig": {
8+
"access": "public"
9+
},
410
"description": "Utils for all egg projects",
5-
"main": "lib/index.js",
6-
"types": "lib/index.d.ts",
7-
"files": [
8-
"lib"
9-
],
1011
"scripts": {
11-
"lint": "eslint .",
12-
"pretest": "npm run lint && npm run tsc",
13-
"test": "egg-bin test",
14-
"preci": "npm run lint && npm run tsc",
15-
"ci": "egg-bin cov",
16-
"tsc": "tsc",
17-
"clean": "tsc --build --clean",
18-
"prepublishOnly": "npm run clean && npm run tsc",
19-
"contributor": "git-contributor"
12+
"lint": "eslint src test --ext ts",
13+
"pretest": "npm run prepublishOnly",
14+
"test": "npm run lint -- --fix && npm run test-local",
15+
"test-local": "egg-bin test",
16+
"preci": "npm run prepublishOnly",
17+
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly",
18+
"contributor": "git-contributor",
19+
"prepublishOnly": "tshy && tshy-after"
2020
},
2121
"keywords": [
2222
"egg",
@@ -25,29 +25,52 @@
2525
"author": "fengmk2 <[email protected]> (https://github.com/fengmk2)",
2626
"repository": {
2727
"type": "git",
28-
"url": "https://github.com/eggjs/egg-utils.git"
28+
"url": "git://github.com/eggjs/egg-utils.git"
2929
},
3030
"license": "MIT",
3131
"dependencies": {},
3232
"devDependencies": {
33-
"@eggjs/tsconfig": "^1.3.3",
34-
"@types/mocha": "^10.0.1",
35-
"@types/node": "^20.2.5",
36-
"coffee": "^5.5.0",
37-
"egg-bin": "^6.4.0",
38-
"eslint": "^8.41.0",
39-
"eslint-config-egg": "^12.2.1",
40-
"git-contributor": "^2.1.5",
41-
"mm": "^3.3.0",
42-
"npm": "^9.6.7",
43-
"npminstall": "^7.9.0",
44-
"runscript": "^1.5.3",
45-
"typescript": "^5.0.4"
33+
"@eggjs/tsconfig": "1",
34+
"@types/mocha": "10",
35+
"@types/node": "20",
36+
"coffee": "5",
37+
"egg-bin": "6",
38+
"eslint": "8",
39+
"eslint-config-egg": "13",
40+
"git-contributor": "2",
41+
"mm": "3",
42+
"npminstall": "7",
43+
"runscript": "1",
44+
"tshy": "1",
45+
"tshy-after": "1",
46+
"typescript": "5"
4647
},
47-
"engine": {
48-
"node": ">=16.13.0"
48+
"files": [
49+
"dist",
50+
"src"
51+
],
52+
"type": "module",
53+
"tshy": {
54+
"exports": {
55+
"./package.json": "./package.json",
56+
".": "./src/index.ts"
57+
}
4958
},
50-
"publishConfig": {
51-
"access": "public"
52-
}
59+
"exports": {
60+
"./package.json": "./package.json",
61+
".": {
62+
"import": {
63+
"source": "./src/index.ts",
64+
"types": "./dist/esm/index.d.ts",
65+
"default": "./dist/esm/index.js"
66+
},
67+
"require": {
68+
"source": "./src/index.ts",
69+
"types": "./dist/commonjs/index.d.ts",
70+
"default": "./dist/commonjs/index.js"
71+
}
72+
}
73+
},
74+
"main": "./dist/commonjs/index.js",
75+
"types": "./dist/commonjs/index.d.ts"
5376
}

src/deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { existsSync, readdirSync } from 'node:fs';
3-
import { readJSONSync } from './utils';
3+
import { readJSONSync } from './utils.js';
44

55
/**
66
* Try to get framework dir path

src/framework.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path';
22
import assert from 'node:assert';
33
import { existsSync } from 'node:fs';
4-
import { readJSONSync } from './utils';
4+
import { readJSONSync } from './utils.js';
55

66
const initCwd = process.cwd();
77

src/import.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { debuglog } from 'node:util';
2+
import { createRequire } from 'node:module';
3+
import { pathToFileURL } from 'node:url';
4+
5+
const debug = debuglog('@eggjs/utils:loader');
6+
7+
let _customRequire: NodeRequire;
8+
9+
export interface ImportResolveOptions {
10+
paths?: string[];
11+
}
12+
13+
export interface ImportModuleOptions extends ImportResolveOptions {
14+
// only import export default object
15+
importDefaultOnly?: boolean;
16+
}
17+
18+
export function importResolve(filepath: string, options?: ImportResolveOptions) {
19+
if (!_customRequire) {
20+
if (typeof require !== 'undefined') {
21+
_customRequire = require;
22+
} else {
23+
_customRequire = createRequire(process.cwd());
24+
}
25+
}
26+
const moduleFilePath = _customRequire.resolve(filepath, options);
27+
debug('[importResolve] %o, options: %o => %o', filepath, options, moduleFilePath);
28+
return moduleFilePath;
29+
}
30+
31+
export async function importModule(filepath: string, options?: ImportModuleOptions) {
32+
const moduleFilePath = importResolve(filepath, options);
33+
let obj: any;
34+
if (typeof require === 'function') {
35+
// commonjs
36+
obj = require(moduleFilePath);
37+
debug('[importModule] require %o => %o', filepath, obj);
38+
if (obj?.__esModule === true && obj?.default) {
39+
// 兼容 cjs 模拟 esm 的导出格式
40+
// {
41+
// __esModule: true,
42+
// default: { fn: [Function: fn], foo: 'bar', one: 1 }
43+
// }
44+
obj = obj.default;
45+
}
46+
} else {
47+
// esm
48+
debug('[importModule] await import start: %o', filepath);
49+
const fileUrl = pathToFileURL(moduleFilePath).toString();
50+
obj = await import(fileUrl);
51+
debug('[importModule] await import end: %o => %o', filepath, obj);
52+
// {
53+
// default: { foo: 'bar', one: 1 },
54+
// foo: 'bar',
55+
// one: 1,
56+
// [Symbol(Symbol.toStringTag)]: 'Module'
57+
// }
58+
if (obj?.__esModule === true && obj?.default?.__esModule === true) {
59+
// 兼容 cjs 模拟 esm 的导出格式
60+
// {
61+
// __esModule: true,
62+
// default: {
63+
// __esModule: true,
64+
// default: {
65+
// fn: [Function: fn] { [length]: 0, [name]: 'fn' },
66+
// foo: 'bar',
67+
// one: 1
68+
// }
69+
// },
70+
// [Symbol(Symbol.toStringTag)]: 'Module'
71+
// }
72+
obj = obj.default;
73+
}
74+
if (options?.importDefaultOnly) {
75+
if (obj.default) {
76+
obj = obj.default;
77+
}
78+
}
79+
}
80+
debug('[importModule] return %o => %o', filepath, obj);
81+
return obj;
82+
}

src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { getFrameworkPath } from './framework';
2-
import { getPlugins, getConfig, getLoadUnits } from './plugin';
3-
import { getFrameworkOrEggPath } from './deprecated';
1+
import { getFrameworkPath } from './framework.js';
2+
import { getPlugins, getConfig, getLoadUnits } from './plugin.js';
3+
import { getFrameworkOrEggPath } from './deprecated.js';
44

55
// support import { getFrameworkPath } from '@eggjs/utils'
6-
export { getFrameworkPath } from './framework';
7-
export { getPlugins, getConfig, getLoadUnits } from './plugin';
8-
export { getFrameworkOrEggPath } from './deprecated';
6+
export { getFrameworkPath } from './framework.js';
7+
export { getPlugins, getConfig, getLoadUnits } from './plugin.js';
8+
export { getFrameworkOrEggPath } from './deprecated.js';
9+
export * from './import.js';
910

1011
// support import utils from '@eggjs/utils'
1112
export default {

0 commit comments

Comments
 (0)