Skip to content

Commit e9f8c88

Browse files
committed
fix: force loading the mjs module when using nuxt
1 parent 141ad3e commit e9f8c88

File tree

7 files changed

+49
-17
lines changed

7 files changed

+49
-17
lines changed

.changeset/wise-suits-divide.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"vee-validate": patch
3+
"@vee-validate/valibot": patch
4+
"@vee-validate/nuxt": patch
5+
"@vee-validate/joi": patch
6+
"@vee-validate/yup": patch
7+
"@vee-validate/zod": patch
8+
---
9+
10+
fix: force loading the mjs module when using nuxt

packages/joi/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"types": "./dist/vee-validate-joi.d.ts",
1515
"import": "./dist/vee-validate-joi.mjs",
1616
"require": "./dist/vee-validate-joi.cjs"
17-
}
17+
},
18+
"./dist/*": "./dist/*"
1819
},
1920
"homepage": "https://vee-validate.logaretm.com/v4/integrations/joi-schema-validation/",
2021
"repository": {

packages/nuxt/src/module.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { defineNuxtModule, addComponent, addImports, logger } from '@nuxt/kit';
2-
import type { NuxtModule } from '@nuxt/schema';
1+
import { defineNuxtModule, addComponent, addImports, logger, resolveModule } from '@nuxt/kit';
2+
import type { Nuxt, NuxtModule } from '@nuxt/schema';
33
import { isPackageExists } from 'local-pkg';
44

55
type ComponentName = 'Field' | 'Form' | 'ErrorMessage' | 'FieldArray';
@@ -51,7 +51,7 @@ export default defineNuxtModule<VeeValidateNuxtOptions>({
5151
autoImports: true,
5252
componentNames: {},
5353
},
54-
setup(options) {
54+
setup(options, nuxt) {
5555
if (options.autoImports) {
5656
composables.forEach(composable => {
5757
addImports({
@@ -75,25 +75,27 @@ export default defineNuxtModule<VeeValidateNuxtOptions>({
7575
}
7676

7777
if (options.typedSchemaPackage === 'yup') {
78-
checkForYup(options);
78+
checkForYup(options, nuxt);
7979
return;
8080
}
8181

8282
if (options.typedSchemaPackage === 'zod') {
83-
checkForZod(options);
83+
checkForZod(options, nuxt);
8484
return;
8585
}
8686

8787
if (options.typedSchemaPackage === 'valibot') {
88-
checkForValibot(options);
88+
checkForValibot(options, nuxt);
8989
return;
9090
}
9191

92-
if (!checkForYup(options)) {
93-
if (!checkForZod(options)) {
94-
checkForValibot(options);
92+
if (!checkForYup(options, nuxt)) {
93+
if (!checkForZod(options, nuxt)) {
94+
checkForValibot(options, nuxt);
9595
}
9696
}
97+
98+
addMjsAlias('vee-validate', 'vee-validate', nuxt);
9799
},
98100
}) as NuxtModule<VeeValidateNuxtOptions>;
99101

@@ -113,7 +115,7 @@ function checkSchemaResolverDependencies(pkgName: (typeof schemaProviders)[numbe
113115
}
114116
}
115117

116-
function checkForValibot(options: VeeValidateNuxtOptions) {
118+
function checkForValibot(options: VeeValidateNuxtOptions, nuxt: Nuxt) {
117119
checkSchemaResolverDependencies('valibot');
118120
if (isPackageExists('@vee-validate/valibot') && isPackageExists('valibot')) {
119121
logger.info('Using valibot with vee-validate');
@@ -125,13 +127,15 @@ function checkForValibot(options: VeeValidateNuxtOptions) {
125127
});
126128
}
127129

130+
addMjsAlias('@vee-validate/valibot', 'vee-validate-valibot', nuxt);
131+
128132
return true;
129133
}
130134

131135
return false;
132136
}
133137

134-
function checkForZod(options: VeeValidateNuxtOptions) {
138+
function checkForZod(options: VeeValidateNuxtOptions, nuxt: Nuxt) {
135139
checkSchemaResolverDependencies('zod');
136140
if (isPackageExists('@vee-validate/zod') && isPackageExists('zod')) {
137141
logger.info('Using zod with vee-validate');
@@ -143,13 +147,15 @@ function checkForZod(options: VeeValidateNuxtOptions) {
143147
});
144148
}
145149

150+
addMjsAlias('@vee-validate/zod', 'vee-validate-zod', nuxt);
151+
146152
return true;
147153
}
148154

149155
return false;
150156
}
151157

152-
function checkForYup(options: VeeValidateNuxtOptions) {
158+
function checkForYup(options: VeeValidateNuxtOptions, nuxt: Nuxt) {
153159
checkSchemaResolverDependencies('yup');
154160
if (isPackageExists('@vee-validate/yup') && isPackageExists('yup')) {
155161
logger.info('Using yup with vee-validate');
@@ -161,12 +167,23 @@ function checkForYup(options: VeeValidateNuxtOptions) {
161167
});
162168
}
163169

170+
addMjsAlias('@vee-validate/yup', 'vee-validate-yup', nuxt);
171+
164172
return true;
165173
}
166174

167175
return false;
168176
}
169177

178+
function addMjsAlias(pkgName: string, fileName: string, nuxt: Nuxt) {
179+
// FIXME: Deprecated, idk why since it duplicate imports
180+
nuxt.options.alias[pkgName] =
181+
nuxt.options.alias[pkgName] ||
182+
resolveModule(`${pkgName}/dist/${fileName}.mjs`, {
183+
paths: [nuxt.options.rootDir, import.meta.url],
184+
});
185+
}
186+
170187
declare module '@nuxt/schema' {
171188
interface NuxtConfig {
172189
'vee-validate'?: VeeValidateNuxtOptions;

packages/valibot/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"types": "./dist/vee-validate-valibot.d.ts",
1414
"import": "./dist/vee-validate-valibot.mjs",
1515
"require": "./dist/vee-validate-valibot.cjs"
16-
}
16+
},
17+
"./dist/*": "./dist/*"
1718
},
1819
"types": "dist/vee-validate-valibot.d.ts",
1920
"homepage": "https://vee-validate.logaretm.com/v4/integrations/zod-schema-validation/",

packages/vee-validate/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"types": "./dist/vee-validate.d.ts",
1414
"import": "./dist/vee-validate.mjs",
1515
"require": "./dist/vee-validate.cjs"
16-
}
16+
},
17+
"./dist/*": "./dist/*"
1718
},
1819
"types": "dist/vee-validate.d.ts",
1920
"homepage": "https://vee-validate.logaretm.com/",

packages/yup/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"types": "./dist/vee-validate-yup.d.ts",
1515
"import": "./dist/vee-validate-yup.mjs",
1616
"require": "./dist/vee-validate-yup.cjs"
17-
}
17+
},
18+
"./dist/*": "./dist/*"
1819
},
1920
"homepage": "https://vee-validate.logaretm.com/v4/guide/composition-api/typed-schema/",
2021
"repository": {

packages/zod/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"types": "./dist/vee-validate-zod.d.ts",
1515
"import": "./dist/vee-validate-zod.mjs",
1616
"require": "./dist/vee-validate-zod.cjs"
17-
}
17+
},
18+
"./dist/*": "./dist/*"
1819
},
1920
"homepage": "https://vee-validate.logaretm.com/v4/integrations/zod-schema-validation/",
2021
"repository": {

0 commit comments

Comments
 (0)