Skip to content

Commit

Permalink
style(prettier)
Browse files Browse the repository at this point in the history
  • Loading branch information
darioferderber committed Apr 20, 2023
1 parent 23f1ca9 commit d71956a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
8 changes: 5 additions & 3 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<template>
<div>
<CookieControl :locale="locale" />

<iframe src="https://player.vimeo.com/video/259622029?h=8dd8029ca1"

<iframe
src="https://player.vimeo.com/video/259622029?h=8dd8029ca1"
width="640"
height="480"
frameborder="0"
allow="autoplay; fullscreen; picture-in-picture"
allowfullscreen>
allowfullscreen
>
</iframe>

<fieldset>
Expand Down
16 changes: 10 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const blockIframes = (moduleOptions: ModuleOptions) => {
name: 'functional',
initialState:
typeof moduleOptions.isIframeBlocked !== 'boolean' &&
moduleOptions.isIframeBlocked.initialState !== undefined
moduleOptions.isIframeBlocked.initialState !== undefined
? moduleOptions.isIframeBlocked.initialState
: true,
}
Expand All @@ -102,16 +102,20 @@ const blockIframes = (moduleOptions: ModuleOptions) => {
})
})

extendViteConfig(config => {
extendViteConfig((config) => {
config?.plugins?.push(
replaceCodePlugin({
replacements: [
{
from: /<iframe[^>]*.*|<\/iframe>/g,
to: (match: string) => match.includes('cookie-enabled') ?
match : match.replace(/<iframe/g, '<CookieIframe').replace(/iframe>/g, 'CookieIframe>')
}
]
to: (match: string) =>
match.includes('cookie-enabled')
? match
: match
.replace(/<iframe/g, '<CookieIframe')
.replace(/iframe>/g, 'CookieIframe>'),
},
],
})
)
})
Expand Down
44 changes: 27 additions & 17 deletions src/replace.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const execSrcReplacements = (src: any, replacements: any[]) => {
for(const replacement of replacements){
if ((typeof replacement.from === "string" ||
replacement.from instanceof RegExp) === false) {
throw new Error("[vite-plugin-replace]: The replacement option 'from' is not of type 'string' or 'RegExp'.");
}
else if ((typeof replacement.to === "string" ||
replacement.to instanceof Function) === false) {
throw new Error("[vite-plugin-replace]: The replacement option 'to' is not of type 'string' or 'Function'");
}
else src = src.replace(replacement.from, replacement.to)
for (const replacement of replacements) {
if (
(typeof replacement.from === 'string' ||
replacement.from instanceof RegExp) === false
) {
throw new Error(
"[vite-plugin-replace]: The replacement option 'from' is not of type 'string' or 'RegExp'."
)
} else if (
(typeof replacement.to === 'string' ||
replacement.to instanceof Function) === false
) {
throw new Error(
"[vite-plugin-replace]: The replacement option 'to' is not of type 'string' or 'Function'"
)
} else src = src.replace(replacement.from, replacement.to)
}

return src
Expand All @@ -19,20 +25,24 @@ export const replaceCodePlugin = (config: any) => {
config = {
replacements: [],
}
} else if ((typeof config === "object" || config !== null) === false) {
throw new Error("[vite-plugin-replace]: The configuration is not of type 'Object'.");
} else if ((typeof config === 'object' || config !== null) === false) {
throw new Error(
"[vite-plugin-replace]: The configuration is not of type 'Object'."
)
} else if (Array.isArray(config.replacements) === false) {
throw new Error("[vite-plugin-replace]: The configuration option 'replacement' is not of type 'Array'.");
throw new Error(
"[vite-plugin-replace]: The configuration option 'replacement' is not of type 'Array'."
)
}

return {
name: "transform-file",
name: 'transform-file',
enforce: 'pre',
transform: function (src: any) {
return {
code: execSrcReplacements(src, config.replacements),
map: null
map: null,
}
}
},
} as any
}
}
9 changes: 6 additions & 3 deletions src/runtime/components/CookieControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,12 @@ watch(isConsentGiven, (current, _previous) => {
}
})
watch(() => props.locale, (locale) => {
useNuxtApp().$cookies.locale.value = locale
})
watch(
() => props.locale,
(locale) => {
useNuxtApp().$cookies.locale.value = locale
}
)
// initialization
init()
Expand Down
11 changes: 8 additions & 3 deletions src/runtime/components/CookieIframe.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<template>
<ClientOnly>
<iframe :cookie-enabled="null" v-if="isCookieFunctionalEnabled" v-bind="$attrs" />
<iframe
:cookie-enabled="null"
v-if="isCookieFunctionalEnabled"
v-bind="$attrs"
/>
<div v-else class="cookieControl__BlockedIframe">
<p>
{{ localeStrings?.iframeBlocked }}
Expand Down Expand Up @@ -28,6 +32,7 @@ const isCookieFunctionalEnabled = computed(
(cookieEnabled: Cookie) => cookieEnabled.name === 'functional'
).length > 0
)
const localeStrings = computed(() => moduleOptions.localeTexts[useNuxtApp().$cookies.locale.value])
const localeStrings = computed(
() => moduleOptions.localeTexts[useNuxtApp().$cookies.locale.value]
)
</script>

2 changes: 1 addition & 1 deletion src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default defineNuxtPlugin((_nuxtApp) => {
cookiesEnabledIds,
isModalActive,
moduleOptions,
locale
locale,
} as State

return {
Expand Down

0 comments on commit d71956a

Please sign in to comment.