Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
  • Loading branch information
fengmk2 committed Jan 7, 2025
1 parent 571b851 commit a0750e7
Show file tree
Hide file tree
Showing 42 changed files with 649 additions and 421 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '14, 16, 18, 20, 22'
os: 'macos-latest'
version: '18.19.0, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ node_modules/
coverage/
test/fixtures/**/run
.DS_Store
.tshy*
.eslintcache
dist
package-lock.json
.package-lock.json
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# egg-development
# @eggjs/development

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/eggjs/egg-development/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-development/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/egg-development.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/egg-development.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-development
Expand Down Expand Up @@ -75,6 +76,6 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).

## Contributors

[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-development)](https://github.com/eggjs/egg-development/graphs/contributors)
[![Contributors](https://contrib.rocks/image?repo=eggjs/development)](https://github.com/eggjs/development/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
96 changes: 0 additions & 96 deletions agent.js

This file was deleted.

7 changes: 0 additions & 7 deletions app.js

This file was deleted.

88 changes: 63 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "egg-development",
"name": "@eggjs/development",
"version": "3.0.2",
"publishConfig": {
"access": "public"
},
"description": "development tool for egg",
"eggPlugin": {
"name": "development",
Expand All @@ -9,7 +12,12 @@
],
"dependencies": [
"watcher"
]
],
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs",
"typescript": "./src"
}
},
"keywords": [
"egg",
Expand All @@ -18,42 +26,72 @@
"eggPlugin"
],
"dependencies": {
"debounce": "^1.1.0",
"@eggjs/core": "^6.2.11",
"debounce": "^2.2.0",
"multimatch": "^5.0.0",
"utility": "^2.4.0"
},
"devDependencies": {
"@types/node": "^22.10.2",
"egg": "3",
"egg-bin": "6",
"egg-mock": "5",
"@arethetypeswrong/cli": "^0.17.2",
"@eggjs/bin": "7",
"@eggjs/mock": "6",
"@eggjs/supertest": "8",
"@eggjs/tsconfig": "1",
"@types/mocha": "10",
"@types/node": "22",
"egg": "beta",
"eslint": "8",
"eslint-config-egg": "12",
"supertest": "^3.4.2"
"eslint-config-egg": "14",
"rimraf": "^6.0.1",
"tshy": "3",
"tshy-after": "1",
"typescript": "5"
},
"engines": {
"node": ">=14.0.0"
"node": ">=18.19.0"
},
"scripts": {
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test --ts false",
"cov": "egg-bin cov --ts false",
"lint": "eslint .",
"ci": "npm run lint && npm run cov"
"lint": "eslint --cache src test --ext .ts",
"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 run clean",
"clean": "rimraf dist",
"prepublishOnly": "tshy && tshy-after && attw --pack"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-development.git"
"url": "git+https://github.com/eggjs/development.git"
},
"files": [
"app",
"config",
"lib",
"agent.js",
"app.js"
],
"bugs": "https://github.com/eggjs/egg/issues",
"homepage": "https://github.com/eggjs/egg-development#readme",
"homepage": "https://github.com/eggjs/development#readme",
"author": "jtyjty99999",
"license": "MIT"
"license": "MIT",
"type": "module",
"tshy": {
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
}
},
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./package.json": "./package.json"
},
"files": [
"dist",
"src"
],
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js"
}
106 changes: 106 additions & 0 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import debounce from 'debounce';
import multimatch from 'multimatch';
import { exists } from 'utility';
import type { ILifecycleBoot, EggCore } from '@eggjs/core';
import { isTimingFile } from './utils.js';

export default class AgentBoot implements ILifecycleBoot {
#agent: EggCore;

constructor(agent: EggCore) {
this.#agent = agent;
}

async didLoad() {
// clean all timing json
const rundir = this.#agent.config.rundir;
const stat = await exists(rundir);
if (!stat) return;
const files = await fs.readdir(rundir);
for (const file of files) {
if (!isTimingFile(file)) continue;
await fs.rm(path.join(rundir, file), { force: true, recursive: true });
}
}

async serverDidReady() {
const agent = this.#agent;
// single process mode don't watch and reload
if (agent.options && Reflect.get(agent.options, 'mode') === 'single') {
return;
}

const logger = agent.logger;
const baseDir = agent.config.baseDir;
const config = agent.config.development;

let watchDirs = config.overrideDefault ? [] : [
'app',
'config',
'mocks',
'mocks_proxy',
'app.js',
];

watchDirs = watchDirs.concat(config.watchDirs).map(dir => path.resolve(baseDir, dir));

let ignoreReloadFileDirs = config.overrideIgnore ? [] : [
'app/views',
'app/view',
'app/assets',
'app/public',
'app/web',
];

ignoreReloadFileDirs = ignoreReloadFileDirs.concat(config.ignoreDirs).map(dir => path.resolve(baseDir, dir));

const reloadFile = debounce(function(info) {
logger.warn(`[agent:development] reload worker because ${info.path} ${info.event}`);

process.send!({
to: 'master',
action: 'reload-worker',
});
}, 200);

// watch dirs to reload worker, will debounce 200ms
/**
* reload app worker:
* [AgentWorker] - on file change
* |-> emit reload-worker
* [Master] - receive reload-worker event
* |-> TODO: Mark worker will die
* |-> Fork new worker
* |-> kill old worker
*
* @param {Object} info - changed fileInfo
*/
agent.watcher.watch(watchDirs, info => {
if (!config.reloadOnDebug) {
return;
}

if (isAssetsDir(info.path) || info.isDirectory) {
return;
}

// don't reload if don't match
if (config.reloadPattern && multimatch(info.path, config.reloadPattern).length === 0) {
return;
}

reloadFile(info);
});

function isAssetsDir(filepath: string) {
for (const ignorePath of ignoreReloadFileDirs) {
if (filepath.startsWith(ignorePath)) {
return true;
}
}
return false;
}
}
}
17 changes: 17 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { ILifecycleBoot, EggCore } from '@eggjs/core';

export default class AppBoot implements ILifecycleBoot {
#app: EggCore;

constructor(app: EggCore) {
this.#app = app;
// if true, then don't need to wait at local development mode
if (app.config.development.fastReady) {
process.nextTick(() => this.#app.ready(true));
}
}

async configWillLoad() {
this.#app.config.coreMiddleware.push('eggLoaderTrace');
}
}
Loading

0 comments on commit a0750e7

Please sign in to comment.