Skip to content

使用 tshy 来默认支持 cjs 和 esm #5257

@fengmk2

Description

@fengmk2

请详细告知你的新点子(Nice Ideas):

node-modules/urllib#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
[![Node.js Version](https://img.shields.io/node/v/{pkgName}.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)
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/{group}/{repo})
  • contributors
## Contributors

[![Contributors](https://contrib.rocks/image?repo={group/repo})](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);
  }
}

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions