{"version":3,"file":"index.cjs","sources":["../src/versionSatisfies.ts","../src/index.ts","../src/pluginRequirements.ts"],"sourcesContent":["type Comparator = '>' | '>=' | '<' | '<=';\n\n// Fill versions to exactly 3 decimals\nexport const normalizeVersion = (version: string): string => {\n\treturn String(version)\n\t\t.split('.')\n\t\t.map(segment => String(parseInt(segment || '0', 10)))\n\t\t.concat(['0', '0'])\n\t\t.slice(0, 3)\n\t\t.join('.');\n};\n\n// Numerically compare version strings after normalizing them\nexport const compareVersion = (a: string, b: string): number => {\n\ta = normalizeVersion(a);\n\tb = normalizeVersion(b);\n\treturn a.localeCompare(b, undefined, { numeric: true });\n};\n\n// Apply a comparator (equals, greater-than, etc) by its symbol to a sort comparison\nconst applyComparator = (comparisonResult: number, comparator: Comparator) => {\n\tconst comparators = {\n\t\t'': (r: number) => r === 0,\n\t\t'>': (r: number) => r > 0,\n\t\t'>=': (r: number) => r >= 0,\n\t\t'<': (r: number) => r < 0,\n\t\t'<=': (r: number) => r <= 0\n\t};\n\tconst comparatorFn = comparators[comparator] || comparators[''];\n\treturn comparatorFn(comparisonResult);\n};\n\n/**\n * Check if a version satisfies all given version requirements\n *\n * versionSatisfies('2.1.0', ['>=2', '<4']) // true\n * versionSatisfies('2.1.0', ['5']) // false\n *\n * @param {string} installed Installed version\n * @param {Array.<string>} requirements Array of requirements that must be satisfied\n * @returns boolean\n */\nexport const versionSatisfies = (installed: string, requirements: string[]) => {\n\treturn requirements.every((required) => {\n\t\tconst [, comparator, version] = required.match(/^([\\D]+)?(.*)$/) || [];\n\t\tconst comparisonResult = compareVersion(installed, version);\n\t\treturn applyComparator(comparisonResult, (comparator as Comparator) || '>=');\n\t});\n};\n","import type Swup from 'swup';\nimport type { Plugin, HookName, HookOptions, HookUnregister, Handler } from 'swup';\nimport { checkDependencyVersion } from './pluginRequirements.js';\n\nfunction isBound(func: Function) {\n\treturn func.name.startsWith('bound ') && !func.hasOwnProperty('prototype');\n}\n\nexport default abstract class SwupPlugin implements Plugin {\n\t/** Name of the plugin */\n\tabstract name: string;\n\n\t/** Identify as a swup plugin */\n\tisSwupPlugin: true = true;\n\n\t// Swup instance, assigned by swup itself\n\tswup!: Swup;\n\n\t/** Version of this plugin. Currently not in use, defined here for backward compatiblity. */\n\tversion?: string;\n\n\t/** Version requirements of this plugin. Example: `{ swup: '>=4' }` */\n\trequires?: Record<string, string | string[]> = {};\n\n\t// List of hook handlers to unregister on unmount\n\tprivate handlersToUnregister: HookUnregister[] = [];\n\n\t/** Run on mount */\n\tmount() {\n\t\t// this is mount method rewritten by class extending\n\t\t// and is executed when swup is enabled with plugin\n\t}\n\n\t/** Run on unmount */\n\tunmount() {\n\t\t// this is unmount method rewritten by class extending\n\t\t// and is executed when swup with plugin is disabled\n\n\t\t// Unsubscribe all registered hook handlers\n\t\tthis.handlersToUnregister.forEach((unregister) => unregister());\n\t\tthis.handlersToUnregister = [];\n\t}\n\n\t_beforeMount(): void {\n\t\tif (!this.name) {\n\t\t\tthrow new Error('You must define a name of plugin when creating a class.');\n\t\t}\n\t}\n\n\t_afterUnmount(): void {\n\t\t// here for any future hidden auto-cleanup\n\t}\n\n\t_checkRequirements(): boolean {\n\t\tif (typeof this.requires !== 'object') {\n\t\t\treturn true;\n\t\t}\n\n\t\tObject.entries(this.requires).forEach(([dependency, versions]) => {\n\t\t\tversions = Array.isArray(versions) ? versions : [versions];\n\t\t\tif (!checkDependencyVersion(dependency, versions, this.swup)) {\n\t\t\t\tconst requirement = `${dependency} ${versions.join(', ')}`;\n\t\t\t\tthrow new Error(`Plugin version mismatch: ${this.name} requires ${requirement}`);\n\t\t\t}\n\t\t});\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Register a new hook handler.\n\t *\n\t * On plugin unmount, the handler will automatically be unregistered.\n\t * The handler function is lexically bound to the plugin instance for convenience.\n\t * @see swup.hooks.on\n\t */\n\tprotected on<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {\n\t\thandler = !isBound(handler) ? handler.bind(this) : handler;\n\t\tconst unregister = this.swup.hooks.on(hook, handler, options);\n\t\tthis.handlersToUnregister.push(unregister);\n\t\treturn unregister;\n\t}\n\n\tprotected once<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {\n\t\treturn this.on(hook, handler, { ...options, once: true });\n\t}\n\n\tprotected before<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {\n\t\treturn this.on(hook, handler, { ...options, before: true });\n\t}\n\n\tprotected replace<T extends HookName>(hook: T, handler: Handler<T>, options: HookOptions = {}): HookUnregister {\n\t\treturn this.on(hook, handler, { ...options, replace: true });\n\t}\n\n\tprotected off<T extends HookName>(hook: T, handler?: Handler<T>): void {\n\t\treturn this.swup.hooks.off(hook, handler!);\n\t}\n}\n","import type Swup from 'swup';\n\nimport { versionSatisfies } from './versionSatisfies.js';\n\nfunction getInstalledDependencyVersion(dependency: string, swup: Swup): string {\n\tif (dependency === 'swup') {\n\t\treturn swup.version ?? '';\n\t} else {\n\t\t// Circular type dependency?\n\t\t// findPlugin returns swup's Plugin type which is not up-to-date\n\t\t// with the actual Plugin type from index.ts\n\t\tconst plugin = swup.findPlugin(dependency);\n\t\treturn plugin?.version ?? '';\n\t}\n}\n\nexport function checkDependencyVersion(\n\tdependency: string,\n\trequirements: string[],\n\tswup: Swup\n): boolean {\n\tconst version = getInstalledDependencyVersion(dependency, swup);\n\tif (version) {\n\t\treturn versionSatisfies(version, requirements);\n\t} else {\n\t\treturn false;\n\t}\n}\n"],"names":["normalizeVersion","version","String","split","map","segment","parseInt","concat","slice","join","constructor","isSwupPlugin","this","swup","requires","handlersToUnregister","mount","unmount","forEach","unregister","_beforeMount","name","Error","_afterUnmount","_checkRequirements","Object","entries","_ref","dependency","versions","Array","isArray","requirements","plugin","findPlugin","getInstalledDependencyVersion","versionSatisfies","installed","every","required","comparator","match","compareVersion","a","b","applyComparator","comparisonResult","comparators","r","localeCompare","undefined","numeric","checkDependencyVersion","requirement","on","hook","handler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