Skip to content

Commit

Permalink
feat: import composable
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Dec 7, 2022
1 parent 8dfe66a commit a07f344
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 190 deletions.
57 changes: 26 additions & 31 deletions playground/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
export default defineNuxtConfig({
cookies: {
necessary: [
{
name: {
de: 'Notwendiger Cookie',
en: 'Necessary Cookie',
},
cookies: ['NEC'],
},
],
optional: [
{
name: 'Optional Cookie',
identifier: 'op',
cookies: ['_o', '_p', '_t'],
accepted: () => {
// const app = useNuxtApp()
alert('accepted')
},
declined: () => {
// const app = useNuxtApp()
alert('declined')
},
},
],
},
modules: [
[
'@dargmuesli/nuxt-cookie-control',
{
locales: ['en', 'de'],
cookies: {
necessary: [
{
name: {
de: 'Notwendiger Cookie',
en: 'Necessary Cookie',
},
cookies: ['NEC'],
},
],
optional: [
{
name: 'Optional Cookie',
identifier: 'op',
cookies: ['_o', '_p', '_t'],
accepted: () => {
// const app = useNuxtApp()
alert('accepted')
},
declined: () => {
// const app = useNuxtApp()
alert('declined')
},
},
],
},
},
],
],
typescript: {
tsConfig: {
include: ['../../**/*'], // https://github.com/nuxt/framework/pull/7726
},
},
})
26 changes: 15 additions & 11 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { resolve } from 'path'
import { fileURLToPath } from 'url'

import {
defineNuxtModule,
Expand All @@ -8,23 +7,28 @@ import {
extendWebpackConfig,
addWebpackPlugin,
addTemplate,
addImports,
createResolver,
} from '@nuxt/kit'
import webpack from 'webpack' // eslint-disable-line import/no-named-as-default

import { name, version } from '../package.json'
import { DEFAULTS, ModuleOptions } from './runtime/types'

const resolver = createResolver(import.meta.url)

export default defineNuxtModule<ModuleOptions>({
meta: {
name,
version,
configKey: 'cookieControl',
compatibility: { nuxt: '^3.0.0' },
},
defaults: DEFAULTS,
hooks: {
'components:dirs'(dirs) {
dirs.push({
path: fileURLToPath(new URL('./runtime/components', import.meta.url)),
path: resolver.resolve('./runtime/components'),
prefix: 'cookie',
})
},
Expand All @@ -38,9 +42,7 @@ export default defineNuxtModule<ModuleOptions>({
// const options = Object.assign(defaultOptions, _options)

if (moduleOptions.css) {
nuxt.options.css.push(
fileURLToPath(new URL('./runtime/styles.css', import.meta.url))
)
nuxt.options.css.push(resolver.resolve('./runtime/styles.css'))
}

if (moduleOptions.blockIframe) {
Expand Down Expand Up @@ -88,15 +90,17 @@ export default defineNuxtModule<ModuleOptions>({
// addVitePlugin(vitePlugin, options?)
}

const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
const runtimeDir = resolver.resolve('./runtime')

nuxt.options.build.transpile.push(runtimeDir)
addPlugin(resolve(runtimeDir, 'plugin'))
nuxt.options.alias['#nuxtCookieControl'] = runtimeDir
// nuxt.addPlugin({
// src: path.resolve(__dirname, 'plugin.js'),
// fileName: 'nuxt-cookie-control.js',
// options
// })

addImports({
name: 'useCookieControl',
as: 'useCookieControl',
from: resolve(runtimeDir, 'composables'),
})

addTemplate({
filename: 'nuxtCookieControl.options.ts',
Expand Down
Loading

0 comments on commit a07f344

Please sign in to comment.