eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.9k stars 1.82k forks source link

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

Open fengmk2 opened 1 year ago

fengmk2 commented 1 year ago

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

https://github.com/node-modules/urllib/pull/468 目前看起来没有什么问题,非常轻松就支持了。

修改内容

  "engines": {
    "node": ">= 18.19.0"
  },
  "devDependencies": {
    "@arethetypeswrong/cli": "^0.15.3",
    "@eggjs/tsconfig": "1",
    "@types/node": "20",
    "@types/mocha": "10",
    "egg-bin": "6",
    "eslint": "8",
    "eslint-config-egg": "14",
    "tshy": "2",
    "tshy-after": "1",
    "typescript": "5",
  },
  "scripts": {
    "lint": "eslint --cache src test --ext .ts",
    "test": "npm run lint -- --fix && egg-bin test",
    "ci": "npm run lint && egg-bin cov && npm run prepublishOnly && attw --pack",
    "prepublishOnly": "tshy && tshy-after"
  },
  "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"
## Contributors

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

Made with [contributors-img](https://contrib.rocks).
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

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);
}

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`
```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 }}

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`
```json
{
  "extends": "@eggjs/tsconfig",
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}
.tshy*
.eslintcache
dist
coverage
fengmk2 commented 1 year ago

https://github.com/node-modules/is-type-of/pull/22 https://github.com/eggjs/egg-bin/pull/239 egg-bin 需要支持 esm 跑 test 和 cov

fengmk2 commented 1 year ago

https://github.com/node-modules/oss-interface/pull/7

fengmk2 commented 1 year ago

通过 https://github.com/node-modules/tshy-after 保留 package.json types 配置。

fengmk2 commented 1 year ago

https://github.com/node-modules/address/pull/37

fengmk2 commented 1 year ago

https://github.com/node-modules/node-homedir/pull/7

fengmk2 commented 1 year ago

https://github.com/node-modules/oss-client/pull/12

fengmk2 commented 1 year ago

https://github.com/node-modules/ready-callback/pull/117

fengmk2 commented 10 months ago

https://github.com/node-modules/utility/pull/60

fengmk2 commented 5 months ago
 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);
fengmk2 commented 4 months ago

https://johnnyreilly.com/dual-publishing-esm-cjs-modules-with-tsup-and-are-the-types-wrong

通过 @arethetypeswrong/cli 来检查配置是否正确

fengmk2 commented 4 months ago

通过字符串替换解决 import.meta 在 commonjs 中的语法错误问题。

https://github.com/node-modules/tshy-after/pull/1

fengmk2 commented 4 months ago

支持自动复制 web 相关文件 https://github.com/node-modules/tshy-after/pull/2

zhangvj commented 3 months ago

大佬,目前egg.js有什么办法可以在让应用不使用ts的情况下支持esm吗

fengmk2 commented 3 months ago

大佬,目前egg.js有什么办法可以在让应用不使用ts的情况下支持esm吗

目前 egg 发布的 npm 包只支持 cjs 产物。需要等 egg v4 发布之后才会同时支持 cjs 和 esm 构建产物。

fengmk2 commented 1 month ago

https://github.com/nodejs/node/pull/55085

可能不需要了,如果 nodejs 20 就能支持 cjs 里面 require esm,那么我们就不需要编译出2份产物了。