Skip to content

Commit

Permalink
fix: force loading the mjs module when using nuxt
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Oct 26, 2024
1 parent 141ad3e commit e9f8c88
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 17 deletions.
10 changes: 10 additions & 0 deletions .changeset/wise-suits-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"vee-validate": patch
"@vee-validate/valibot": patch
"@vee-validate/nuxt": patch
"@vee-validate/joi": patch
"@vee-validate/yup": patch
"@vee-validate/zod": patch
---

fix: force loading the mjs module when using nuxt
3 changes: 2 additions & 1 deletion packages/joi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"types": "./dist/vee-validate-joi.d.ts",
"import": "./dist/vee-validate-joi.mjs",
"require": "./dist/vee-validate-joi.cjs"
}
},
"./dist/*": "./dist/*"
},
"homepage": "https://vee-validate.logaretm.com/v4/integrations/joi-schema-validation/",
"repository": {
Expand Down
41 changes: 29 additions & 12 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineNuxtModule, addComponent, addImports, logger } from '@nuxt/kit';
import type { NuxtModule } from '@nuxt/schema';
import { defineNuxtModule, addComponent, addImports, logger, resolveModule } from '@nuxt/kit';
import type { Nuxt, NuxtModule } from '@nuxt/schema';
import { isPackageExists } from 'local-pkg';

type ComponentName = 'Field' | 'Form' | 'ErrorMessage' | 'FieldArray';
Expand Down Expand Up @@ -51,7 +51,7 @@ export default defineNuxtModule<VeeValidateNuxtOptions>({
autoImports: true,
componentNames: {},
},
setup(options) {
setup(options, nuxt) {
if (options.autoImports) {
composables.forEach(composable => {
addImports({
Expand All @@ -75,25 +75,27 @@ export default defineNuxtModule<VeeValidateNuxtOptions>({
}

if (options.typedSchemaPackage === 'yup') {
checkForYup(options);
checkForYup(options, nuxt);
return;
}

if (options.typedSchemaPackage === 'zod') {
checkForZod(options);
checkForZod(options, nuxt);
return;
}

if (options.typedSchemaPackage === 'valibot') {
checkForValibot(options);
checkForValibot(options, nuxt);
return;
}

if (!checkForYup(options)) {
if (!checkForZod(options)) {
checkForValibot(options);
if (!checkForYup(options, nuxt)) {
if (!checkForZod(options, nuxt)) {
checkForValibot(options, nuxt);
}
}

addMjsAlias('vee-validate', 'vee-validate', nuxt);
},
}) as NuxtModule<VeeValidateNuxtOptions>;

Expand All @@ -113,7 +115,7 @@ function checkSchemaResolverDependencies(pkgName: (typeof schemaProviders)[numbe
}
}

function checkForValibot(options: VeeValidateNuxtOptions) {
function checkForValibot(options: VeeValidateNuxtOptions, nuxt: Nuxt) {
checkSchemaResolverDependencies('valibot');
if (isPackageExists('@vee-validate/valibot') && isPackageExists('valibot')) {
logger.info('Using valibot with vee-validate');
Expand All @@ -125,13 +127,15 @@ function checkForValibot(options: VeeValidateNuxtOptions) {
});
}

addMjsAlias('@vee-validate/valibot', 'vee-validate-valibot', nuxt);

return true;
}

return false;
}

function checkForZod(options: VeeValidateNuxtOptions) {
function checkForZod(options: VeeValidateNuxtOptions, nuxt: Nuxt) {
checkSchemaResolverDependencies('zod');
if (isPackageExists('@vee-validate/zod') && isPackageExists('zod')) {
logger.info('Using zod with vee-validate');
Expand All @@ -143,13 +147,15 @@ function checkForZod(options: VeeValidateNuxtOptions) {
});
}

addMjsAlias('@vee-validate/zod', 'vee-validate-zod', nuxt);

return true;
}

return false;
}

function checkForYup(options: VeeValidateNuxtOptions) {
function checkForYup(options: VeeValidateNuxtOptions, nuxt: Nuxt) {
checkSchemaResolverDependencies('yup');
if (isPackageExists('@vee-validate/yup') && isPackageExists('yup')) {
logger.info('Using yup with vee-validate');
Expand All @@ -161,12 +167,23 @@ function checkForYup(options: VeeValidateNuxtOptions) {
});
}

addMjsAlias('@vee-validate/yup', 'vee-validate-yup', nuxt);

return true;
}

return false;
}

function addMjsAlias(pkgName: string, fileName: string, nuxt: Nuxt) {
// FIXME: Deprecated, idk why since it duplicate imports
nuxt.options.alias[pkgName] =
nuxt.options.alias[pkgName] ||
resolveModule(`${pkgName}/dist/${fileName}.mjs`, {
paths: [nuxt.options.rootDir, import.meta.url],
});
}

declare module '@nuxt/schema' {
interface NuxtConfig {
'vee-validate'?: VeeValidateNuxtOptions;
Expand Down
3 changes: 2 additions & 1 deletion packages/valibot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"types": "./dist/vee-validate-valibot.d.ts",
"import": "./dist/vee-validate-valibot.mjs",
"require": "./dist/vee-validate-valibot.cjs"
}
},
"./dist/*": "./dist/*"
},
"types": "dist/vee-validate-valibot.d.ts",
"homepage": "https://vee-validate.logaretm.com/v4/integrations/zod-schema-validation/",
Expand Down
3 changes: 2 additions & 1 deletion packages/vee-validate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"types": "./dist/vee-validate.d.ts",
"import": "./dist/vee-validate.mjs",
"require": "./dist/vee-validate.cjs"
}
},
"./dist/*": "./dist/*"
},
"types": "dist/vee-validate.d.ts",
"homepage": "https://vee-validate.logaretm.com/",
Expand Down
3 changes: 2 additions & 1 deletion packages/yup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"types": "./dist/vee-validate-yup.d.ts",
"import": "./dist/vee-validate-yup.mjs",
"require": "./dist/vee-validate-yup.cjs"
}
},
"./dist/*": "./dist/*"
},
"homepage": "https://vee-validate.logaretm.com/v4/guide/composition-api/typed-schema/",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion packages/zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"types": "./dist/vee-validate-zod.d.ts",
"import": "./dist/vee-validate-zod.mjs",
"require": "./dist/vee-validate-zod.cjs"
}
},
"./dist/*": "./dist/*"
},
"homepage": "https://vee-validate.logaretm.com/v4/integrations/zod-schema-validation/",
"repository": {
Expand Down

0 comments on commit e9f8c88

Please sign in to comment.