Skip to content

Commit cbe0f25

Browse files
authored
chore: esm/cjs publishing (#5)
Publish both esm and cjs. Also, default types to browser fetch.
1 parent 141068d commit cbe0f25

File tree

8 files changed

+78
-43
lines changed

8 files changed

+78
-43
lines changed

.aegir.js renamed to .aegir.cjs

File renamed without changes.

.github/workflows/main.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,45 @@ jobs:
2323
strategy:
2424
matrix:
2525
os: [ubuntu-latest]
26-
node: [14, 15]
26+
node: [16]
2727
fail-fast: true
2828
steps:
2929
- uses: actions/checkout@v2
3030
- uses: actions/setup-node@v1
3131
with:
3232
node-version: ${{ matrix.node }}
3333
- run: npm install
34-
- run: npx xvfb-maybe npm test
34+
- run: npm run pretest
35+
- run: npm run test:node
3536
test-chrome:
3637
needs: check
3738
runs-on: ubuntu-latest
3839
steps:
3940
- uses: actions/checkout@v2
4041
- run: npm install
41-
- run: npx xvfb-maybe npm test
42+
- run: npm run pretest
43+
- run: npx xvfb-maybe npm run test:browser
4244
test-firefox:
4345
needs: check
4446
runs-on: ubuntu-latest
4547
steps:
4648
- uses: actions/checkout@v2
4749
- run: npm install
48-
- run: npx xvfb-maybe npm test
50+
- run: npm run pretest
51+
- run: npx xvfb-maybe npm run test:browser -- -- --browser firefox
4952
test-electron-main:
5053
needs: check
5154
runs-on: ubuntu-latest
5255
steps:
5356
- uses: actions/checkout@v2
5457
- run: npm install
55-
- run: npx xvfb-maybe npm test
58+
- run: npm run pretest
59+
- run: npx xvfb-maybe npm run test:electron-main
5660
test-electron-renderer:
5761
needs: check
5862
runs-on: ubuntu-latest
5963
steps:
6064
- uses: actions/checkout@v2
6165
- run: npm install
62-
- run: npx xvfb-maybe npm test
66+
- run: npm run pretest
67+
- run: npx xvfb-maybe npm run test:electron-renderer

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ package-lock.json
44
.DS_Store
55
dist
66
docs
7+
*.tsbuildinfo
8+
types

package.json

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,48 @@
22
"name": "native-fetch",
33
"version": "4.0.0",
44
"description": "Returns native fetch if available or the undici module if not",
5-
"main": "src/index.js",
6-
"types": "dist/src/index.d.ts",
5+
"type": "module",
76
"files": [
8-
"src",
9-
"dist"
7+
"*",
8+
"!**/*.tsbuildinfo"
109
],
10+
"publishConfig": {
11+
"directory": "dist"
12+
},
13+
"exports": {
14+
".": {
15+
"browser": "./src/index.js",
16+
"import": "./src/index-node.js"
17+
}
18+
},
19+
"types": "./types/index.d.ts",
20+
"typesVersions": {
21+
"*": {
22+
"src/*": [
23+
"types/src/*",
24+
"types/src/*/index"
25+
]
26+
}
27+
},
28+
"eslintConfig": {
29+
"extends": "ipfs",
30+
"parserOptions": {
31+
"sourceType": "module"
32+
}
33+
},
1134
"scripts": {
12-
"test": "aegir test -t node -t browser -t webworker -t electron-main -t electron-renderer",
13-
"lint": "aegir lint && aegir ts -p check",
14-
"build": "aegir build --no-bundle",
15-
"release": "aegir release --docs false",
16-
"release-minor": "aegir release --type minor --docs false",
17-
"release-major": "aegir release --type major --docs false"
35+
"pretest": "npm run build",
36+
"test": "npm run test:node && npm run test:browser && npm run test:electron-main && npm run test:electron-renderer",
37+
"test:node": "aegir test -t node",
38+
"test:browser": "aegir test -t browser",
39+
"test:electron-main": "aegir build --esm-tests && aegir test -t electron-main -f ./dist/cjs/node-test/**/*.spec.js",
40+
"test:electron-renderer": "aegir build --esm-tests && aegir test -t electron-renderer -f ./dist/cjs/browser-test/**/*.spec.js",
41+
"lint": "aegir ts -p check && aegir lint",
42+
"release": "aegir release --docs",
43+
"release-minor": "aegir release --target node --type minor --docs",
44+
"release-major": "aegir release --type major --docs",
45+
"build": "aegir build",
46+
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js"
1847
},
1948
"author": "Alex Potsides <alex@achingbrain.net>",
2049
"license": "MIT",
@@ -27,7 +56,8 @@
2756
},
2857
"devDependencies": {
2958
"aegir": "^36.0.0",
30-
"undici": "^4.10.0"
59+
"undici": "^4.10.0",
60+
"native-fetch": "./dist"
3161
},
3262
"contributors": [
3363
"achingbrain <alex@achingbrain.net>"

src/index-node.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as undici from 'undici'
2+
3+
export const fetch = globalThis.fetch ? globalThis.fetch : undici.fetch
4+
export const Headers = globalThis.Headers ? globalThis.Headers : undici.Headers
5+
export const Request = globalThis.Request ? globalThis.Request : undici.Request
6+
export const Response = globalThis.Response ? globalThis.Response : undici.Response

src/index.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
'use strict'
1+
const globalFetch = globalThis.fetch
2+
const globalHeaders = globalThis.Headers
3+
const globalRequest = globalThis.Request
4+
const globalResponse = globalThis.Response
25

3-
// @ts-expect-error globalThis.fetch is defined according to the env types but it's not in node
4-
if (globalThis.fetch && globalThis.Headers && globalThis.Request && globalThis.Response) {
5-
module.exports = {
6-
fetch: globalThis.fetch,
7-
Headers: globalThis.Headers,
8-
Request: globalThis.Request,
9-
Response: globalThis.Response
10-
}
11-
} else {
12-
module.exports = {
13-
fetch: require('undici').fetch,
14-
Headers: require('undici').Headers,
15-
Request: require('undici').Request,
16-
Response: require('undici').Response
17-
}
18-
}
6+
export { globalFetch as fetch }
7+
export { globalHeaders as Headers }
8+
export { globalRequest as Request }
9+
export { globalResponse as Response }

test/index.spec.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
'use strict'
2-
31
/* eslint-env mocha */
4-
const { expect } = require('aegir/utils/chai')
5-
const NativeFetch = require('../src')
6-
const Undici = require('undici')
72

8-
describe('env', function () {
3+
import { expect } from 'aegir/utils/chai.js'
4+
import * as NativeFetch from 'native-fetch'
5+
import * as Undici from 'undici'
6+
7+
describe(`env ${process.env.AEGIR_RUNNER}`, function () {
98
it('fetch should be correct in each env', function () {
109
switch (process.env.AEGIR_RUNNER) {
1110
case 'electron-main':

tsconfig.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"extends": "aegir/src/config/tsconfig.aegir.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "types"
55
},
66
"include": [
7-
"src",
8-
"test"
7+
"src"
8+
],
9+
"exclude": [
10+
"dist"
911
]
1012
}

0 commit comments

Comments
 (0)