Skip to content

Commit

Permalink
feat: lemon sqeezy (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw authored Apr 22, 2024
1 parent 53a16db commit f3827c1
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 116 deletions.
5 changes: 5 additions & 0 deletions playground/pages/third-parties/lemon-squeezy.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<LemonSqueezyButton href="https://harlantest.lemonsqueezy.com/buy/52a40427-36d2-4450-a514-ae80d9e1a333?embed=1">
Buy Test
</LemonSqueezyButton>
</template>
120 changes: 4 additions & 116 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
hasNuxtModule,
} from '@nuxt/kit'
import { readPackageJSON } from 'pkg-types'
import { joinURL, withBase, withQuery } from 'ufo'
import { lt } from 'semver'
import { resolvePath } from 'mlly'
import { join } from 'pathe'
Expand All @@ -19,10 +18,7 @@ import { NuxtScriptAssetBundlerTransformer } from './plugins/transform'
import { setupPublicAssetStrategy } from './assets'
import { logger } from './logger'
import { extendTypes, installNuxtModule } from './kit'
import type { IntercomInput } from './runtime/registry/intercom'
import type { SegmentInput } from './runtime/registry/segment'
import type { HotjarInput } from './runtime/registry/hotjar'
import type { NpmInput } from './runtime/registry/npm'
import { registry } from './registry'
import type { NuxtUseScriptInput, NuxtUseScriptOptions, RegistryScripts, ScriptRegistry } from '#nuxt-scripts'

export interface ModuleOptions {
Expand Down Expand Up @@ -96,6 +92,7 @@ export default defineNuxtModule<ModuleOptions>({
},
async setup(config, nuxt) {
const { resolve } = createResolver(import.meta.url)

const { version, name } = await readPackageJSON(resolve('../package.json'))
const { version: unheadVersion } = await readPackageJSON(join(await resolvePath('@unhead/vue'), 'package.json'))

Expand All @@ -121,120 +118,11 @@ export default defineNuxtModule<ModuleOptions>({
})

nuxt.hooks.hook('modules:done', async () => {
let registry: RegistryScripts = [
{
name: 'useScriptCloudflareWebAnalytics',
key: 'cloudflareWebAnalytics',
from: resolve('./runtime/registry/cloudflare-web-analytics'),
src: 'https://static.cloudflareinsights.com/beacon.min.js',
},
{
name: 'useScriptConfetti',
key: 'confetti',
from: resolve('./runtime/registry/confetti'),
src: 'https://unpkg.com/js-confetti@latest/dist/js-confetti.browser.js',
},
{
name: 'useScriptFacebookPixel',
key: 'facebookPixel',
from: resolve('./runtime/registry/facebook-pixel'),
src: 'https://connect.facebook.net/en_US/fbevents.js',
},
{
name: 'useScriptXPixel',
key: 'xPixel',
from: resolve('./runtime/registry/x-pixel'),
src: 'https://static.ads-twitter.com/uwt.js',
},
{
name: 'useScriptFathomAnalytics',
key: 'fathomAnalytics',
from: resolve('./runtime/registry/fathom-analytics'),
src: false, // can not be bundled, breaks script
},
{
name: 'useScriptMatomoAnalytics',
key: 'matomoAnalytics',
from: resolve('./runtime/registry/matomo-analytics'),
src: false, // can not be bundled, breaks script
},
{
name: 'useScriptStripe',
key: 'stripe',
from: resolve('./runtime/registry/stripe'),
src: false, // can not be bundled, breaks script
},
{
name: 'useScriptHotjar',
from: resolve('./runtime/registry/hotjar'),
key: 'hotjar',
transform(options?: HotjarInput) {
return withQuery(`https://static.hotjar.com/c/hotjar-${options?.id || ''}.js`, {
sv: options?.sv || '6',
})
},
},
{
name: 'useScriptVimeoPlayer',
from: resolve('./runtime/registry/vimeo-player'),
key: 'vimeoPlayer',
},
{
name: 'useScriptIntercom',
from: resolve('./runtime/registry/intercom'),
key: 'intercom',
transform(options?: IntercomInput) {
return joinURL(`https://widget.intercom.io/widget`, options?.app_id || '')
},
},
{
name: 'useScriptSegment',
from: resolve('./runtime/registry/segment'),
key: 'segment',
transform(options?: SegmentInput) {
return joinURL('https://cdn.segment.com/analytics.js/v1', options?.writeKey || '', 'analytics.min.js')
},
},
{
name: 'useScriptNpm',
// key is based on package name
from: resolve('./runtime/registry/npm'),
transform(options?: NpmInput) {
return withBase(options?.file || '', `https://unpkg.com/${options?.packageName || ''}@${options?.version || 'latest'}`)
},
},
{
name: 'useScriptGoogleAnalytics',
key: 'googleAnalytics',
from: resolve('./runtime/registry/google-analytics'),
},
{
name: 'useScriptGoogleTagManager',
key: 'googleTagManager',
from: resolve('./runtime/registry/google-tag-manager'),
},
{
name: 'useScriptGoogleMaps',
key: 'googleMaps',
from: resolve('./runtime/registry/google-maps'),
},
{
name: 'useScriptYouTubeIframe',
key: 'youtubeIframe',
from: resolve('./runtime/registry/youtube-iframe'),
},
{
name: 'useScriptPlausibleAnalytics',
key: 'plausibleAnalytics',
from: resolve('./runtime/registry/plausible-analytics'),
},
]
registry = registry.map((i) => {
addImports(registry.map((i) => {
i.priority = -1
i.module = i.module || '@nuxt/scripts'
return i
})
addImports(registry)
}))

// @ts-expect-error runtime
await nuxt.hooks.callHook('scripts:registry', registry)
Expand Down
124 changes: 124 additions & 0 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { joinURL, withBase, withQuery } from 'ufo'
import { createResolver } from '@nuxt/kit'
import type { RegistryScripts } from '#nuxt-scripts'
import type { HotjarInput } from '~/src/runtime/registry/hotjar'
import type { IntercomInput } from '~/src/runtime/registry/intercom'
import type { SegmentInput } from '~/src/runtime/registry/segment'
import type { NpmInput } from '~/src/runtime/registry/npm'

const { resolve } = createResolver(import.meta.url)

export const registry: RegistryScripts = [
// analytics
{
name: 'useScriptGoogleAnalytics',
key: 'googleAnalytics',
from: resolve('./runtime/registry/google-analytics'),
},
{
name: 'useScriptPlausibleAnalytics',
key: 'plausibleAnalytics',
from: resolve('./runtime/registry/plausible-analytics'),
},
{
name: 'useScriptCloudflareWebAnalytics',
key: 'cloudflareWebAnalytics',
from: resolve('./runtime/registry/cloudflare-web-analytics'),
src: 'https://static.cloudflareinsights.com/beacon.min.js',
},
{
name: 'useScriptFathomAnalytics',
key: 'fathomAnalytics',
from: resolve('./runtime/registry/fathom-analytics'),
src: false, // can not be bundled, breaks script
},
{
name: 'useScriptMatomoAnalytics',
key: 'matomoAnalytics',
from: resolve('./runtime/registry/matomo-analytics'),
src: false, // can not be bundled, breaks script
},
// tracking
{
name: 'useScriptGoogleTagManager',
key: 'googleTagManager',
from: resolve('./runtime/registry/google-tag-manager'),
},
{
name: 'useScriptSegment',
from: resolve('./runtime/registry/segment'),
key: 'segment',
transform(options?: SegmentInput) {
return joinURL('https://cdn.segment.com/analytics.js/v1', options?.writeKey || '', 'analytics.min.js')
},
},
{
name: 'useScriptFacebookPixel',
key: 'facebookPixel',
from: resolve('./runtime/registry/facebook-pixel'),
src: 'https://connect.facebook.net/en_US/fbevents.js',
},
{
name: 'useScriptXPixel',
key: 'xPixel',
from: resolve('./runtime/registry/x-pixel'),
src: 'https://static.ads-twitter.com/uwt.js',
},
// payments
{
name: 'useScriptStripe',
key: 'stripe',
from: resolve('./runtime/registry/stripe'),
src: false, // can not be bundled, breaks script
},
{
name: 'useScriptLemonSqueezy',
key: 'lemonSqueezy',
from: resolve('./runtime/registry/lemon-squeezy'),
src: false, // should not be bundled
},
// content
{
name: 'useScriptVimeoPlayer',
from: resolve('./runtime/registry/vimeo-player'),
key: 'vimeoPlayer',
},
{
name: 'useScriptYouTubeIframe',
key: 'youtubeIframe',
from: resolve('./runtime/registry/youtube-iframe'),
},
{
name: 'useScriptGoogleMaps',
key: 'googleMaps',
from: resolve('./runtime/registry/google-maps'),
},
// tools
{
name: 'useScriptIntercom',
from: resolve('./runtime/registry/intercom'),
key: 'intercom',
transform(options?: IntercomInput) {
return joinURL(`https://widget.intercom.io/widget`, options?.app_id || '')
},
},
{
name: 'useScriptHotjar',
from: resolve('./runtime/registry/hotjar'),
key: 'hotjar',
transform(options?: HotjarInput) {
return withQuery(`https://static.hotjar.com/c/hotjar-${options?.id || ''}.js`, {
sv: options?.sv || '6',
})
},
},
// other
{
name: 'useScriptNpm',
// key is based on package name
from: resolve('./runtime/registry/npm'),
transform(options?: NpmInput) {
return withBase(options?.file || '', `https://unpkg.com/${options?.packageName || ''}@${options?.version || 'latest'}`)
},
},
]
28 changes: 28 additions & 0 deletions src/runtime/components/LemonSqueezyButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="ts" setup>
import { useScriptLemonSqueezy } from '#imports'
defineProps<{
href: string
}>()
const emits = defineEmits<{
event: [{ event: string, data?: Record<string, any> }]
}>()
const instance = useScriptLemonSqueezy()
instance.Setup({
eventHandler(event) {
emits('event', event)
},
})
defineExpose({
instance,
})
</script>

<template>
<a :href="href" class="lemonsqueezy-button">
<slot />
</a>
</template>
Loading

0 comments on commit f3827c1

Please sign in to comment.