Skip to content

Commit

Permalink
feat: support @eggjs/core next version
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

eggjs/egg#5257
  • Loading branch information
fengmk2 committed Jun 17, 2024
1 parent 3ceccab commit bc4239f
Show file tree
Hide file tree
Showing 24 changed files with 378 additions and 133 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

workflow_dispatch: {}

jobs:
Job:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '16.13.0, 16, 18, 20'
version: '18.19.0, 18, 20, 22'
6 changes: 1 addition & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ on:
push:
branches: [ master ]

workflow_dispatch: {}

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ test/fixtures/**/*.yml
.nyc_output/
test/fixtures/tmp/
lib/
.tshy*
dist/
86 changes: 53 additions & 33 deletions package.json
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",
Expand All @@ -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"
}
2 changes: 1 addition & 1 deletion src/deprecated.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { existsSync, readdirSync } from 'node:fs';
import { readJSONSync } from './utils';
import { readJSONSync } from './utils.js';

/**
* Try to get framework dir path
Expand Down
2 changes: 1 addition & 1 deletion src/framework.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import assert from 'node:assert';
import { existsSync } from 'node:fs';
import { readJSONSync } from './utils';
import { readJSONSync } from './utils.js';

const initCwd = process.cwd();

Expand Down
82 changes: 82 additions & 0 deletions src/import.ts
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;
}
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getFrameworkPath } from './framework';
import { getPlugins, getConfig, getLoadUnits } from './plugin';
import { getFrameworkOrEggPath } from './deprecated';
import { getFrameworkPath } from './framework.js';
import { getPlugins, getConfig, getLoadUnits } from './plugin.js';
import { getFrameworkOrEggPath } from './deprecated.js';

// support import { getFrameworkPath } from '@eggjs/utils'
export { getFrameworkPath } from './framework';
export { getPlugins, getConfig, getLoadUnits } from './plugin';
export { getFrameworkOrEggPath } from './deprecated';
export { getFrameworkPath } from './framework.js';
export { getPlugins, getConfig, getLoadUnits } from './plugin.js';
export { getFrameworkOrEggPath } from './deprecated.js';
export * from './import.js';

// support import utils from '@eggjs/utils'
export default {
Expand Down
Loading

0 comments on commit bc4239f

Please sign in to comment.