使用 tshy 来默认支持 cjs 和 esm
请详细告知你的新点子(Nice Ideas):
https://github.com/node-modules/urllib/pull/468 目前看起来没有什么问题,非常轻松就支持了。
还可以发布到 jsr https://jsr.io/@eggjs/read-env-value
修改内容
- package.json
"engines": {
"node": ">= 18.19.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.18.2",
"@eggjs/tsconfig": "2",
"@types/node": "22",
"@types/mocha": "10",
"@eggjs/bin": "7",
"husky": "9",
"lint-staged": "15",
"oxlint": "1",
"prettier": "3",
"rimraf": "6",
"tshy": "3",
"tshy-after": "1",
"typescript": "5"
},
"scripts": {
"lint": "oxlint",
"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",
"prepare": "husky"
},
"lint-staged": {
"*": "prettier --write --ignore-unknown --cache",
"*.{ts,js,json,md,yml}": [
"prettier --ignore-unknown --write",
"oxlint --fix"
]
},
"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",
"module": "./dist/esm/index.js"
.oxlintrc.json, .prettierrc, .prettierignore, .husky/pre-commit
https://gist.github.com/fengmk2/e984a0e91a719837fd459b290455689a
初始化 husky 和 prettier
npx husky init
npx prettier --write .
https://github.com/oxc-project/oxlint-migrate
插件配置
"eggPlugin": {
"name": "development",
"env": [
"local"
],
"dependencies": [
"watcher"
],
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs",
"typescript": "./src"
}
},
- pr welcome
[](https://nodejs.org/en/download/)
[](https://makeapullrequest.com)

- contributors
## Contributors
[](https://github.com/{group/repo}/graphs/contributors)
Made with [contributors-img](https://contrib.rocks).
- commit log
feat: support cjs and esm both by tshy
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
-
__dirname单测中的 helper 方法
test/helper.ts
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function getFixtures(filename: string) {
return path.join(__dirname, filename);
}
- 获取代码根目录
export function getSourceDirname() {
if (typeof __dirname === 'string') {
return __dirname;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const __filename = fileURLToPath(import.meta.url);
return path.dirname(__filename);
}
-
.github/workflows/nodejs.yml
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '18.19.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
-
.github/workflows/release.yml
name: Release
on:
push:
branches: [ master ]
jobs:
release:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
-
.github/workflows/pkg.pr.new.yml
name: Publish Any Commit
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm install
- name: Build
run: npm run prepublishOnly --if-present
- run: npx pkg-pr-new publish
-
tsconfig.json
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
}
}
-
.eslintrc
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
-
.gitignore
.tshy*
.eslintcache
dist
coverage
Sub Error
export class ClusterAgentWorkerError extends Error {
id: number;
/**
* pid in process mode
* tid in worker_threads mode
*/
workerId: number;
status: string;
constructor(id: number, workerId: number, status: string, error: Error) {
const message = `Got agent worker error: ${error.message}`;
super(message, { cause: error });
this.name = this.constructor.name;
this.id = id;
this.workerId = workerId;
this.status = status;
Error.captureStackTrace(this, this.constructor);
}
}
https://github.com/node-modules/is-type-of/pull/22 https://github.com/eggjs/egg-bin/pull/239 egg-bin 需要支持 esm 跑 test 和 cov
https://github.com/node-modules/oss-interface/pull/7
通过 https://github.com/node-modules/tshy-after 保留 package.json types 配置。
https://github.com/node-modules/address/pull/37
https://github.com/node-modules/node-homedir/pull/7
https://github.com/node-modules/oss-client/pull/12
https://github.com/node-modules/ready-callback/pull/117
https://github.com/node-modules/utility/pull/60
Exception during run: ReferenceError: __dirname is not defined in ES module scope
解决办法
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
https://johnnyreilly.com/dual-publishing-esm-cjs-modules-with-tsup-and-are-the-types-wrong
通过 @arethetypeswrong/cli 来检查配置是否正确
通过字符串替换解决 import.meta 在 commonjs 中的语法错误问题。
https://github.com/node-modules/tshy-after/pull/1
支持自动复制 web 相关文件 https://github.com/node-modules/tshy-after/pull/2
大佬,目前egg.js有什么办法可以在让应用不使用ts的情况下支持esm吗
大佬,目前egg.js有什么办法可以在让应用不使用ts的情况下支持esm吗
目前 egg 发布的 npm 包只支持 cjs 产物。需要等 egg v4 发布之后才会同时支持 cjs 和 esm 构建产物。