|
1 | | -import { defineNuxtModule, addComponent, addImports, logger, resolveModule } from '@nuxt/kit'; |
| 1 | +import { defineNuxtModule, addComponent, addImports, resolveModule } from '@nuxt/kit'; |
2 | 2 | import type { Nuxt, NuxtModule } from '@nuxt/schema'; |
3 | | -import { isPackageExists } from 'local-pkg'; |
4 | 3 |
|
5 | 4 | type ComponentName = 'Field' | 'Form' | 'ErrorMessage' | 'FieldArray'; |
6 | 5 | type TypedSchemaPackage = 'yup' | 'zod' | 'valibot' | 'none'; |
@@ -37,13 +36,6 @@ const composables = [ |
37 | 36 | 'useValidateForm', |
38 | 37 | ]; |
39 | 38 |
|
40 | | -const schemaProviders = ['zod', 'yup', 'valibot'] as const; |
41 | | -const schemaProviderResolvers: Record<(typeof schemaProviders)[number], string> = { |
42 | | - zod: '@vee-validate/zod', |
43 | | - yup: '@vee-validate/yup', |
44 | | - valibot: '@vee-validate/valibot', |
45 | | -}; |
46 | | - |
47 | 39 | export default defineNuxtModule<VeeValidateNuxtOptions>({ |
48 | 40 | meta: { |
49 | 41 | name: 'vee-validate', |
@@ -78,106 +70,9 @@ export default defineNuxtModule<VeeValidateNuxtOptions>({ |
78 | 70 | if (options.typedSchemaPackage === 'none') { |
79 | 71 | return; |
80 | 72 | } |
81 | | - |
82 | | - if (options.typedSchemaPackage === 'yup') { |
83 | | - checkForYup(options, nuxt); |
84 | | - return; |
85 | | - } |
86 | | - |
87 | | - if (options.typedSchemaPackage === 'zod') { |
88 | | - checkForZod(options, nuxt); |
89 | | - return; |
90 | | - } |
91 | | - |
92 | | - if (options.typedSchemaPackage === 'valibot') { |
93 | | - checkForValibot(options, nuxt); |
94 | | - return; |
95 | | - } |
96 | | - |
97 | | - if (!checkForYup(options, nuxt)) { |
98 | | - if (!checkForZod(options, nuxt)) { |
99 | | - checkForValibot(options, nuxt); |
100 | | - } |
101 | | - } |
102 | 73 | }, |
103 | 74 | }) as NuxtModule<VeeValidateNuxtOptions>; |
104 | 75 |
|
105 | | -function checkSchemaResolverDependencies(pkgName: (typeof schemaProviders)[number]) { |
106 | | - const makeMsg = (installed: string, uninstalled: string) => |
107 | | - `You seem to be using ${installed}, but you have not installed ${uninstalled}. Please install it to use ${pkgName} with vee-validate.`; |
108 | | - |
109 | | - const resolverPkg = schemaProviderResolvers[pkgName]; |
110 | | - if (isPackageExists(pkgName) && !isPackageExists(resolverPkg)) { |
111 | | - logger.warn(makeMsg(pkgName, resolverPkg)); |
112 | | - return true; |
113 | | - } |
114 | | - |
115 | | - if (isPackageExists(resolverPkg) && !isPackageExists(pkgName)) { |
116 | | - logger.warn(makeMsg(resolverPkg, pkgName)); |
117 | | - return true; |
118 | | - } |
119 | | -} |
120 | | - |
121 | | -function checkForValibot(options: VeeValidateNuxtOptions, nuxt: Nuxt) { |
122 | | - checkSchemaResolverDependencies('valibot'); |
123 | | - if (isPackageExists('@vee-validate/valibot') && isPackageExists('valibot')) { |
124 | | - logger.info('Using valibot with vee-validate'); |
125 | | - if (options.autoImports) { |
126 | | - addImports({ |
127 | | - name: 'toTypedSchema', |
128 | | - as: 'toTypedSchema', |
129 | | - from: '@vee-validate/valibot', |
130 | | - }); |
131 | | - } |
132 | | - |
133 | | - addMjsAlias('@vee-validate/valibot', 'vee-validate-valibot', nuxt); |
134 | | - |
135 | | - return true; |
136 | | - } |
137 | | - |
138 | | - return false; |
139 | | -} |
140 | | - |
141 | | -function checkForZod(options: VeeValidateNuxtOptions, nuxt: Nuxt) { |
142 | | - checkSchemaResolverDependencies('zod'); |
143 | | - if (isPackageExists('@vee-validate/zod') && isPackageExists('zod')) { |
144 | | - logger.info('Using zod with vee-validate'); |
145 | | - if (options.autoImports) { |
146 | | - addImports({ |
147 | | - name: 'toTypedSchema', |
148 | | - as: 'toTypedSchema', |
149 | | - from: '@vee-validate/zod', |
150 | | - }); |
151 | | - } |
152 | | - |
153 | | - addMjsAlias('@vee-validate/zod', 'vee-validate-zod', nuxt); |
154 | | - |
155 | | - return true; |
156 | | - } |
157 | | - |
158 | | - return false; |
159 | | -} |
160 | | - |
161 | | -function checkForYup(options: VeeValidateNuxtOptions, nuxt: Nuxt) { |
162 | | - checkSchemaResolverDependencies('yup'); |
163 | | - if (isPackageExists('@vee-validate/yup') && isPackageExists('yup')) { |
164 | | - logger.info('Using yup with vee-validate'); |
165 | | - if (options.autoImports) { |
166 | | - addImports({ |
167 | | - name: 'toTypedSchema', |
168 | | - as: 'toTypedSchema', |
169 | | - from: '@vee-validate/yup', |
170 | | - }); |
171 | | - } |
172 | | - |
173 | | - addMjsAlias('@vee-validate/yup', 'vee-validate-yup', nuxt); |
174 | | - |
175 | | - return true; |
176 | | - } |
177 | | - |
178 | | - return false; |
179 | | -} |
180 | | - |
181 | 76 | function addMjsAlias(pkgName: string, fileName: string, nuxt: Nuxt) { |
182 | 77 | // FIXME: Deprecated, idk why since it duplicate imports |
183 | 78 | nuxt.options.alias[pkgName] = |
|
0 commit comments