Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't include css-vars-ponyfill chunk if not enabled #53

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default defineNuxtModule<ModuleOptions>({
},
},
async setup(moduleOptions, nuxt) {
nuxt.options.alias['#cookie-control/set-vars'] =
moduleOptions.isCssPonyfillEnabled
? resolve(runtimeDir, 'set-vars/ponyfill')
: resolve(runtimeDir, 'set-vars/native')

nuxt.options.alias['#cookie-control'] = runtimeDir
nuxt.options.build.transpile.push(runtimeDir)

Expand Down
16 changes: 3 additions & 13 deletions src/runtime/components/CookieControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ import {
setCookie,
resolveTranslatable,
} from '../methods'
import setCSSVariables from '#cookie-control/set-vars'

import { useCookieControl } from '#imports'

Expand Down Expand Up @@ -312,26 +313,15 @@ const toggleLabel = ($event: KeyboardEvent) => {
}

// lifecycle
onBeforeMount(async () => {
onBeforeMount(() => {
if (moduleOptions.colors) {
const variables: Record<string, any> = {}

for (const key in moduleOptions.colors) {
variables[`cookie-control-${key}`] = `${moduleOptions.colors[key]}`
}

if (moduleOptions.isCssPonyfillEnabled) {
const module = await import('css-vars-ponyfill')
const cssVars = module.default
cssVars({ variables })
} else {
for (const cssVar in variables) {
document.documentElement.style.setProperty(
`--${cssVar}`,
variables[cssVar]
)
}
}
setCSSVariables(variables)
}

if (
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/set-vars/native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function (variables: Record<string, string>) {
for (const cssVar in variables) {
document.documentElement.style.setProperty(`--${cssVar}`, variables[cssVar])
}
}
5 changes: 5 additions & 0 deletions src/runtime/set-vars/ponyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cssVars from 'css-vars-ponyfill'

export default function (variables: Record<string, string>) {
cssVars({ variables })
}