Skip to content

Commit

Permalink
chore: esm/cjs publishing (#5)
Browse files Browse the repository at this point in the history
Publish both esm and cjs.

Also, default types to browser fetch.
  • Loading branch information
achingbrain authored Dec 13, 2021
1 parent 141068d commit cbe0f25
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 43 deletions.
File renamed without changes.
17 changes: 11 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,45 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: [14, 15]
node: [16]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npx xvfb-maybe npm test
- run: npm run pretest
- run: npm run test:node
test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe npm test
- run: npm run pretest
- run: npx xvfb-maybe npm run test:browser
test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe npm test
- run: npm run pretest
- run: npx xvfb-maybe npm run test:browser -- -- --browser firefox
test-electron-main:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe npm test
- run: npm run pretest
- run: npx xvfb-maybe npm run test:electron-main
test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe npm test
- run: npm run pretest
- run: npx xvfb-maybe npm run test:electron-renderer
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ package-lock.json
.DS_Store
dist
docs
*.tsbuildinfo
types
52 changes: 41 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,48 @@
"name": "native-fetch",
"version": "4.0.0",
"description": "Returns native fetch if available or the undici module if not",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"type": "module",
"files": [
"src",
"dist"
"*",
"!**/*.tsbuildinfo"
],
"publishConfig": {
"directory": "dist"
},
"exports": {
".": {
"browser": "./src/index.js",
"import": "./src/index-node.js"
}
},
"types": "./types/index.d.ts",
"typesVersions": {
"*": {
"src/*": [
"types/src/*",
"types/src/*/index"
]
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"scripts": {
"test": "aegir test -t node -t browser -t webworker -t electron-main -t electron-renderer",
"lint": "aegir lint && aegir ts -p check",
"build": "aegir build --no-bundle",
"release": "aegir release --docs false",
"release-minor": "aegir release --type minor --docs false",
"release-major": "aegir release --type major --docs false"
"pretest": "npm run build",
"test": "npm run test:node && npm run test:browser && npm run test:electron-main && npm run test:electron-renderer",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"test:electron-main": "aegir build --esm-tests && aegir test -t electron-main -f ./dist/cjs/node-test/**/*.spec.js",
"test:electron-renderer": "aegir build --esm-tests && aegir test -t electron-renderer -f ./dist/cjs/browser-test/**/*.spec.js",
"lint": "aegir ts -p check && aegir lint",
"release": "aegir release --docs",
"release-minor": "aegir release --target node --type minor --docs",
"release-major": "aegir release --type major --docs",
"build": "aegir build",
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js"
},
"author": "Alex Potsides <[email protected]>",
"license": "MIT",
Expand All @@ -27,7 +56,8 @@
},
"devDependencies": {
"aegir": "^36.0.0",
"undici": "^4.10.0"
"undici": "^4.10.0",
"native-fetch": "./dist"
},
"contributors": [
"achingbrain <[email protected]>"
Expand Down
6 changes: 6 additions & 0 deletions src/index-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as undici from 'undici'

export const fetch = globalThis.fetch ? globalThis.fetch : undici.fetch
export const Headers = globalThis.Headers ? globalThis.Headers : undici.Headers
export const Request = globalThis.Request ? globalThis.Request : undici.Request
export const Response = globalThis.Response ? globalThis.Response : undici.Response
25 changes: 8 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
'use strict'
const globalFetch = globalThis.fetch
const globalHeaders = globalThis.Headers
const globalRequest = globalThis.Request
const globalResponse = globalThis.Response

// @ts-expect-error globalThis.fetch is defined according to the env types but it's not in node
if (globalThis.fetch && globalThis.Headers && globalThis.Request && globalThis.Response) {
module.exports = {
fetch: globalThis.fetch,
Headers: globalThis.Headers,
Request: globalThis.Request,
Response: globalThis.Response
}
} else {
module.exports = {
fetch: require('undici').fetch,
Headers: require('undici').Headers,
Request: require('undici').Request,
Response: require('undici').Response
}
}
export { globalFetch as fetch }
export { globalHeaders as Headers }
export { globalRequest as Request }
export { globalResponse as Response }
11 changes: 5 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const NativeFetch = require('../src')
const Undici = require('undici')

describe('env', function () {
import { expect } from 'aegir/utils/chai.js'
import * as NativeFetch from 'native-fetch'
import * as Undici from 'undici'

describe(`env ${process.env.AEGIR_RUNNER}`, function () {
it('fetch should be correct in each env', function () {
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
Expand Down
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "types"
},
"include": [
"src",
"test"
"src"
],
"exclude": [
"dist"
]
}

0 comments on commit cbe0f25

Please sign in to comment.