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

Only support egg >= 4.0.0

part of eggjs/egg#3644

eggjs/egg#5257
  • Loading branch information
fengmk2 committed Feb 4, 2025
1 parent f396e5f commit f9ab4e4
Show file tree
Hide file tree
Showing 41 changed files with 159 additions and 187 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/codeql.yml

This file was deleted.

5 changes: 1 addition & 4 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: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '14, 16, 18, 20'
version: '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: node-modules/github-actions/.github/workflows/node-release.yml@master
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,9 @@ JS demo: <https://github.com/whxaxes/egg-boilerplate-d-js>
## License

[MIT](LICENSE)

## Contributors

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

Made with [contributors-img](https://contrib.rocks).
6 changes: 6 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,9 @@ JS 项目: <https://github.com/whxaxes/egg-boilerplate-d-js>
## License

[MIT](LICENSE)

## Contributors

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

Made with [contributors-img](https://contrib.rocks).
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"lint": "eslint . --ext .ts",
"check": "npm run tsc && npm run lint",
"test": "npm run check && npm run test-local",
"test-local": "egg-bin test --ts",
"test-local": "egg-bin test",
"prepublishOnly": "del dist && npm run tsc",
"cov": "egg-bin cov --ts",
"cov": "egg-bin cov",
"ci": "npm run check && npm run cov && npm run tsc"
},
"keywords": [
Expand All @@ -48,6 +48,8 @@
"yn": "^3.0.0"
},
"devDependencies": {
"@eggjs/bin": "^7.0.4",
"@eggjs/mock": "^6.0.5",
"@eggjs/tsconfig": "^1.0.0",
"@types/commander": "^2.12.2",
"@types/del": "^3.0.0",
Expand All @@ -56,16 +58,13 @@
"@types/node": "^20.4.5",
"del": "^3.0.0",
"del-cli": "^1.1.0",
"egg": "^3.5.0",
"egg-bin": "^6.4.1",
"egg-mock": "^5.2.1",
"egg": "^4.0.7",
"egg-sequelize": "^4.3.1",
"eslint": "^8.28.0",
"eslint-config-egg": "^12.1.0",
"extend2": "^1.0.0",
"runscript": "^1.3.0"
"eslint-config-egg": "14",
"extend2": "^1.0.0"
},
"engines": {
"node": ">= 14.21.3"
"node": ">= 18.19.0"
}
}
4 changes: 2 additions & 2 deletions src/cmd/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { prompt } from 'enquirer';
import * as utils from '../utils';
import path from 'path';
import fs from 'fs';
import path from 'node:path';
import fs from 'node:fs';
import { createTsHelperInstance } from '../';

const TYPE_TS = 'typescript';
Expand Down
5 changes: 2 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import path from 'path';
import path from 'node:path';
import { Command } from 'commander';
import assert from 'assert';
import assert from 'node:assert';
import TsHelper, { defaultConfig } from './core';
import { loadModules, writeJsConfig, checkMaybeIsJsProj, getPkgInfo } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import { getPkgInfo } from './utils';

const root = path.dirname(__dirname);
Expand Down
10 changes: 5 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import chokidar from 'chokidar';
import assert from 'assert';
import { EventEmitter } from 'events';
import fs from 'fs';
import crypto from 'crypto';
import assert from 'node:assert';
import { EventEmitter } from 'node:events';
import fs from 'node:fs';
import crypto from 'node:crypto';
import chalk from 'chalk';
import path from 'path';
import path from 'node:path';
import * as generator from './generator';
import { get as deepGet, set as deepSet } from 'dot-prop';
import { declMapping, dtsComment, dtsCommentRE } from './config';
Expand Down
5 changes: 3 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import ObjectGenerator from './generators/object';
import PluginGenerator from './generators/plugin';
import { BaseGenerator } from './generators/base';
import * as utils from './utils';
import path from 'path';
import assert = require('assert');
import path from 'node:path';
import assert from 'node:assert';

type GeneratorKlass = { new (...args: any[]): BaseGenerator };

export const generators = {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { debuglog } from 'util';
import path from 'path';
import { debuglog } from 'node:util';
import path from 'node:path';
import { TsGenConfig, TsHelperConfig } from '..';
import * as utils from '../utils';

Expand Down
4 changes: 2 additions & 2 deletions src/generators/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import ts from 'typescript';
import { TsGenConfig } from '..';
import { declMapping } from '../config';
Expand Down
2 changes: 1 addition & 1 deletion src/generators/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { default as TsHelper, TsGenConfig, TsHelperConfig } from '../core';
import { declMapping } from '../config';
import * as utils from '../utils';
import path from 'path';
import path from 'node:path';

const customWatcherName = 'custom';
const customSpecRef = `${customWatcherName}_spec_ref`;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/egg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TsGenConfig, TsHelperConfig } from '..';
import path from 'path';
import path from 'node:path';

// declare global namespace Egg
export default function EggGenerator(config: TsGenConfig, baseConfig: TsHelperConfig) {
Expand Down
6 changes: 3 additions & 3 deletions src/generators/extend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debuglog } from 'util';
import fs from 'fs';
import path from 'path';
import { debuglog } from 'node:util';
import fs from 'node:fs';
import path from 'node:path';
import * as utils from '../utils';
import { declMapping } from '../config';
import { GeneratorResult, TsGenConfig, TsHelperConfig } from '..';
Expand Down
2 changes: 1 addition & 1 deletion src/generators/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import { declMapping } from '../config';
import { TsGenConfig, TsHelperConfig } from '..';
import * as utils from '../utils';
Expand Down
4 changes: 2 additions & 2 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cluster from 'cluster';
import { debuglog } from 'util';
import cluster from 'node:cluster';
import { debuglog } from 'node:util';
import TsHelper, { TsHelperOption } from './core';
import {
convertString, checkMaybeIsJsProj, writeJsConfig, cleanJs,
Expand Down
80 changes: 42 additions & 38 deletions src/scripts/eggInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Getting plugin info in child_process to prevent effecting egg application( splitting scopes ).
*/

import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import { eggInfoPath } from '../config';
import * as utils from '../utils';

Expand All @@ -21,43 +21,45 @@ if (utils.checkMaybeIsTsProj(cwd)) {
}
}

// try to read postinstall script env.ETS_SCRIPT_FRAMEWORK, let egg-bin can auto set the default framework
const framework = (utils.getPkgInfo(cwd).egg || {}).framework || process.env.ETS_SCRIPT_FRAMEWORK || 'egg';
const loader = getLoader(cwd, framework);
if (loader) {
try {
loader.loadPlugin();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadPlugin() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
}
async function main() {
// try to read postinstall script env.ETS_SCRIPT_FRAMEWORK, let egg-bin can auto set the default framework
const framework = (utils.getPkgInfo(cwd).egg || {}).framework || process.env.ETS_SCRIPT_FRAMEWORK || 'egg';
const loader = getLoader(cwd, framework);
if (loader) {
try {
await loader.loadPlugin();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadPlugin() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
}

// hack loadFile, ignore config file without customLoader for faster booting
mockFn(loader, 'loadFile', (filepath: string) => {
if (filepath && filepath.substring(filepath.lastIndexOf(path.sep) + 1).startsWith('config.')) {
const fileContent = fs.readFileSync(filepath, 'utf-8');
if (!fileContent.includes('customLoader')) return;
}
return true;
});

// hack loadFile, ignore config file without customLoader for faster booting
mockFn(loader, 'loadFile', filepath => {
if (filepath && filepath.substring(filepath.lastIndexOf(path.sep) + 1).startsWith('config.')) {
const fileContent = fs.readFileSync(filepath, 'utf-8');
if (!fileContent.includes('customLoader')) return;
try {
await loader.loadConfig();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadConfig() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
}
return true;
});

try {
loader.loadConfig();
} catch (e) {
console.warn('[egg-ts-helper] WARN loader.loadConfig() error: %s, cwd: %s, framework: %s',
e, cwd, framework);
// do nothing
eggInfo.plugins = loader.allPlugins;
eggInfo.config = loader.config;
eggInfo.eggPaths = loader.eggPaths;
eggInfo.timing = Date.now() - startTime;
}

eggInfo.plugins = loader.allPlugins;
eggInfo.config = loader.config;
eggInfo.eggPaths = loader.eggPaths;
eggInfo.timing = Date.now() - startTime;
utils.writeFileSync(eggInfoPath, JSON.stringify(eggInfo));
}

utils.writeFileSync(eggInfoPath, JSON.stringify(eggInfo));

/* istanbul ignore next */
function noop() {}

Expand All @@ -81,7 +83,7 @@ function getLoader(baseDir: string, framework: string) {
}
const eggCore = findEggCore(baseDir, frameworkPath);
if (!eggCore) {
console.warn('[egg-ts-helper] WARN cannot find egg core from frameworkPath: %s', frameworkPath);
console.warn('[egg-ts-helper] WARN cannot find @eggjs/core from frameworkPath: %s', frameworkPath);
return;
}
const EggLoader = eggCore.EggLoader;
Expand All @@ -100,24 +102,26 @@ function getLoader(baseDir: string, framework: string) {
});
}

function findEggCore(baseDir: string, frameworkPath: string) {
function findEggCore(baseDir: string, frameworkPath: string, eggCorePkgName = '@eggjs/core') {
let eggCorePath = '';
try {
eggCorePath = require.resolve('egg-core', { paths: [ frameworkPath ] });
eggCorePath = require.resolve(eggCorePkgName, { paths: [ frameworkPath ] });
} catch (_) {
// ignore error
}
if (!eggCorePath) {
eggCorePath = path.join(baseDir, 'node_modules/egg-core');
eggCorePath = path.join(baseDir, 'node_modules', eggCorePkgName);
if (!fs.existsSync(eggCorePath)) {
eggCorePath = path.join(frameworkPath, 'node_modules/egg-core');
eggCorePath = path.join(frameworkPath, 'node_modules', eggCorePkgName);
}
}
// try to load egg-core in cwd
const eggCore = utils.requireFile(eggCorePath);
if (!eggCore) {
// try to resolve egg-core
return utils.requireFile('egg-core');
return utils.requireFile(eggCorePkgName);
}
return eggCore;
}

main();
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import fs from 'node:fs';
import glob from 'globby';
import path from 'path';
import path from 'node:path';
import ts from 'typescript';
import yn from 'yn';
import { execFileSync, execFile, ExecFileOptions } from 'child_process';
import { execFileSync, execFile, ExecFileOptions } from 'node:child_process';
import JSON5 from 'json5';
import { eggInfoPath, tmpDir } from './config';

Expand Down
8 changes: 4 additions & 4 deletions src/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path';
import path from 'node:path';
import chokidar from 'chokidar';
import assert from 'assert';
import { EventEmitter } from 'events';
import { debuglog } from 'util';
import assert from 'node:assert';
import { EventEmitter } from 'node:events';
import { debuglog } from 'node:util';
import { TsGenerator, TsGenConfig, TsHelperConfig, default as TsHelper } from './core';
import * as utils from './utils';
import { loadGenerator } from './generator';
Expand Down
Loading

0 comments on commit f9ab4e4

Please sign in to comment.