Skip to content

Commit

Permalink
feat: dual tsc builds trying to address microsoft/TypeScript#49462
Browse files Browse the repository at this point in the history
  • Loading branch information
knightedcodemonkey committed Jul 28, 2023
0 parents commit d6738cb
Show file tree
Hide file tree
Showing 26 changed files with 5,578 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,25 @@
{
"env": {
"es6": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:n/recommended"
],
"parserOptions": {
"ecmaVersion": 2023,
"sourceType": "module"
},
"rules": {
"no-console": "error",
"n/shebang": [
"error",
{
"convertPath": {
"src/*.js": ["^src/(.+)$", "dist/$1"]
}
}
]
}
}
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,39 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
ci:
name: CI
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
- name: Setup Node
uses: actions/setup-node@v3.6.0
with:
node-version: '20.5.0'
- name: Install Dependencies
run: npm ci
- name: Save error log
uses: actions/upload-artifact@v2
if: ${{ failure() }}
with:
name: npm-debug-log-${{ hashFiles('package-lock.json') }}
path: npm-debug.log
- name: Lint
run: npm run lint
- name: Test
run: npm test
- name: Report Coverage
uses: codecov/codecov-action@v3.1.4
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Pack
run: npm pack
36 changes: 36 additions & 0 deletions .github/workflows/publish.yml
@@ -0,0 +1,36 @@
name: Publish

on:
release:
types: [published]

jobs:
publish:
if: contains('["knightedcodemonkey"]', github.actor)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3.5.2
- name: Setup Node
uses: actions/setup-node@v3.6.0
with:
node-version: '20.5.0'
- name: Install Dependencies
run: npm ci
- name: Save error log
uses: actions/upload-artifact@v2
if: ${{ failure() }}
with:
name: npm-debug-log-${{ hashFiles('package-lock.json') }}
path: npm-debug.log
- name: Lint
run: npm run lint
- name: Test
run: npm test
- name: Pack
run: npm pack
- name: Push to NPM registry
uses: JS-DevTools/npm-publish@v2.1.0
with:
token: ${{ secrets.NPM_AUTH_TOKEN }}
access: public
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.tgz
dist
node_modules
coverage
42 changes: 42 additions & 0 deletions README.md
@@ -0,0 +1,42 @@
# [`@knighted/duel`](https://www.npmjs.com/package/@knighted/duel)

![CI](https://github.com/knightedcodemonkey/duel/actions/workflows/ci.yml/badge.svg)
[![NPM version](https://img.shields.io/npm/v/@knighted/duel.svg)](https://www.npmjs.com/package/@knighted/duel)

Node.js tool for creating a TypeScript dual package.

Early stages of development. Inspired by https://github.com/microsoft/TypeScript/issues/49462.

## Example

Consider a project that is ESM-first, i.e. `"type": "module"` in package.json, that also wants to create a separate CJS build. It might have a tsconfig.json file that looks like the following.

**tsconfig.json**

```json
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"strict": true,
"outDir": "dist"
},
"include": ["src/*.ts"]
}
```

Running the following will use the tsconfig.json defined above and create a separate CJS build in `dist/cjs`.

```console
user@comp ~ $ duel -p tsconfig.json -x .cjs
```

Now you can update your `exports` in package.json to match the build output.

It should work similarly for a CJS first project. Except, your tsconfig.json would be slightly different and you'd want to pass `-x .mjs`.

## Gotchas

Unfortunately, TypeScript doesn't really understand dual packages very well. For instance, it will **always** create CJS exports when `--module commonjs` is used, even on files with an `.mts` extension. One reference issue is https://github.com/microsoft/TypeScript/issues/54573.
8 changes: 8 additions & 0 deletions babel.config.json
@@ -0,0 +1,8 @@
{
"targets": "node >= 16.19",
"presets": [
["@babel/preset-env", {
"modules": false
}]
]
}
10 changes: 10 additions & 0 deletions codecov.yml
@@ -0,0 +1,10 @@
coverage:
status:
project:
default:
target: 80.0
threshold: 0.5
patch:
default:
target: 80.0
threshold: 1.0

0 comments on commit d6738cb

Please sign in to comment.