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 3, 2025
1 parent 571b851 commit 2e5a9ff
Show file tree
Hide file tree
Showing 24 changed files with 270 additions and 70 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"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest'
version: '14, 16, 18, 20, 22'
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).
19 changes: 0 additions & 19 deletions config/config.default.js

This file was deleted.

86 changes: 62 additions & 24 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": {
"@eggjs/core": "^6.2.11",
"debounce": "^1.1.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"
}
10 changes: 5 additions & 5 deletions agent.js → src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('node:path');
const fs = require('node:fs/promises');
const debounce = require('debounce');
const multimatch = require('multimatch');
const { exists } = require('utility');
import path from 'node:path';
import fs from 'node:fs/promises';
import debounce from 'debounce';
import multimatch from 'multimatch';
import { exists } from 'utility';

module.exports = agent => {
// clean all timing json
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const path = require('node:path');
const fs = require('node:fs/promises');
const { readJSON } = require('utility');
import path from 'node:path';
import fs from 'node:fs/promises';
import { readJSON } from 'utility';
import type { EggCore, MiddlewareFunc } from '@eggjs/core';

module.exports = (_, app) => {
export default (_: unknown, app: EggCore): MiddlewareFunc => {
return async (ctx, next) => {
if (ctx.path !== '/__loader_trace__') return await next();
if (ctx.path !== '/__loader_trace__') {
return await next();
}
const template = await fs.readFile(path.join(__dirname, '../../lib/loader_trace.html'), 'utf8');
const data = await loadTimingData(app);
ctx.body = template.replace('{{placeholder}}', JSON.stringify(data));
};
};

async function loadTimingData(app) {
async function loadTimingData(app: EggCore) {
const rundir = app.config.rundir;
const files = await fs.readdir(rundir);
const data = [];
const data: any[] = [];
for (const file of files) {
if (!/^(agent|application)_timing/.test(file)) continue;
const json = await readJSON(path.join(rundir, file));
Expand Down
52 changes: 52 additions & 0 deletions src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export interface DevelopmentConfig {
/**
* dirs needed watch, when files under these change, application will reload, use relative path
*/
watchDirs: string[];
/**
* dirs don't need watch, including subdirectories, use relative path
*/
ignoreDirs: string[];
/**
* don't wait all plugins ready, default is false.
*/
fastReady: boolean;
/**
* whether reload on debug, default is true.
*/
reloadOnDebug: boolean;
/**
* whether override default watchDirs, default is false.
*/
overrideDefault: boolean;
/**
* whether override default ignoreDirs, default is false.
*/
overrideIgnore: boolean;
/**
* whether to reload, use https://github.com/sindresorhus/multimatch
*/
reloadPattern?: string[] | string;
}

/**
* @member Config#development
* @property {Array} watchDirs - dirs needed watch, when files under these change, application will reload, use relative path
* @property {Array} ignoreDirs - dirs don't need watch, including subdirectories, use relative path
* @property {Boolean} fastReady - don't wait all plugins ready, default is false.
* @property {Boolean} reloadOnDebug - whether reload on debug, default is true.
* @property {Boolean} overrideDefault - whether override default watchDirs, default is false.
* @property {Boolean} overrideIgnore - whether override default ignoreDirs, default is false.
* @property {Array|String} reloadPattern - whether to reload, use https://github.com/sindresorhus/multimatch
*/
export default {
development: {
watchDirs: [],
ignoreDirs: [],
fastReady: false,
reloadOnDebug: true,
overrideDefault: false,
overrideIgnore: false,
reloadPattern: undefined,
} as DevelopmentConfig,
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './types.js';
File renamed without changes.
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { DevelopmentConfig } from './config/config.default.js';

declare module '@eggjs/core' {
// add EggAppConfig overrides types
interface EggAppConfig {
development: DevelopmentConfig;
}
}
58 changes: 58 additions & 0 deletions test/development-ts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from 'node:fs/promises';
import { strict as assert } from 'node:assert';
import { scheduler } from 'node:timers/promises';
import { mm, MockApplication } from '@eggjs/mock';
import { escape, getFilepath } from './utils.js';

describe('test/development-ts.test.ts', () => {
let app: MockApplication;
before(() => {
mm.env('local');
app = mm.cluster({
baseDir: 'development-ts',
});
return app.ready();
});
after(() => app.close());
afterEach(mm.restore);
// for debounce
afterEach(() => scheduler.wait(500));

it('should reload when change service', async () => {
const filepath = getFilepath('development-ts/app/service/a.ts');
await fs.writeFile(filepath, '');
await scheduler.wait(5000);

await fs.unlink(filepath);
app.expect('stdout', new RegExp(escape(`reload worker because ${filepath}`)));
});

it('should not reload when change assets', async () => {
const filepath = getFilepath('development-ts/app/assets/b.js');
await fs.writeFile(filepath, '');
await scheduler.wait(5000);

await fs.unlink(filepath);
app.notExpect('stdout', new RegExp(escape(`reload worker because ${filepath}`)));
});

it('should reload once when 2 file change', async () => {
const filepath = getFilepath('development-ts/app/service/c.js');
const filepath1 = getFilepath('development-ts/app/service/d.js');
await fs.writeFile(filepath, '');
// set a timeout for watcher's interval
await scheduler.wait(1000);
await fs.writeFile(filepath1, '');

await scheduler.wait(2000);
await fs.unlink(filepath);
await fs.unlink(filepath1);

assert.equal(count(app.stdout, 'reload worker'), 2);
});
});

function count(str: string, match: string) {
const m = str.match(new RegExp(match, 'g'));
return m ? m.length : 0;
}
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/development-ts/app/public/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('bar');
11 changes: 11 additions & 0 deletions test/fixtures/development-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Application } from 'egg';

export default (app: Application) => {
app.get('/foo.js', async ctx => {
ctx.body = 'foo.js';
});

app.get('/foo', async ctx => {
ctx.body = 'foo';
});
};
Empty file.
10 changes: 10 additions & 0 deletions test/fixtures/development-ts/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../../../../src/index.js';

import { EggAppConfig } from 'egg';

export default {
keys: 'foo,bar',
development: {
fastReady: false,
},
} as EggAppConfig;
4 changes: 4 additions & 0 deletions test/fixtures/development-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "development-ts",
"type": "module"
}
10 changes: 10 additions & 0 deletions test/fixtures/development-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}
Loading

0 comments on commit 2e5a9ff

Please sign in to comment.