Fix extension build

This commit is contained in:
Nathan Bierema 2024-08-05 21:51:13 -04:00
parent 70d9932942
commit 5596651862
11 changed files with 114 additions and 107 deletions

View File

@ -18,7 +18,7 @@ import syncOptions, {
} from '../../options/syncOptions'; } from '../../options/syncOptions';
import openDevToolsWindow, { DevToolsPosition } from '../openWindow'; import openDevToolsWindow, { DevToolsPosition } from '../openWindow';
import { getReport } from '../logging'; import { getReport } from '../logging';
import { Action, Dispatch, MiddlewareAPI } from 'redux'; import { Action, Dispatch, Middleware } from 'redux';
import type { import type {
ContentScriptToBackgroundMessage, ContentScriptToBackgroundMessage,
SplitMessage, SplitMessage,
@ -670,10 +670,10 @@ declare global {
window.syncOptions = syncOptions(toAllTabs); // Expose to the options page window.syncOptions = syncOptions(toAllTabs); // Expose to the options page
export default function api( const api: Middleware<{}, BackgroundState, Dispatch<BackgroundAction>> =
store: MiddlewareAPI<Dispatch<BackgroundAction>, BackgroundState>, (store) => (next) => (untypedAction) => {
) { const action = untypedAction as BackgroundAction;
return (next: Dispatch<BackgroundAction>) => (action: BackgroundAction) => {
if (action.type === LIFTED_ACTION) toContentScript(action); if (action.type === LIFTED_ACTION) toContentScript(action);
else if (action.type === TOGGLE_PERSIST) { else if (action.type === TOGGLE_PERSIST) {
togglePersist(); togglePersist();
@ -684,4 +684,5 @@ export default function api(
} }
return next(action); return next(action);
}; };
}
export default api;

View File

@ -1,14 +1,17 @@
import { combineReducers, Reducer } from 'redux'; import { combineReducers, Reducer } from 'redux';
import { instances, InstancesState } from '@redux-devtools/app'; import { instances, InstancesState } from '@redux-devtools/app';
import type { BackgroundAction } from './backgroundStore'; import { BackgroundAction } from './backgroundStore';
export interface BackgroundState { export interface BackgroundState {
readonly instances: InstancesState; readonly instances: InstancesState;
} }
const rootReducer: Reducer<BackgroundState, BackgroundAction> = const rootReducer: Reducer<
combineReducers<BackgroundState>({ BackgroundState,
instances, BackgroundAction,
}); Partial<BackgroundState>
> = combineReducers({
instances,
}) as any;
export default rootReducer; export default rootReducer;

View File

@ -1,4 +1,4 @@
import { createStore, applyMiddleware, PreloadedState } from 'redux'; import { createStore, applyMiddleware } from 'redux';
import { import {
CustomAction, CustomAction,
DispatchAction, DispatchAction,
@ -60,7 +60,7 @@ export type BackgroundAction =
| DisconnectedAction; | DisconnectedAction;
export default function configureStore( export default function configureStore(
preloadedState?: PreloadedState<BackgroundState>, preloadedState?: Partial<BackgroundState>,
) { ) {
return createStore(rootReducer, preloadedState, applyMiddleware(api)); return createStore(rootReducer, preloadedState, applyMiddleware(api));
/* /*

View File

@ -30,16 +30,19 @@ export interface StoreStateWithoutSocket {
readonly stateTreeSettings: StateTreeSettings; readonly stateTreeSettings: StateTreeSettings;
} }
const rootReducer: Reducer<StoreStateWithoutSocket, StoreAction> = const rootReducer: Reducer<
combineReducers<StoreStateWithoutSocket>({ StoreStateWithoutSocket,
instances, StoreAction,
monitor, Partial<StoreStateWithoutSocket>
reports, > = combineReducers({
notification, instances,
section, monitor,
theme, reports,
connection, notification,
stateTreeSettings, section,
}); theme,
connection,
stateTreeSettings,
}) as any;
export default rootReducer; export default rootReducer;

View File

@ -1,4 +1,4 @@
import { createStore, applyMiddleware, Reducer } from 'redux'; import { createStore, applyMiddleware, Reducer, Store } from 'redux';
import localForage from 'localforage'; import localForage from 'localforage';
import { persistReducer, persistStore } from 'redux-persist'; import { persistReducer, persistStore } from 'redux-persist';
import { exportStateMiddleware, StoreAction } from '@redux-devtools/app'; import { exportStateMiddleware, StoreAction } from '@redux-devtools/app';
@ -23,6 +23,6 @@ export default function configureStore(
panelDispatcher(bgConnection), panelDispatcher(bgConnection),
); );
const store = createStore(persistedReducer, enhancer); const store = createStore(persistedReducer, enhancer);
const persistor = persistStore(store); const persistor = persistStore(store as Store);
return { store, persistor }; return { store, persistor };
} }

View File

@ -7,31 +7,33 @@ import {
TOGGLE_PERSIST, TOGGLE_PERSIST,
UPDATE_STATE, UPDATE_STATE,
} from '@redux-devtools/app'; } from '@redux-devtools/app';
import { Dispatch, MiddlewareAPI } from 'redux'; import { Dispatch, Middleware } from 'redux';
function panelDispatcher(bgConnection: chrome.runtime.Port) { function panelDispatcher(
bgConnection: chrome.runtime.Port,
): Middleware<{}, StoreState, Dispatch<StoreAction>> {
let autoselected = false; let autoselected = false;
const tabId = chrome.devtools.inspectedWindow.tabId; const tabId = chrome.devtools.inspectedWindow.tabId;
return (store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) => return (store) => (next) => (untypedAction) => {
(next: Dispatch<StoreAction>) => const action = untypedAction as StoreAction;
(action: StoreAction) => {
const result = next(action); const result = next(action);
if (!autoselected && action.type === UPDATE_STATE && tabId) { if (!autoselected && action.type === UPDATE_STATE && tabId) {
autoselected = true; autoselected = true;
const connections = store.getState().instances.connections[tabId]; const connections = store.getState().instances.connections[tabId];
if (connections && connections.length === 1) { if (connections && connections.length === 1) {
next({ type: SELECT_INSTANCE, selected: connections[0] }); next({ type: SELECT_INSTANCE, selected: connections[0] });
}
} }
if (action.type === LIFTED_ACTION || action.type === TOGGLE_PERSIST) { }
const instances = store.getState().instances; if (action.type === LIFTED_ACTION || action.type === TOGGLE_PERSIST) {
const instanceId = getActiveInstance(instances); const instances = store.getState().instances;
const id = instances.options[instanceId].connectionId; const instanceId = getActiveInstance(instances);
bgConnection.postMessage({ ...action, instanceId, id }); const id = instances.options[instanceId].connectionId;
} bgConnection.postMessage({ ...action, instanceId, id });
return result; }
}; return result;
};
} }
export default panelDispatcher; export default panelDispatcher;

View File

@ -9,7 +9,6 @@ import {
Action, Action,
ActionCreator, ActionCreator,
Dispatch, Dispatch,
PreloadedState,
Reducer, Reducer,
StoreEnhancer, StoreEnhancer,
StoreEnhancerStoreCreator, StoreEnhancerStoreCreator,
@ -526,34 +525,28 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<string>>(
relayState(liftedState); relayState(liftedState);
} }
const enhance = const enhance = (): StoreEnhancer => (next) => {
(): StoreEnhancer => return <S2, A2 extends Action<string>, PreloadedState>(
<NextExt, NextStateExt>( reducer_: Reducer<S2, A2, PreloadedState>,
next: StoreEnhancerStoreCreator<NextExt, NextStateExt>, initialState_?: PreloadedState | undefined,
): any => { ) => {
return <S2 extends S, A2 extends A>( if (!isAllowed(window.devToolsOptions)) {
reducer_: Reducer<S2, A2>, return next(reducer_, initialState_);
initialState_?: PreloadedState<S2>, }
) => {
if (!isAllowed(window.devToolsOptions)) {
return next(reducer_, initialState_);
}
store = stores[instanceId] = configureStore( store = stores[instanceId] = (
next as StoreEnhancerStoreCreator, configureStore(next, monitor.reducer, {
monitor.reducer, ...config,
{ maxAge: getMaxAge as any,
...config, }) as any
maxAge: getMaxAge as any, )(reducer_, initialState_) as any;
},
)(reducer_, initialState_) as any;
if (isInIframe()) setTimeout(init, 3000); if (isInIframe()) setTimeout(init, 3000);
else init(); else init();
return store; return store as any;
};
}; };
};
return enhance(); return enhance();
} }
@ -598,11 +591,11 @@ export type InferComposedStoreExt<StoreEnhancers> = StoreEnhancers extends [
? HeadStoreEnhancer extends StoreEnhancer<infer StoreExt> ? HeadStoreEnhancer extends StoreEnhancer<infer StoreExt>
? StoreExt & InferComposedStoreExt<RestStoreEnhancers> ? StoreExt & InferComposedStoreExt<RestStoreEnhancers>
: never : never
: unknown; : {};
const extensionCompose = const extensionCompose =
(config: Config) => (config: Config) =>
<StoreEnhancers extends readonly StoreEnhancer<unknown>[]>( <StoreEnhancers extends readonly StoreEnhancer[]>(
...funcs: StoreEnhancers ...funcs: StoreEnhancers
): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>> => { ): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>> => {
// @ts-ignore FIXME // @ts-ignore FIXME
@ -619,10 +612,10 @@ const extensionCompose =
interface ReduxDevtoolsExtensionCompose { interface ReduxDevtoolsExtensionCompose {
( (
config: Config, config: Config,
): <StoreEnhancers extends readonly StoreEnhancer<unknown>[]>( ): <StoreEnhancers extends readonly StoreEnhancer[]>(
...funcs: StoreEnhancers ...funcs: StoreEnhancers
) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>; ) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
<StoreEnhancers extends readonly StoreEnhancer<unknown>[]>( <StoreEnhancers extends readonly StoreEnhancer[]>(
...funcs: StoreEnhancers ...funcs: StoreEnhancers
): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>; ): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
} }
@ -635,24 +628,22 @@ declare global {
function reduxDevtoolsExtensionCompose( function reduxDevtoolsExtensionCompose(
config: Config, config: Config,
): <StoreEnhancers extends readonly StoreEnhancer<unknown>[]>( ): <StoreEnhancers extends readonly StoreEnhancer[]>(
...funcs: StoreEnhancers ...funcs: StoreEnhancers
) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>; ) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
function reduxDevtoolsExtensionCompose< function reduxDevtoolsExtensionCompose<
StoreEnhancers extends readonly StoreEnhancer<unknown>[], StoreEnhancers extends readonly StoreEnhancer[],
>( >(
...funcs: StoreEnhancers ...funcs: StoreEnhancers
): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>; ): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
function reduxDevtoolsExtensionCompose( function reduxDevtoolsExtensionCompose(...funcs: [Config] | StoreEnhancer[]) {
...funcs: [Config] | StoreEnhancer<unknown>[]
) {
if (funcs.length === 0) { if (funcs.length === 0) {
return __REDUX_DEVTOOLS_EXTENSION__(); return __REDUX_DEVTOOLS_EXTENSION__();
} }
if (funcs.length === 1 && typeof funcs[0] === 'object') { if (funcs.length === 1 && typeof funcs[0] === 'object') {
return extensionCompose(funcs[0]); return extensionCompose(funcs[0]);
} }
return extensionCompose({})(...(funcs as StoreEnhancer<unknown>[])); return extensionCompose({})(...(funcs as StoreEnhancer[]));
} }
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = reduxDevtoolsExtensionCompose; window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = reduxDevtoolsExtensionCompose;

View File

@ -1,4 +1,4 @@
import { Dispatch, MiddlewareAPI } from 'redux'; import { Dispatch, Middleware, MiddlewareAPI } from 'redux';
import { import {
SELECT_INSTANCE, SELECT_INSTANCE,
StoreAction, StoreAction,
@ -9,7 +9,7 @@ import {
function selectInstance( function selectInstance(
tabId: number, tabId: number,
store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>, store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>,
next: Dispatch<StoreAction>, next: (action: unknown) => unknown,
) { ) {
const instances = store.getState().instances; const instances = store.getState().instances;
if (instances.current === 'default') return; if (instances.current === 'default') return;
@ -33,10 +33,10 @@ function getCurrentTabId(next: (tabId: number) => void) {
); );
} }
export default function popupSelector( const popupSelector: Middleware<{}, StoreState, Dispatch<StoreAction>> =
store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>, (store) => (next) => (untypedAction) => {
) { const action = untypedAction as StoreAction;
return (next: Dispatch<StoreAction>) => (action: StoreAction) => {
const result = next(action); const result = next(action);
if (action.type === UPDATE_STATE) { if (action.type === UPDATE_STATE) {
if (chrome.devtools && chrome.devtools.inspectedWindow) { if (chrome.devtools && chrome.devtools.inspectedWindow) {
@ -47,4 +47,5 @@ export default function popupSelector(
} }
return result; return result;
}; };
}
export default popupSelector;

View File

@ -13,17 +13,20 @@ import {
import instances from './instancesReducer'; import instances from './instancesReducer';
import type { WindowStoreAction } from './windowStore'; import type { WindowStoreAction } from './windowStore';
const rootReducer: Reducer<StoreState, WindowStoreAction> = const rootReducer: Reducer<
combineReducers<StoreState>({ StoreState,
instances, WindowStoreAction,
monitor, Partial<StoreState>
socket, > = combineReducers({
reports, instances,
notification, monitor,
section, socket,
theme, reports,
connection, notification,
stateTreeSettings, section,
}); theme,
connection,
stateTreeSettings,
}) as any;
export default rootReducer; export default rootReducer;

View File

@ -69,7 +69,7 @@ export default function configureStore(
); );
} }
const store = createStore(persistedReducer, enhancer); const store = createStore(persistedReducer, enhancer);
const persistor = persistStore(store, null, () => { const persistor = persistStore(store as Store, null, () => {
if (store.getState().connection.type !== 'disabled') { if (store.getState().connection.type !== 'disabled') {
store.dispatch({ store.dispatch({
type: CONNECT_REQUEST, type: CONNECT_REQUEST,

View File

@ -6,16 +6,19 @@ import {
TOGGLE_PERSIST, TOGGLE_PERSIST,
UPDATE_STATE, UPDATE_STATE,
} from '@redux-devtools/app'; } from '@redux-devtools/app';
import { Dispatch, MiddlewareAPI, Store } from 'redux'; import { Dispatch, Middleware, Store } from 'redux';
import type { BackgroundState } from '../../background/store/backgroundReducer'; import type { BackgroundState } from '../../background/store/backgroundReducer';
import type { WindowStoreAction } from './windowStore';
import type { BackgroundAction } from '../../background/store/backgroundStore'; import type { BackgroundAction } from '../../background/store/backgroundStore';
const syncStores = const syncStores =
(baseStore: Store<BackgroundState, BackgroundAction>) => (
(store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) => baseStore: Store<BackgroundState, BackgroundAction>,
(next: Dispatch<WindowStoreAction>) => ): Middleware<{}, StoreState, Dispatch<StoreAction>> =>
(action: StoreAction) => { (store) =>
(next) =>
(untypedAction) => {
const action = untypedAction as StoreAction;
if (action.type === UPDATE_STATE) { if (action.type === UPDATE_STATE) {
return next({ return next({
...action, ...action,