Skip to content

chore: esm/cjs publishing #5

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

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <alex@achingbrain.net>",
"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 <alex@achingbrain.net>"
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"
]
}