Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fengmk2 opened this issue Sep 14, 2023 · 15 comments
Open

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

fengmk2 opened this issue Sep 14, 2023 · 15 comments
Assignees

Comments

@fengmk2
Copy link
Member

fengmk2 commented Sep 14, 2023

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

node-modules/urllib#468 目前看起来没有什么问题,非常轻松就支持了。

修改内容

  • package.json
  "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

[![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);
}
  • .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": {
    "strict": true,
    "noImplicitAny": true,
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}
  • .eslintrc
{
  "extends": [
    "eslint-config-egg/typescript"
  ]
}
  • .gitignore
.tshy*
.eslintcache
dist
coverage
@fengmk2 fengmk2 self-assigned this Sep 14, 2023
@fengmk2
Copy link
Member Author

fengmk2 commented Sep 15, 2023

node-modules/is-type-of#22
eggjs/egg-bin#239 egg-bin 需要支持 esm 跑 test 和 cov

@fengmk2
Copy link
Member Author

fengmk2 commented Sep 16, 2023

@fengmk2
Copy link
Member Author

fengmk2 commented Sep 17, 2023

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

@fengmk2
Copy link
Member Author

fengmk2 commented Sep 22, 2023

node-modules/address#37

fengmk2 added a commit to node-modules/node-homedir that referenced this issue Oct 5, 2023
BREAKING CHANGE: Drop Node.js < 16 support

eggjs/egg#5257
@fengmk2
Copy link
Member Author

fengmk2 commented Oct 5, 2023

fengmk2 added a commit to node-modules/node-homedir that referenced this issue Oct 5, 2023
BREAKING CHANGE: Drop Node.js < 16 support

eggjs/egg#5257
fengmk2 pushed a commit to node-modules/node-homedir that referenced this issue Oct 5, 2023
[skip ci]

## [2.0.0](v1.1.1...v2.0.0) (2023-10-05)

### ⚠ BREAKING CHANGES

* Drop Node.js < 16 support

eggjs/egg#5257

### Features

* refactor with typescript ([#7](#7)) ([9e2a9ff](9e2a9ff))
@fengmk2
Copy link
Member Author

fengmk2 commented Oct 5, 2023

fengmk2 added a commit to node-modules/oss-client that referenced this issue Oct 5, 2023
BREAKING CHANGE: Drop Node.js < 16 support

Other BREAKING changes:
- remove stsToken support
- remove headerEncoding support
- remove Bucket, Image Client support

eggjs/egg#5257
fengmk2 pushed a commit to node-modules/oss-client that referenced this issue Oct 5, 2023
[skip ci]

## [2.0.0](v1.2.6...v2.0.0) (2023-10-05)

### ⚠ BREAKING CHANGES

* Drop Node.js < 16 support

Other BREAKING changes:
- remove stsToken support
- remove headerEncoding support
- remove Bucket, Image Client support

eggjs/egg#5257

### Features

* refactor with typescript ([#12](#12)) ([5a0eb01](5a0eb01))
@fengmk2
Copy link
Member Author

fengmk2 commented Oct 10, 2023

fengmk2 added a commit to node-modules/get-ready that referenced this issue Oct 10, 2023
fengmk2 added a commit to node-modules/get-ready that referenced this issue Oct 10, 2023
fengmk2 pushed a commit to node-modules/ready-callback that referenced this issue Oct 11, 2023
BREAKING CHANGE: Drop Node.js < 16 support

closes #116

part of eggjs/egg#5257

---------

Co-authored-by: hanquliu <[email protected]>
fengmk2 pushed a commit to node-modules/ready-callback that referenced this issue Oct 11, 2023
[skip ci]

## [4.0.0](v3.0.0...v4.0.0) (2023-10-11)

### ⚠ BREAKING CHANGES

* Drop Node.js < 16 support

closes #116

part of eggjs/egg#5257

### Features

* refactor with typescript to support esm and cjs both ([#117](#117)) ([7193ec1](7193ec1))
@fengmk2 fengmk2 pinned this issue Oct 14, 2023
fengmk2 added a commit to eggjs/egg-cors that referenced this issue Dec 11, 2023
BREAKING CHANGE: drop Node.js < 14

deps: use @koa/[email protected]

eggjs/egg#5257
fengmk2 added a commit to eggjs/egg-cors that referenced this issue Dec 11, 2023
BREAKING CHANGE: drop Node.js < 16

deps: use @koa/[email protected]

eggjs/egg#5257
fengmk2 added a commit to eggjs/egg-cors that referenced this issue Dec 11, 2023
BREAKING CHANGE: drop Node.js < 16

deps: use @koa/[email protected]

eggjs/egg#5257
fengmk2 added a commit to eggjs/egg-cors that referenced this issue Dec 11, 2023
BREAKING CHANGE: drop Node.js < 14

eggjs/egg#5257
fengmk2 pushed a commit to eggjs/egg-path-matching that referenced this issue Jun 15, 2024
[skip ci]

## [2.0.0](v1.1.0...v2.0.0) (2024-06-15)

### ⚠ BREAKING CHANGES

* drop Node.js < 18.19.0 support

eggjs/egg#5257

### Features

* support cjs and esm both ([#8](#8)) ([a092108](a092108))
fengmk2 added a commit to node-modules/performance-ms that referenced this issue Jun 16, 2024
fengmk2 added a commit to node-modules/performance-ms that referenced this issue Jun 16, 2024
eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Chores**
- Updated workflow configurations to include Node.js version 22 and
added `CODECOV_TOKEN`.
	- Removed `checkTest` parameter from the release workflow.
	- Updated `.gitignore` to exclude `.tshy*` and `dist` directories.

- **New Features**
- Significant updates to `package.json` including script modifications,
dependency updates, and new export configurations.

- **Tests**
	- Updated import paths and added a loop in test cases.

- **Refactor**
- Updated TypeScript configuration for stricter type checking and modern
module resolution.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
fengmk2 added a commit to eggjs/egg-utils that referenced this issue Jun 17, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

eggjs/egg#5257
fengmk2 added a commit to eggjs/egg-utils that referenced this issue Jun 17, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced new utility functions for resolving and importing modules
with support for CommonJS and ESM formats.
- Added new test fixtures for CommonJS and ESM modules to validate
module import functionality.

- **Refactor**
- Updated import statements to include file extensions (`.js`) for
consistency and compatibility.
	- Refactored code to use async/await for asynchronous operations.
	- Improved path handling in tests with helper functions.

- **Documentation**
- Updated `package.json` with new scripts, dependencies, and module
management configurations.

- **Chores**
- Enhanced `.gitignore` to exclude `.tshy*` files and `dist/` directory.
	- Modified GitHub Actions workflows for Node.js and release processes.

- **Tests**
	- Added tests for new module import functions.
	- Updated existing tests to reflect new import paths and async changes.

- **Configuration**
- Updated `tsconfig.json` for stricter TypeScript settings and modern
module resolution.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
fengmk2 pushed a commit to eggjs/egg-utils that referenced this issue Jun 17, 2024
[skip ci]

## [4.0.0](v3.0.1...v4.0.0) (2024-06-17)

### ⚠ BREAKING CHANGES

* drop Node.js < 18.19.0 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced new utility functions for resolving and importing modules
with support for CommonJS and ESM formats.
- Added new test fixtures for CommonJS and ESM modules to validate
module import functionality.

- **Refactor**
- Updated import statements to include file extensions (`.js`) for
consistency and compatibility.
	- Refactored code to use async/await for asynchronous operations.
	- Improved path handling in tests with helper functions.

- **Documentation**
- Updated `package.json` with new scripts, dependencies, and module
management configurations.

- **Chores**
- Enhanced `.gitignore` to exclude `.tshy*` files and `dist/` directory.
	- Modified GitHub Actions workflows for Node.js and release processes.

- **Tests**
	- Added tests for new module import functions.
	- Updated existing tests to reflect new import paths and async changes.

- **Configuration**
- Updated `tsconfig.json` for stricter TypeScript settings and modern
module resolution.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

### Features

* support @eggjs/core next version ([#21](#21)) ([a37968c](a37968c))
@fengmk2
Copy link
Member Author

fengmk2 commented Jun 18, 2024

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

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

fengmk2 pushed a commit to node-modules/address that referenced this issue Jun 19, 2024
[skip ci]

## [2.0.3](v2.0.2...v2.0.3) (2024-06-19)

### Bug Fixes

* use @arethetypeswrong/cli to auto add source exports ([#43](#43)) ([9ded1af](9ded1af)), closes [/github.com/eggjs/egg/issues/5257#issuecomment-2176524639](https://github.com/node-modules//github.com/eggjs/egg/issues/5257/issues/issuecomment-2176524639)
fengmk2 added a commit to node-modules/ylru that referenced this issue Jun 22, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

eggjs/egg#5257
fengmk2 added a commit to node-modules/ylru that referenced this issue Jun 22, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a Least Recently Used (LRU) cache implementation with
enhanced functionalities.

- **Bug Fixes**
  - Improved cache item expiration handling and test case accuracy.

- **Documentation**
  - Updated `README` with new import syntax and method signatures.
  - LICENSE changed to MIT License.

- **Chores**
- Updated ESLint configuration, Node.js version in workflows, and
`.gitignore`.

- **Refactor**
- Converted test cases to use async/await and updated module imports to
ES module syntax.

- **Dependencies**
  - Updated `devDependencies` and added new scripts in `package.json`.

- **Build Configuration**
  - Updated `tsconfig.json` for stricter typing and ES2022 target.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
fengmk2 pushed a commit to node-modules/ylru that referenced this issue Jun 22, 2024
[skip ci]

## [2.0.0](v1.4.0...v2.0.0) (2024-06-22)

### ⚠ BREAKING CHANGES

* drop Node.js < 18.19.0 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a Least Recently Used (LRU) cache implementation with
enhanced functionalities.

- **Bug Fixes**
  - Improved cache item expiration handling and test case accuracy.

- **Documentation**
  - Updated `README` with new import syntax and method signatures.
  - LICENSE changed to MIT License.

- **Chores**
- Updated ESLint configuration, Node.js version in workflows, and
`.gitignore`.

- **Refactor**
- Converted test cases to use async/await and updated module imports to
ES module syntax.

- **Dependencies**
  - Updated `devDependencies` and added new scripts in `package.json`.

- **Build Configuration**
  - Updated `tsconfig.json` for stricter typing and ES2022 target.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

### Features

* support cjs and esm both ([#13](#13)) ([73e07bf](73e07bf))
fengmk2 added a commit to eggjs/koa-override that referenced this issue Jun 23, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
fengmk2 added a commit to eggjs/koa-override that referenced this issue Jun 23, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
fengmk2 pushed a commit to eggjs/koa-override that referenced this issue Jun 23, 2024
[skip ci]

## [4.0.0](v3.0.0...v4.0.0) (2024-06-23)

### ⚠ BREAKING CHANGES

* drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257

### Features

* support cjs and esm both by tshy ([#7](#7)) ([f021708](f021708))
fengmk2 added a commit to node-modules/sendmessage that referenced this issue Jun 23, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
fengmk2 added a commit to node-modules/sendmessage that referenced this issue Jun 23, 2024
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
fengmk2 pushed a commit to node-modules/sendmessage that referenced this issue Jun 23, 2024
[skip ci]

## [3.0.0](v2.0.0...v3.0.0) (2024-06-23)

### ⚠ BREAKING CHANGES

* drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257

### Features

* support cjs and esm both by tshy ([#6](#6)) ([30acc65](30acc65))
@fengmk2
Copy link
Member Author

fengmk2 commented Jul 7, 2024

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

node-modules/tshy-after#1

@fengmk2
Copy link
Member Author

fengmk2 commented Jul 8, 2024

支持自动复制 web 相关文件
node-modules/tshy-after#2

@zhangvj
Copy link

zhangvj commented Jul 16, 2024

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

@fengmk2
Copy link
Member Author

fengmk2 commented Jul 16, 2024

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

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

@fengmk2
Copy link
Member Author

fengmk2 commented Sep 28, 2024

nodejs/node#55085

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

No branches or pull requests

2 participants