From b7501572bd043bea1d5b8976a15f3c1e600e24df Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 4 Feb 2025 10:58:10 +0800 Subject: [PATCH] feat: support cjs and esm both by tshy (#27) BREAKING CHANGE: drop Node.js < 18.19.0 support part of https://github.com/eggjs/egg/issues/3644 https://github.com/eggjs/egg/issues/5257 ## Summary by CodeRabbit ## Release Notes - **Project Configuration** - Updated Node.js version support to 18, 20, and 22 - Updated TypeScript configuration to ES2022 and NodeNext module resolution - Updated minimum Node.js requirement to version 18 - Added MIT license - **Dependencies** - Upgraded several development dependencies - Updated testing and build tools to latest versions - **Documentation** - Added Contributors section to README - Updated project requirements documentation - **Maintenance** - Updated `.gitignore` to exclude `package-lock.json` - Removed pull request template - Introduced a new configuration file for unit tests --- .github/PULL_REQUEST_TEMPLATE.md | 24 -------------- .github/workflows/nodejs.yml | 3 +- .gitignore | 2 ++ README.md | 10 ++++++ boilerplate/README.md | 4 +-- boilerplate/_.gitignore | 2 ++ boilerplate/_package.json | 33 ++++++++++--------- boilerplate/app/module/bar/controller/home.ts | 2 +- boilerplate/app/module/bar/controller/user.ts | 2 +- boilerplate/app/module/foo/package.json | 2 +- .../app/module/foo/service/HelloService.ts | 2 +- boilerplate/config/config.default.ts | 9 ++++- boilerplate/config/config.local.ts | 2 +- boilerplate/config/config.prod.ts | 2 +- boilerplate/config/config.unittest.ts | 6 ++++ .../app/module/bar/controller/home.test.ts | 2 +- .../app/module/bar/controller/user.test.ts | 2 +- .../module/foo/service/HelloService.test.ts | 2 +- boilerplate/tsconfig.json | 3 ++ package.json | 12 ++++--- 20 files changed, 69 insertions(+), 57 deletions(-) delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 boilerplate/config/config.unittest.ts diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 48f9944..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,24 +0,0 @@ - - -##### Checklist - - -- [ ] `npm test` passes -- [ ] tests and/or benchmarks are included -- [ ] documentation is changed or added -- [ ] commit message follows commit guidelines - -##### Affected core subsystem(s) - - - -##### Description of change - diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 58f05ed..dc5563d 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -3,7 +3,6 @@ name: CI on: push: branches: [ master ] - pull_request: branches: [ master ] @@ -13,4 +12,4 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' - version: '16, 18, 20' + version: '18, 20, 22' diff --git a/.gitignore b/.gitignore index e4bd6a0..4735a4e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ logs run *.sw* *.un~ +package-lock.json +.package-lock.json diff --git a/README.md b/README.md index 3bc5e79..26d95e0 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,13 @@ open http://localhost:7001 ``` See [egg-init](https://github.com/eggjs/egg-init) for more detail. + +## License + +[MIT](LICENSE) + +## Contributors + +[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-boilerplate-ts)](https://github.com/eggjs/egg-boilerplate-ts/graphs/contributors) + +Made with [contributors-img](https://contrib.rocks). diff --git a/boilerplate/README.md b/boilerplate/README.md index a2b03ec..fce1bce 100644 --- a/boilerplate/README.md +++ b/boilerplate/README.md @@ -29,5 +29,5 @@ npm start ### Requirement -- Node.js >= 16.x -- Typescript >= 4.x +- Node.js >= 18.x +- Typescript >= 5.x diff --git a/boilerplate/_.gitignore b/boilerplate/_.gitignore index 5d0b9ec..bd951b0 100644 --- a/boilerplate/_.gitignore +++ b/boilerplate/_.gitignore @@ -18,3 +18,5 @@ config/**/*.js app/**/*.map test/**/*.map config/**/*.map +package-lock.json +.package-lock.json diff --git a/boilerplate/_package.json b/boilerplate/_package.json index 1714281..7657d67 100644 --- a/boilerplate/_package.json +++ b/boilerplate/_package.json @@ -7,41 +7,44 @@ "typescript": true }, "scripts": { - "start": "egg-scripts start --daemon --title=egg-server-{{name}}", - "stop": "egg-scripts stop --title=egg-server-{{name}}", + "start": "eggctl start --daemon --title=egg-server-{{name}}", + "stop": "eggctl stop --title=egg-server-{{name}}", "dev": "egg-bin dev", - "test:local": "egg-bin test -p", - "test": "npm run lint -- --fix && npm run test:local", - "cov": "egg-bin cov -p", - "ci": "npm run lint && npm run cov && npm run tsc && npm run clean", + "test:local": "egg-bin test", + "pretest": "npm run clean && npm run lint -- --fix", + "test": "egg-bin test", + "preci": "npm run clean && npm run lint", + "ci": "egg-bin cov", + "postci": "npm run prepublishOnly && npm start && npm stop && npm run clean", "lint": "eslint . --ext .ts --cache", "tsc": "tsc", - "clean": "tsc -b --clean" + "clean": "tsc -b --clean", + "prepublishOnly": "npm run clean && npm run tsc" }, "dependencies": { "@eggjs/tegg": "^3.5.2", "@eggjs/tegg-aop-plugin": "^3.5.2", - "@eggjs/tegg-config": "^3.2.3", + "@eggjs/tegg-config": "^3.5.2", "@eggjs/tegg-controller-plugin": "^3.5.2", "@eggjs/tegg-eventbus-plugin": "^3.5.2", "@eggjs/tegg-plugin": "^3.5.2", "@eggjs/tegg-schedule-plugin": "^3.5.2", - "egg": "^3.15.0", - "egg-scripts": "^2.17.0", + "egg": "^4.0.0", + "@eggjs/scripts": "^4.0.0", "egg-tracer": "^2.0.0" }, "devDependencies": { "@types/mocha": "10", - "@types/node": "20", + "@types/node": "22", "@eggjs/tsconfig": "1", - "egg-bin": "6", - "egg-mock": "5", + "@eggjs/bin": "7", + "@eggjs/mock": "6", "eslint": "8", - "eslint-config-egg": "13", + "eslint-config-egg": "14", "typescript": "5" }, "engines": { - "node": ">=20.10.0" + "node": ">=20.18.1" }, "repository": { "type": "git", diff --git a/boilerplate/app/module/bar/controller/home.ts b/boilerplate/app/module/bar/controller/home.ts index 7f71795..d3593ac 100644 --- a/boilerplate/app/module/bar/controller/home.ts +++ b/boilerplate/app/module/bar/controller/home.ts @@ -6,7 +6,7 @@ import { Inject, HTTPController, HTTPMethod, HTTPMethodEnum } from '@eggjs/tegg' }) export class HomeController { @Inject() - logger: EggLogger; + private logger: EggLogger; @HTTPMethod({ method: HTTPMethodEnum.GET, diff --git a/boilerplate/app/module/bar/controller/user.ts b/boilerplate/app/module/bar/controller/user.ts index e337b0c..ea516b2 100644 --- a/boilerplate/app/module/bar/controller/user.ts +++ b/boilerplate/app/module/bar/controller/user.ts @@ -6,7 +6,7 @@ import { HelloService } from '@/module/foo'; }) export class UserController { @Inject() - helloService: HelloService; + private helloService: HelloService; @HTTPMethod({ method: HTTPMethodEnum.GET, diff --git a/boilerplate/app/module/foo/package.json b/boilerplate/app/module/foo/package.json index 8eb94e2..738281a 100644 --- a/boilerplate/app/module/foo/package.json +++ b/boilerplate/app/module/foo/package.json @@ -3,4 +3,4 @@ "eggModule": { "name": "foo" } -} \ No newline at end of file +} diff --git a/boilerplate/app/module/foo/service/HelloService.ts b/boilerplate/app/module/foo/service/HelloService.ts index 8b9f25b..f5ab030 100644 --- a/boilerplate/app/module/foo/service/HelloService.ts +++ b/boilerplate/app/module/foo/service/HelloService.ts @@ -8,7 +8,7 @@ import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg'; export class HelloService { // 注入一个 logger @Inject() - logger: EggLogger; + private logger: EggLogger; // 封装业务 async hello(userId: string): Promise { diff --git a/boilerplate/config/config.default.ts b/boilerplate/config/config.default.ts index 2140d52..1529f79 100644 --- a/boilerplate/config/config.default.ts +++ b/boilerplate/config/config.default.ts @@ -10,7 +10,14 @@ export default (appInfo: EggAppInfo) => { // add your egg config in here config.middleware = []; + // change multipart mode to file + // @see https://github.com/eggjs/multipart/blob/master/src/config/config.default.ts#L104 + config.multipart = { + mode: 'file', + }; + // add your special config in here + // Usage: `app.config.bizConfig.sourceUrl` const bizConfig = { sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`, }; @@ -18,6 +25,6 @@ export default (appInfo: EggAppInfo) => { // the return config will combines to EggAppConfig return { ...config, - ...bizConfig, + bizConfig, }; }; diff --git a/boilerplate/config/config.local.ts b/boilerplate/config/config.local.ts index 56415ce..3c733a0 100644 --- a/boilerplate/config/config.local.ts +++ b/boilerplate/config/config.local.ts @@ -1,6 +1,6 @@ import { EggAppConfig, PowerPartial } from 'egg'; export default () => { - const config: PowerPartial = {}; + const config = {} as PowerPartial; return config; }; diff --git a/boilerplate/config/config.prod.ts b/boilerplate/config/config.prod.ts index 56415ce..3c733a0 100644 --- a/boilerplate/config/config.prod.ts +++ b/boilerplate/config/config.prod.ts @@ -1,6 +1,6 @@ import { EggAppConfig, PowerPartial } from 'egg'; export default () => { - const config: PowerPartial = {}; + const config = {} as PowerPartial; return config; }; diff --git a/boilerplate/config/config.unittest.ts b/boilerplate/config/config.unittest.ts new file mode 100644 index 0000000..3c733a0 --- /dev/null +++ b/boilerplate/config/config.unittest.ts @@ -0,0 +1,6 @@ +import { EggAppConfig, PowerPartial } from 'egg'; + +export default () => { + const config = {} as PowerPartial; + return config; +}; diff --git a/boilerplate/test/app/module/bar/controller/home.test.ts b/boilerplate/test/app/module/bar/controller/home.test.ts index 4737ddd..4c524ab 100644 --- a/boilerplate/test/app/module/bar/controller/home.test.ts +++ b/boilerplate/test/app/module/bar/controller/home.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import { app } from 'egg-mock/bootstrap'; +import { app } from '@eggjs/mock/bootstrap'; describe('test/app/module/bar/controller/home.test.ts', () => { it('should GET /', async () => { diff --git a/boilerplate/test/app/module/bar/controller/user.test.ts b/boilerplate/test/app/module/bar/controller/user.test.ts index 7b35a3a..e0d8593 100644 --- a/boilerplate/test/app/module/bar/controller/user.test.ts +++ b/boilerplate/test/app/module/bar/controller/user.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import { app } from 'egg-mock/bootstrap'; +import { app } from '@eggjs/mock/bootstrap'; describe('test/app/module/bar/controller/user.test.ts', () => { it('should GET /', async () => { diff --git a/boilerplate/test/app/module/foo/service/HelloService.test.ts b/boilerplate/test/app/module/foo/service/HelloService.test.ts index 6f90237..bdf727d 100644 --- a/boilerplate/test/app/module/foo/service/HelloService.test.ts +++ b/boilerplate/test/app/module/foo/service/HelloService.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import { app } from 'egg-mock/bootstrap'; +import { app } from '@eggjs/mock/bootstrap'; import { HelloService } from '@/module/foo/service/HelloService'; describe('test/app/module/foo/service/HelloService.test.js', () => { diff --git a/boilerplate/tsconfig.json b/boilerplate/tsconfig.json index fb6dbb7..78be519 100644 --- a/boilerplate/tsconfig.json +++ b/boilerplate/tsconfig.json @@ -1,6 +1,9 @@ { "extends": "@eggjs/tsconfig", "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", "declaration": false, "paths": { "@/module/*": ["app/module/*"] diff --git a/package.json b/package.json index cf127a3..6738696 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,13 @@ "test": "npm run lint -- --fix && npm run test-boilerplate", "ci": "npm run lint && npm run ci-boilerplate", "test-boilerplate": "npm run build && npm run install-deps && cd dist && npm test", + "test-boilerplate-from-cnpm": "npm run build && npm run install-deps-from-cnpm && cd dist && npm test", "ci-boilerplate": "npm run build && npm run install-deps && cd dist && npm run ci", + "ci-boilerplate-from-cnpm": "npm run build && npm run install-deps-from-cnpm && cd dist && npm run ci", "clean": "rm -rf dist", "build": "npm run clean && egg-init dist --force --silent --template=./", - "install-deps": "cd dist && npm i" + "install-deps": "cd dist && npm i", + "install-deps-from-cnpm": "cd dist && cnpm i" }, "homepage": "https://github.com/eggjs/egg-boilerplate-ts", "bugs": "https://github.com/eggjs/egg/issues", @@ -21,7 +24,8 @@ "author": "TZ ", "devDependencies": { "egg-init": "^3.0.2", - "eslint": "^8.34.0", - "eslint-config-egg": "13" - } + "eslint": "8", + "eslint-config-egg": "14" + }, + "license": "MIT" }