Skip to content
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

build: Mangle internal export names #1963

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions _internal/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const enum InternalUtils {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe explain why we need to have this as comment here, to help contributors understand better this is for size compression purpose

serialize,
useIsomorphicLayoutEffect,
UNDEFINED,
subscribeCallback,
revalidateEvents,
cache,
mutate,
isUndefined,
isFunction,
SWRConfig,
defaultConfig,
SWRGlobalState,
stableHash,
internalMutate,
normalize,
withArgs,
useStateWithDeps,
getTimestamp,
useSWRConfig,
withMiddleware,
IS_REACT_LEGACY,
IS_SERVER,
rAF,
OBJECT,
isEmptyCache,
mergeObjects,
createCacheHelper
}
78 changes: 58 additions & 20 deletions _internal/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
import SWRConfig from './utils/config-context'
import * as revalidateEvents from './constants'

export { SWRConfig, revalidateEvents }

export { initCache } from './utils/cache'
export { defaultConfig, cache, mutate, compare } from './utils/config'
export * from './utils/env'
export { SWRGlobalState } from './utils/global-state'
export { stableHash } from './utils/hash'
export * from './utils/helper'
export { mergeConfigs } from './utils/merge-config'
export { internalMutate } from './utils/mutate'
export { normalize } from './utils/normalize-args'
export { withArgs } from './utils/resolve-args'
export { serialize } from './utils/serialize'
export { useStateWithDeps } from './utils/state'
export { subscribeCallback } from './utils/subscribe-key'
export { getTimestamp } from './utils/timestamp'
export { useSWRConfig } from './utils/use-swr-config'
export { preset, defaultConfigOptions } from './utils/web-preset'
export { withMiddleware } from './utils/with-middleware'
import { subscribeCallback } from './utils/subscribe-key'
import { defaultConfig, cache, mutate } from './utils/config'
import { SWRGlobalState } from './utils/global-state'
import { stableHash } from './utils/hash'
import { internalMutate } from './utils/mutate'
import { normalize } from './utils/normalize-args'
import { withArgs } from './utils/resolve-args'
import { serialize } from './utils/serialize'
import { useStateWithDeps } from './utils/state'
import { getTimestamp } from './utils/timestamp'
import { useSWRConfig } from './utils/use-swr-config'
import { withMiddleware } from './utils/with-middleware'
import {
IS_REACT_LEGACY,
IS_SERVER,
rAF,
useIsomorphicLayoutEffect
} from './utils/env'
import {
UNDEFINED,
OBJECT,
isUndefined,
isFunction,
isEmptyCache,
mergeObjects,
createCacheHelper
} from './utils/helper'

export * from './types'
export { InternalUtils } from './enums'

export default [
serialize,
useIsomorphicLayoutEffect,
UNDEFINED,
subscribeCallback,
revalidateEvents,
cache,
mutate,
isUndefined,
isFunction,
SWRConfig,
defaultConfig,
SWRGlobalState,
stableHash,
internalMutate,
normalize,
withArgs,
useStateWithDeps,
getTimestamp,
useSWRConfig,
withMiddleware,
IS_REACT_LEGACY,
IS_SERVER,
rAF,
OBJECT,
isEmptyCache,
mergeObjects,
createCacheHelper
] as const
1 change: 0 additions & 1 deletion _internal/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useLayoutEffect } from 'react'
import { hasRequestAnimationFrame, isWindowDefined } from './helper'


export const IS_REACT_LEGACY = !React.useId

export const IS_SERVER = !isWindowDefined || 'Deno' in window
Expand Down
1 change: 1 addition & 0 deletions _internal/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SWRGlobalState } from './global-state'
import { Key, Cache, CacheValue, GlobalState } from '../types'

export const noop = () => {}

// Using noop() as the undefined value as undefined can possibly be replaced
Expand Down
5 changes: 3 additions & 2 deletions core/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-nocheck
shuding marked this conversation as resolved.
Show resolved Hide resolved

// useSWR
import useSWR from './use-swr'
export default useSWR

// Core APIs
export { SWRConfig, unstable_serialize } from './use-swr'
export { useSWRConfig } from 'swr/_internal'
export { mutate } from 'swr/_internal'
export { mutate, useSWRConfig } from './utils'

// Types
export type {
Expand Down
47 changes: 24 additions & 23 deletions core/use-swr.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import { useCallback, useRef, useDebugValue } from 'react'
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'

import type {
Key,
Fetcher,
GlobalState,
SWRConfiguration,
StateDependencies,
CacheValue,
State,
RevalidatorOptions,
SWRResponse,
RevalidateEvent,
FullConfiguration,
SWRHook
} from 'swr/_internal'
import {
defaultConfig,
IS_REACT_LEGACY,
IS_SERVER,
rAF,
useIsomorphicLayoutEffect,
SWRGlobalState,
GlobalState,
serialize,
createCacheHelper,
isUndefined,
isEmptyCache,
IS_REACT_LEGACY,
IS_SERVER,
UNDEFINED,
OBJECT,
getTimestamp,
isFunction,
createCacheHelper,
isEmptyCache,
SWRConfig as ConfigProvider,
withArgs,
subscribeCallback,
getTimestamp,
internalMutate,
revalidateEvents,
State,
Fetcher,
Key,
SWRResponse,
RevalidatorOptions,
FullConfiguration,
SWRConfiguration,
SWRHook,
RevalidateEvent,
CacheValue,
StateDependencies
} from 'swr/_internal'
useIsomorphicLayoutEffect,
subscribeCallback,
rAF,
ConfigProvider
} from './utils'

const WITH_DEDUPE = { dedupe: true }

Expand Down
71 changes: 71 additions & 0 deletions core/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import utils, { InternalUtils } from 'swr/_internal'

const [
defaultConfig,
SWRGlobalState,
serialize,
createCacheHelper,
isEmptyCache,
IS_REACT_LEGACY,
IS_SERVER,
UNDEFINED,
OBJECT,
getTimestamp,
isUndefined,
isFunction,
withArgs,
internalMutate,
revalidateEvents,
useIsomorphicLayoutEffect,
subscribeCallback,
rAF,
ConfigProvider,
mutate,
useSWRConfig
] = [
utils[InternalUtils.defaultConfig],
utils[InternalUtils.SWRGlobalState],
utils[InternalUtils.serialize],
utils[InternalUtils.createCacheHelper],
utils[InternalUtils.isEmptyCache],
utils[InternalUtils.IS_REACT_LEGACY],
utils[InternalUtils.IS_SERVER],
utils[InternalUtils.UNDEFINED],
utils[InternalUtils.OBJECT],
utils[InternalUtils.getTimestamp],
utils[InternalUtils.isUndefined],
utils[InternalUtils.isFunction],
utils[InternalUtils.withArgs],
utils[InternalUtils.internalMutate],
utils[InternalUtils.revalidateEvents],
utils[InternalUtils.useIsomorphicLayoutEffect],
utils[InternalUtils.subscribeCallback],
utils[InternalUtils.rAF],
utils[InternalUtils.SWRConfig],
utils[InternalUtils.mutate],
utils[InternalUtils.useSWRConfig]
] as const

export {
defaultConfig,
SWRGlobalState,
serialize,
createCacheHelper,
isUndefined,
isEmptyCache,
IS_REACT_LEGACY,
IS_SERVER,
UNDEFINED,
OBJECT,
getTimestamp,
isFunction,
withArgs,
internalMutate,
revalidateEvents,
useIsomorphicLayoutEffect,
subscribeCallback,
rAF,
ConfigProvider,
mutate,
useSWRConfig
}
2 changes: 1 addition & 1 deletion immutable/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useSWR, { Middleware } from 'swr'
import { withMiddleware } from 'swr/_internal'
import { withMiddleware } from './utils'

export const immutable: Middleware = useSWRNext => (key, fetcher, config) => {
// Always override all revalidate options.
Expand Down
5 changes: 5 additions & 0 deletions immutable/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import utils, { InternalUtils } from 'swr/_internal'

const [withMiddleware] = [utils[InternalUtils.withMiddleware]] as const

export { withMiddleware }
13 changes: 8 additions & 5 deletions infinite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
import { useRef, useCallback } from 'react'
import useSWR, { SWRConfig } from 'swr'

import type {
SWRHook,
MutatorCallback,
Middleware,
BareFetcher
} from 'swr/_internal'

import {
isUndefined,
isFunction,
UNDEFINED,
createCacheHelper,
SWRHook,
MutatorCallback,
Middleware,
BareFetcher,
useIsomorphicLayoutEffect,
serialize,
withMiddleware
} from 'swr/_internal'
} from './utils'

import type {
SWRInfiniteConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion infinite/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
SWRConfiguration,
SWRResponse,
Arguments,
Expand Down
29 changes: 29 additions & 0 deletions infinite/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import utils, { InternalUtils } from 'swr/_internal'

const [
isUndefined,
isFunction,
UNDEFINED,
createCacheHelper,
useIsomorphicLayoutEffect,
serialize,
withMiddleware
] = [
utils[InternalUtils.isUndefined],
utils[InternalUtils.isFunction],
utils[InternalUtils.UNDEFINED],
utils[InternalUtils.createCacheHelper],
utils[InternalUtils.useIsomorphicLayoutEffect],
utils[InternalUtils.serialize],
utils[InternalUtils.withMiddleware]
] as const

export {
isUndefined,
isFunction,
UNDEFINED,
createCacheHelper,
useIsomorphicLayoutEffect,
serialize,
withMiddleware
}
11 changes: 6 additions & 5 deletions mutation/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { useCallback, useRef } from 'react'
import useSWR, { useSWRConfig } from 'swr'

import {
serialize,
useStateWithDeps,
withMiddleware,
useIsomorphicLayoutEffect,
UNDEFINED,
getTimestamp,
Middleware,
Key
} from 'swr/_internal'
import {
getTimestamp
} from './utils'

import type { Middleware, Key } from 'swr/_internal'
import type {
SWRMutationConfiguration,
SWRMutationResponse,
SWRMutationHook,
Expand Down