Skip to content

Commit c4c2ab1

Browse files
authored
fix: support vite (#193)
Doing the require in this fashion seems to work better with vite.
1 parent c07baa8 commit c4c2ab1

9 files changed

+53
-12
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"./src/http/fetch.js": "./src/http/fetch.browser.js",
1616
"./src/temp-dir.js": "./src/temp-dir.browser.js",
1717
"./src/path-join.js": "./src/path-join.browser.js",
18+
"./src/fetch.js": "./src/fetch.browser.js",
1819
"./src/files/glob-source.js": false,
1920
"./test/files/glob-source.spec.js": false,
2021
"electron-fetch": false,

src/env.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const IS_ELECTRON = isElectron()
77
const IS_BROWSER = IS_ENV_WITH_DOM && !IS_ELECTRON
88
const IS_ELECTRON_MAIN = IS_ELECTRON && !IS_ENV_WITH_DOM
99
const IS_ELECTRON_RENDERER = IS_ELECTRON && IS_ENV_WITH_DOM
10-
const IS_NODE = typeof require === 'function' && typeof globalThis.process !== 'undefined' && typeof globalThis.process.release !== 'undefined' && globalThis.process.release.name === 'node' && !IS_ELECTRON
10+
const IS_NODE = typeof require === 'function' && typeof process !== 'undefined' && typeof process.release !== 'undefined' && process.release.name === 'node' && !IS_ELECTRON
1111
// @ts-ignore - we either ignore worker scope or dom scope
1212
const IS_WEBWORKER = typeof importScripts === 'function' && typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope
13-
const IS_TEST = typeof globalThis.process !== 'undefined' && typeof globalThis.process.env !== 'undefined' && globalThis.process.env.NODE_ENV === 'test'
13+
const IS_TEST = typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.NODE_ENV === 'test'
1414
const IS_REACT_NATIVE = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
1515

1616
module.exports = {

src/fetch.browser.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
/**
4+
* @typedef {globalThis.Headers} Headers
5+
* @typedef {globalThis.Request} Request
6+
* @typedef {globalThis.Response} Response
7+
*/
8+
9+
// use window.fetch if it is available, fall back to node-fetch if not
10+
module.exports = require('native-fetch')

src/fetch.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
'use strict'
22

3+
/**
4+
* @typedef {globalThis.Headers} Headers
5+
* @typedef {globalThis.Request} Request
6+
* @typedef {globalThis.Response} Response
7+
*/
8+
39
const { isElectronMain } = require('./env')
410

5-
if (isElectronMain) {
6-
module.exports = require('electron-fetch')
7-
} else {
811
// use window.fetch if it is available, fall back to node-fetch if not
9-
module.exports = require('native-fetch')
12+
let impl = 'native-fetch'
13+
14+
if (isElectronMain) {
15+
impl = 'electron-fetch'
1016
}
17+
18+
module.exports = require(impl)

src/http.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ class HTTP {
9191
async fetch (resource, options = {}) {
9292
/** @type {HTTPOptions} */
9393
const opts = merge(this.opts, options)
94+
// @ts-expect-error
9495
const headers = new Headers(opts.headers)
9596

9697
// validate resource type
98+
// @ts-expect-error
9799
if (typeof resource !== 'string' && !(resource instanceof URL || resource instanceof Request)) {
98100
throw new TypeError('`resource` must be a string, URL, or Request')
99101
}
@@ -125,12 +127,15 @@ class HTTP {
125127
// @ts-ignore
126128
const signal = anySignal([abortController.signal, opts.signal])
127129

130+
/** @type {ExtendedResponse} */
131+
// @ts-expect-error additional fields are assigned below
128132
const response = await timeout(
129133
fetch(
130134
url.toString(),
131135
{
132136
...opts,
133137
signal,
138+
// @ts-expect-error non-browser fetch implementations may take extra options
134139
timeout: undefined,
135140
headers
136141
}
@@ -146,8 +151,8 @@ class HTTP {
146151
throw new HTTPError(response)
147152
}
148153

149-
response.iterator = function () {
150-
return fromStream(response.body)
154+
response.iterator = async function * () {
155+
yield * fromStream(response.body)
151156
}
152157

153158
response.ndjson = async function * () {

src/http/fetch.browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const { TimeoutError, AbortError } = require('./error')
4+
// @ts-expect-error
45
const { Response, Request, Headers, default: fetch } = require('../fetch')
56

67
/**

src/http/fetch.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
'use strict'
22

3+
/**
4+
* @typedef {object} fetchImpl
5+
* @property {globalThis.fetch} fetchImpl.fetch
6+
* @property {globalThis.Request} fetchImpl.Request
7+
* @property {globalThis.Response} fetchImpl.Response
8+
* @property {globalThis.Headers} fetchImpl.Headers
9+
*/
10+
11+
let implName = './fetch.node'
12+
313
if (typeof XMLHttpRequest === 'function') {
414
// Electron has `XMLHttpRequest` and should get the browser implementation
515
// instead of node.
6-
module.exports = require('./fetch.browser')
7-
} else {
8-
module.exports = require('./fetch.node')
16+
implName = './fetch.browser'
917
}
18+
19+
/** @type {fetchImpl} */
20+
const fetch = require(implName)
21+
22+
module.exports = fetch

src/http/fetch.node.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ const withUploadProgress = (options) => {
3333
// and `ArrayBuffer`s to strings.
3434
const content = normalizeBody(body)
3535

36+
// @ts-expect-error this is node-fetch
3637
const rsp = new Response(content)
38+
// @ts-expect-error this is node-fetch
3739
const source = iterateBodyWithProgress(/** @type {NodeReadableStream} */(rsp.body), onUploadProgress)
3840
return {
3941
...options,

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "aegir/src/config/tsconfig.aegir.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"emitDeclarationOnly": true
56
},
67
"include": [
78
"test",

0 commit comments

Comments
 (0)