Skip to content

Commit cd54b97

Browse files
authored
Merge pull request #53 from swup/chore/refactor-exports
chore: refactor exports
2 parents 3b8e61f + fcd59d4 commit cd54b97

7 files changed

+52
-54
lines changed

src/SwupFragmentPlugin.ts

+35-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
import PluginBase from '@swup/plugin';
22
import ParsedRule from './inc/ParsedRule.js';
33
import Logger from './inc/Logger.js';
4-
import { handlePageView, cleanupFragmentElements, getFragmentVisit } from './inc/functions.js';
5-
import type { Route, Options, FragmentVisit } from './inc/types.js';
6-
4+
import {
5+
handlePageView,
6+
cleanupFragmentElements,
7+
getFirstMatchingRule,
8+
getContainersForVisit
9+
} from './inc/functions.js';
10+
import type { Options, Route, FragmentVisit } from './inc/defs.js';
711
import * as handlers from './inc/handlers.js';
8-
912
import { __DEV__ } from './inc/env.js';
1013

11-
declare module 'swup' {
12-
export interface Swup {
13-
getFragmentVisit?: (route: Route) => FragmentVisit | undefined;
14-
}
15-
export interface Visit {
16-
fragmentVisit?: FragmentVisit;
17-
}
18-
export interface CacheData {
19-
fragmentHtml?: string;
20-
}
21-
}
22-
2314
type RequireKeys<T, K extends keyof T> = Partial<T> & Pick<T, K>;
2415
type InitOptions = RequireKeys<Options, 'rules'>;
2516

@@ -74,7 +65,7 @@ export default class SwupFragmentPlugin extends PluginBase {
7465
this.on('content:replace', handlers.onContentReplace);
7566
this.on('visit:end', handlers.onVisitEnd);
7667

77-
swup.getFragmentVisit = getFragmentVisit.bind(this);
68+
swup.getFragmentVisit = this.getFragmentVisit.bind(this);
7869

7970
if (__DEV__) {
8071
this.logger?.warnIf(
@@ -94,4 +85,31 @@ export default class SwupFragmentPlugin extends PluginBase {
9485
this.swup.getFragmentVisit = undefined;
9586
cleanupFragmentElements();
9687
}
88+
89+
/**
90+
* Get the fragment visit object for a given route
91+
*/
92+
getFragmentVisit(route: Route): FragmentVisit | undefined {
93+
const rule = getFirstMatchingRule(route, this.rules);
94+
95+
// Bail early if no rule matched
96+
if (!rule) return;
97+
98+
// Get containers that should be replaced for this visit
99+
const containers = getContainersForVisit(route, rule.containers, this.logger);
100+
// Bail early if there are no containers to be replaced for this visit
101+
if (!containers.length) return;
102+
103+
// Pick properties from the current rule that should be projected into the fragmentVisit object
104+
const { name, scroll, focus } = rule;
105+
106+
const fragmentVisit: FragmentVisit = {
107+
containers,
108+
name,
109+
scroll,
110+
focus
111+
};
112+
113+
return fragmentVisit;
114+
}
97115
}

src/inc/ParsedRule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { matchPath, classify, Location } from 'swup';
22
import type { Path } from 'swup';
3-
import type { Route } from './types.js';
3+
import type { Route } from './defs.js';
44
import { dedupe } from './functions.js';
55
import Logger, { highlight } from './Logger.js';
66
import { __DEV__ } from './env.js';

src/inc/types.d.ts src/inc/defs.ts

File renamed without changes.

src/inc/functions.ts

+1-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Location } from 'swup';
22
import type { Visit, VisitScroll } from 'swup';
33
import type { default as FragmentPlugin } from '../SwupFragmentPlugin.js';
4-
import type { Route, FragmentVisit, FragmentElement } from './types.js';
4+
import type { Route, FragmentVisit, FragmentElement } from './defs.js';
55
import type ParsedRule from './ParsedRule.js';
66
import Logger, { highlight } from './Logger.js';
77

@@ -329,37 +329,6 @@ export function dedupe<T>(arr: Array<T>): Array<T> {
329329
return [...new Set<T>(arr)];
330330
}
331331

332-
/**
333-
* Get the fragment visit object for a given route
334-
*/
335-
export function getFragmentVisit(
336-
this: FragmentPlugin,
337-
route: Route,
338-
logger?: Logger
339-
): FragmentVisit | undefined {
340-
const rule = getFirstMatchingRule(route, this.rules);
341-
342-
// Bail early if no rule matched
343-
if (!rule) return;
344-
345-
// Get containers that should be replaced for this visit
346-
const containers = getContainersForVisit(route, rule.containers, logger);
347-
// Bail early if there are no containers to be replaced for this visit
348-
if (!containers.length) return;
349-
350-
// Pick properties from the current rule that should be projected into the fragmentVisit object
351-
const { name, scroll, focus } = rule;
352-
353-
const visit: FragmentVisit = {
354-
containers,
355-
name,
356-
scroll,
357-
focus
358-
};
359-
360-
return visit;
361-
}
362-
363332
/**
364333
* Adjusts visit.scroll based on given fragment visit
365334
*/

src/inc/handlers.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
getFirstMatchingRule,
1111
cacheForeignFragmentElements,
1212
shouldSkipAnimation,
13-
getFragmentVisit,
1413
adjustVisitScroll
1514
} from './functions.js';
1615

@@ -36,7 +35,7 @@ export const onVisitStart: Handler<'visit:start'> = async function (this: Fragme
3635
const route = getRoute(visit);
3736
if (!route) return;
3837

39-
const fragmentVisit = getFragmentVisit.call(this, route, this.logger);
38+
const fragmentVisit = this.getFragmentVisit(route);
4039

4140
/**
4241
* Bail early if the current route doesn't match

src/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
import FragmentPlugin from './SwupFragmentPlugin.js';
22
export default FragmentPlugin;
3-
export type { Options, Rule, FragmentElement } from './inc/types.js';
3+
import type { FragmentVisit } from './inc/defs.js';
4+
export type { Options, Rule, FragmentElement, FragmentVisit } from './inc/defs.js';
5+
6+
declare module 'swup' {
7+
export interface Swup {
8+
getFragmentVisit?: FragmentPlugin['getFragmentVisit'];
9+
}
10+
export interface Visit {
11+
fragmentVisit?: FragmentVisit;
12+
}
13+
export interface CacheData {
14+
fragmentHtml?: string;
15+
}
16+
}

tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"moduleDetection": "force",
1313
/* Strictness */
1414
"strict": true,
15-
// "noUncheckedIndexedAccess": true,
1615
/* If transpiling with TypeScript: */
1716
"moduleResolution": "NodeNext",
1817
"module": "NodeNext",

0 commit comments

Comments
 (0)