@@ -4,9 +4,11 @@ import {
44 addPlugin ,
55 addComponentsDir ,
66 extendViteConfig ,
7- addPluginTemplate ,
87} from '@nuxt/kit'
9- import path from 'pathe'
8+ import { join as joinPath } from 'pathe'
9+ import { defu } from 'defu'
10+ import { joinURL } from 'ufo'
11+ import { version as PDFJS_VERSION } from 'pdfjs-dist/legacy/build/pdf.mjs'
1012
1113export interface ModuleOptions {
1214 /**
@@ -26,19 +28,25 @@ export interface ModuleOptions {
2628 useLocalPdfWorker ?: boolean ,
2729}
2830
31+ export interface ModulePublicRuntimeConfig {
32+ persona : {
33+ cdnURL : string ,
34+ } ,
35+ }
36+
2937export default defineNuxtModule < ModuleOptions > ( {
3038 meta : {
3139 name : '@privyid/persona' ,
3240 configKey : 'persona' ,
33- compatibility : { nuxt : '>=3.0 .0' } ,
41+ compatibility : { nuxt : '>=3.4 .0' } ,
3442 } ,
3543 defaults : {
3644 font : true ,
3745 prefix : 'p' ,
3846 useLocalPdfWorker : false ,
3947 } ,
4048 async setup ( options , nuxt ) {
41- const { resolve } = createResolver ( import . meta. url )
49+ const { resolve, resolvePath } = createResolver ( import . meta. url )
4250
4351 // Add font CDN
4452 if ( options . font ) {
@@ -62,7 +70,7 @@ export default defineNuxtModule<ModuleOptions>({
6270 } )
6371
6472 // Add Plugin
65- addPlugin ( { src : resolve ( './runtime/plugin ' ) } )
73+ addPlugin ( { src : resolve ( './runtime/plugins/persona ' ) } )
6674
6775 // Extend vite config
6876 extendViteConfig ( ( config ) => {
@@ -82,45 +90,37 @@ export default defineNuxtModule<ModuleOptions>({
8290
8391 // Use local pdf worker
8492 if ( options . useLocalPdfWorker ) {
93+ nuxt . options . runtimeConfig . public . persona = defu (
94+ { cdnURL : './_persona' } ,
95+ nuxt . options . runtimeConfig . public . persona ,
96+ )
97+
8598 nuxt . hook ( 'nitro:config' , async ( nitroConfig ) => {
8699 // eslint-disable-next-line align-assignments/align-assignments
87100 nitroConfig . publicAssets ||= [ ]
88101
89- // eslint-disable-next-line unicorn/prefer-module
90- const pdfjsDir = path . dirname ( require . resolve ( 'pdfjs-dist' ) )
91- const { default : { version } } = await import ( 'pdfjs-dist/package.json' )
92- const pdfjsBaseUrl = `_persona/pdjs-dist@${ version } `
93-
94- const assetsMaxAge = 60 * 60 * 24 * 30
102+ const pdfjsDir = joinPath ( await resolvePath ( 'pdfjs-dist' ) , '../../' )
103+ const pdfjsBaseURL = `_persona/pdfjs-dist@${ PDFJS_VERSION } `
104+ const assetsMaxAge = 60 * 60 * 24 * 30 // 1 month
95105
96106 nitroConfig . publicAssets . push (
97107 {
98- baseURL : ` ${ pdfjsBaseUrl } / build` ,
99- dir : pdfjsDir ,
108+ baseURL : joinURL ( pdfjsDir , ' build' ) ,
109+ dir : joinPath ( pdfjsDir , 'build' ) ,
100110 maxAge : assetsMaxAge ,
101111 } ,
102112 {
103- baseURL : `${ pdfjsBaseUrl } /cmaps` ,
104- dir : path . join ( pdfjsDir , '../cmaps' ) ,
113+ baseURL : joinURL ( pdfjsBaseURL , 'legacy' ) ,
114+ dir : joinPath ( pdfjsDir , 'legacy' ) ,
115+ maxAge : assetsMaxAge ,
116+ } ,
117+ {
118+ baseURL : joinURL ( pdfjsBaseURL , 'cmaps' ) ,
119+ dir : joinPath ( pdfjsDir , 'cmaps' ) ,
105120 maxAge : assetsMaxAge ,
106121 } ,
107122 )
108123 } )
109-
110- addPluginTemplate ( {
111- filename : 'persona-local-pdf-worker.mjs' ,
112- getContents : ( ) => `
113- import { setCDN } from '@privyid/persona/core'
114-
115- export default defineNuxtPlugin({
116- name: 'persona-local-pdf-worker',
117- dependsOn: ['persona-setup'],
118- setup () {
119- setCDN('/_persona/')
120- },
121- })
122- ` ,
123- } )
124124 }
125125 } ,
126126} )
0 commit comments