Skip to content

Commit

Permalink
chore(build): process is undefined in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
fzn0x committed Jun 18, 2024
1 parent fceceff commit 90fcd30
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-var */
declare module globalThis {
declare module global {
var abortController: AbortController
var abortSignal: AbortSignal
}
8 changes: 4 additions & 4 deletions src/utils/create-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ export const createRequest: RequestFunction = async (

if (isAbortControllerSupported) {
// Expose the AbortController instance
globalThis.abortController = new AbortController()
global.abortController = new AbortController()

// Use the external AbortController instance
globalThis.abortSignal = signal ? signal : globalThis.abortController.signal
global.abortSignal = signal ? signal : global.abortController.signal
}

const timeoutId =
timeout && isAbortControllerSupported
? setTimeout(() => {
globalThis.abortController.abort()
global.abortController.abort()

// Execute post-timeout hook
if (hooks?.postTimeout) {
Expand Down Expand Up @@ -121,7 +121,7 @@ export const createRequest: RequestFunction = async (

const requestOptions = {
method,
signal: isAbortControllerSupported ? globalThis.abortSignal : null,
signal: isAbortControllerSupported ? global.abortSignal : null,
headers: reqHeaders,
...otherOptions,
body: requestBody,
Expand Down
3 changes: 1 addition & 2 deletions src/utils/get-abort-controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isAbortControllerSupported } from './constant.js'

// Expose the AbortController instance through the library interface
export const getAbortController = () =>
isAbortControllerSupported ? globalThis.abortController : null
export const getAbortController = () => (isAbortControllerSupported ? global.abortController : null)
27 changes: 15 additions & 12 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
// @ts-check
const path = require("path");
const path = require('path')

module.exports = /** @type { import('webpack').Configuration } */ {
mode: "production",
entry: "./src/index.ts",
devtool: "source-map",
mode: 'production',
entry: './src/index.ts',
devtool: 'source-map',
output: {
library: {
name: "hypf",
type: "umd",
name: 'hypf',
type: 'umd',
umdNamedDefine: true,
},
path: path.resolve(__dirname, "browser"),
filename: "hyperfetch-browser.min.js",
path: path.resolve(__dirname, 'browser'),
filename: 'hyperfetch-browser.min.js',
},
externals: {
'node:process': '{}',
},
resolve: {
extensionAlias: {
".js": [".js", ".ts"],
'.js': ['.js', '.ts'],
},
extensions: [".ts", ".js"],
extensions: ['.ts', '.js'],
},
module: {
rules: [
// all files with a `.ts`, `.cts`, `.mts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.([cm]?ts|tsx)$/, loader: "ts-loader" },
{ test: /\.([cm]?ts|tsx)$/, loader: 'ts-loader' },
],
},
};
}

0 comments on commit 90fcd30

Please sign in to comment.