{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import Plugin from '@swup/plugin';\nimport { query, queryAll } from 'swup';\nimport type { Swup, HookName, HookArguments } from 'swup';\n\ndeclare global {\n\tinterface Window {\n\t\tswup?: Swup;\n\t}\n}\n\ntype Options = {\n\tglobalInstance: boolean;\n};\n\nexport default class SwupDebugPlugin extends Plugin {\n\tname = 'SwupDebugPlugin';\n\n\trequires = { swup: '>=4' };\n\n\tdefaults: Options = {\n\t\tglobalInstance: false\n\t};\n\toptions: Options;\n\toriginalSwupLog?: Swup['log'];\n\toriginalSwupHookCall?: Swup['hooks']['call'];\n\toriginalSwupHookCallSync?: Swup['hooks']['callSync'];\n\n\tconstructor(options = {}) {\n\t\tsuper();\n\t\tthis.options = { ...this.defaults, ...options };\n\t}\n\n\tmount() {\n\t\t// set non-empty log method of swup\n\t\tthis.setLogImplementation();\n\n\t\t// set swup instance as a global variable swup\n\t\tthis.setGlobalInstance();\n\n\t\t// make hook calls appear in console\n\t\tthis.proxyHooksThroughConsole();\n\n\t\t// check if title tag is present\n\t\tthis.checkDocumentTitle();\n\n\t\t// check if all containers are present\n\t\tthis.checkContainers();\n\n\t\t// check if transition classes map to containers\n\t\tthis.checkAnimationSelector();\n\t}\n\n\tunmount() {\n\t\tsuper.unmount();\n\n\t\tthis.restoreLogImplementation();\n\t\tthis.restoreHooksImplementation();\n\t\tthis.unsetGlobalInstance();\n\t}\n\n\tsetLogImplementation() {\n\t\tthis.originalSwupLog = this.swup.log;\n\t\tthis.swup.log = this.log;\n\t}\n\n\trestoreLogImplementation() {\n\t\tthis.swup.log = this.originalSwupLog!;\n\t}\n\n\tproxyHooksThroughConsole() {\n\t\tthis.originalSwupHookCall = this.swup.hooks.call.bind(this.swup.hooks);\n\t\tthis.originalSwupHookCallSync = this.swup.hooks.callSync.bind(this.swup.hooks);\n\t\tthis.swup.hooks.call = this.callHook.bind(this);\n\t\tthis.swup.hooks.callSync = this.callHookSync.bind(this);\n\t}\n\n\trestoreHooksImplementation() {\n\t\tthis.swup.hooks.call = this.originalSwupHookCall!;\n\t\tthis.swup.hooks.callSync = this.originalSwupHookCallSync!;\n\t}\n\n\tsetGlobalInstance() {\n\t\tif (this.options.globalInstance) {\n\t\t\twindow.swup = this.swup;\n\t\t}\n\t}\n\n\tunsetGlobalInstance() {\n\t\tif (this.options.globalInstance) {\n\t\t\twindow.swup = undefined;\n\t\t}\n\t}\n\n\tcheckDocumentTitle() {\n\t\tif (!query('title')) {\n\t\t\tthis.error('Document is missing a title tag. It is required on every page.');\n\t\t}\n\t}\n\n\tcheckContainers() {\n\t\tfor (const selector of this.swup.options.containers) {\n\t\t\tconst containers = queryAll(selector);\n\t\t\tif (!containers.length) {\n\t\t\t\tthis.error(`Container \\`${selector}\\` is missing on the page.`);\n\t\t\t}\n\t\t\tif (containers.length > 1) {\n\t\t\t\tthis.error(`Container \\`${selector}\\` matches multiple elements.`);\n\t\t\t}\n\t\t\tif (containers.some((container) => !container.matches('body *'))) {\n\t\t\t\tthis.error(`Container \\`${selector}\\` is not supported. It must be a child of the body tag.`);\n\t\t\t}\n\t\t}\n\t}\n\n\tcheckAnimationSelector() {\n\t\tconst { animationSelector } = this.swup.options;\n\t\tif (!animationSelector) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst containers = this.swup.options.containers.map((selector) => query(selector));\n\t\tconst animatedContainers = containers.filter((el) => el?.matches(animationSelector));\n\t\tif (!animatedContainers.length) {\n\t\t\tthis.warn(`No container matches the animation selector \\`${animationSelector}\\`.`);\n\t\t}\n\t}\n\n\tlogHook<T extends HookName>(hook: T, args: HookArguments<T>) {\n\t\tconsole.groupCollapsed('%cswup:' + '%c' + hook, 'color: #343434', 'color: #009ACD');\n\t\tconsole.log(args);\n\t\tconsole.groupEnd();\n\t}\n\n\tcallHook: Swup['hooks']['call'] = (hook, args, ...rest) => {\n\t\tthis.logHook(hook, args);\n\t\treturn this.originalSwupHookCall!(hook, args, ...rest);\n\t};\n\n\tcallHookSync: Swup['hooks']['callSync'] = (hook, args, ...rest) => {\n\t\tthis.logHook(hook, args);\n\t\treturn this.originalSwupHookCallSync!(hook, args, ...rest);\n\t};\n\n\tlog(str: string, object: any): void {\n\t\tif (object) {\n\t\t\tconsole.groupCollapsed(str);\n\t\t\tfor (let key in object) {\n\t\t\t\tconsole.log(object[key]);\n\t\t\t}\n\t\t\tconsole.groupEnd();\n\t\t} else {\n\t\t\tconsole.log(str + '%c', 'color: #009ACD');\n\t\t}\n\t}\n\n\twarn(str: string): void {\n\t\tconsole.warn(`[swup debug plugin] ${str}`);\n\t}\n\n\terror(str: string): void {\n\t\tconsole.error(`[swup debug plugin] ${str}`);\n\t}\n}\n"],"names":["Plugin","constructor","options","super","this","name","requires","swup","defaults","globalInstance","originalSwupLog","originalSwupHookCall","originalSwupHookCallSync","callHook","_this","hook","args","logHook","slice","call","arguments","callHookSync","_this2","mount","setLogImplementation","setGlobalInstance","proxyHooksThroughConsole","checkDocumentTitle","checkContainers","checkAnimationSelector","unmount","restoreLogImplementation","restoreHooksImplementation","unsetGlobalInstance","log","hooks","bind","callSync","window","undefined","query","error","selector","containers","queryAll","length","some","container","matches","animationSelector","map","filter","el","warn","console","groupCollapsed","groupEnd","str","object","key"],"mappings":"+JAcqB,cAAwBA,EAAAA,QAa5CC,WAAAA,CAAYC,QAAAA,IAAAA,IAAAA,EAAU,IACrBC,QAAQC,KAbTC,KAAO,uBAEPC,SAAW,CAAEC,KAAM,YAEnBC,SAAoB,CACnBC,gBAAgB,QAEjBP,aAAO,EAAAE,KACPM,qBACAC,EAAAA,KAAAA,iCACAC,8BAAwB,EAAAR,KA4GxBS,SAAQ,MAAA,IAAAC,EAA0BV,KAAA,OAAA,SAACW,EAAMC,GAExC,OADAF,EAAKG,QAAQF,EAAMC,GACZF,EAAKH,qBAAsBI,EAAMC,KAAME,GAAAA,MAAAC,KAAAC,UAAA,GAC/C,CAAC,EAHO,GAGPhB,KAEDiB,aAAYC,MAAAA,IAAAA,cAA+BP,SAAA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