diff --git a/packages/uniform/package.json b/packages/uniform/package.json index 73c0dfc7..70c75ceb 100644 --- a/packages/uniform/package.json +++ b/packages/uniform/package.json @@ -7,13 +7,40 @@ "license": "MIT", "type": "module", "exports": { - "./": "./dist/" - }, - "typesVersions": { - "*": { - "*": [ - "./dist/*" - ] + "./Form": { + "import": "./dist/Form/index.js", + "require": "./dist/Form/index.cjs", + "types": "./dist/Form/index.d.ts" + }, + "./Grid": { + "import": "./dist/Grid/index.js", + "require": "./dist/Grid/index.cjs", + "types": "./dist/Grid/index.d.ts" + }, + "./SubmitButton": { + "import": "./dist/SubmitButton/index.js", + "require": "./dist/SubmitButton/index.cjs", + "types": "./dist/SubmitButton/index.d.ts" + }, + "./helpers": { + "import": "./dist/helpers/index.js", + "require": "./dist/helpers/index.cjs", + "types": "./dist/helpers/index.d.ts" + }, + "./hooks": { + "import": "./dist/hooks/index.js", + "require": "./dist/hooks/index.cjs", + "types": "./dist/hooks/index.d.ts" + }, + "./partials/FieldCopyTestIdButton": { + "import": "./dist/partials/FieldCopyTestIdButton/index.js", + "require": "./dist/partials/FieldCopyTestIdButton/index.cjs", + "types": "./dist/partials/FieldCopyTestIdButton/index.d.ts" + }, + "./partials/FieldValidationError": { + "import": "./dist/partials/FieldValidationError/index.js", + "require": "./dist/partials/FieldValidationError/index.cjs", + "types": "./dist/partials/FieldValidationError/index.d.ts" } }, "files": [ @@ -68,4 +95,4 @@ "@types/react-dom": "18.2.22", "@types/slug": "5.0.8" } -} +} \ No newline at end of file diff --git a/packages/uniform/src/Form/Form.tsx b/packages/uniform/src/Form/Form.tsx index a0160cd8..84682ac0 100644 --- a/packages/uniform/src/Form/Form.tsx +++ b/packages/uniform/src/Form/Form.tsx @@ -7,7 +7,7 @@ import { useForm } from 'react-hook-form'; import cn from 'classnames'; -import slugify from '../helpers/slugify'; +import {slugify} from '../helpers'; import FormProvider from './subcomponents/FormContext'; import FormDebugViewer from './subcomponents/FormDebugViewer'; diff --git a/packages/uniform/src/SubmitButton/SubmitButton.tsx b/packages/uniform/src/SubmitButton/SubmitButton.tsx index 4d5dae5c..cdc1db0d 100644 --- a/packages/uniform/src/SubmitButton/SubmitButton.tsx +++ b/packages/uniform/src/SubmitButton/SubmitButton.tsx @@ -5,7 +5,7 @@ import cn from 'classnames'; import Button from '@fuf-stack/pixels/Button'; -import slugify from '../helpers/slugify'; +import { slugify } from '../helpers'; import { useFormContext } from '../hooks'; interface SubmitButtonProps { diff --git a/packages/uniform/src/helpers/index.ts b/packages/uniform/src/helpers/index.ts new file mode 100644 index 00000000..429eec58 --- /dev/null +++ b/packages/uniform/src/helpers/index.ts @@ -0,0 +1 @@ +export * from './slugify/slugify'; diff --git a/packages/uniform/src/helpers/slugify.test.ts b/packages/uniform/src/helpers/slugify/slugify.test.ts similarity index 100% rename from packages/uniform/src/helpers/slugify.test.ts rename to packages/uniform/src/helpers/slugify/slugify.test.ts diff --git a/packages/uniform/src/helpers/slugify.ts b/packages/uniform/src/helpers/slugify/slugify.ts similarity index 80% rename from packages/uniform/src/helpers/slugify.ts rename to packages/uniform/src/helpers/slugify/slugify.ts index 202218d5..2d287a78 100644 --- a/packages/uniform/src/helpers/slugify.ts +++ b/packages/uniform/src/helpers/slugify/slugify.ts @@ -1,8 +1,10 @@ +/* eslint-disable import/prefer-default-export */ + import type { Options as SlugOptions } from 'slug'; import slug from 'slug'; -export default (string: string, options?: SlugOptions) => { +export const slugify = (string: string, options?: SlugOptions) => { const replacement = options?.replacement || '_'; return slug(string, { ...slug.defaults.modes.rfc3986, diff --git a/packages/uniform/src/hooks/useFormContext/useFormContext.ts b/packages/uniform/src/hooks/useFormContext/useFormContext.ts index e81e962f..00798628 100644 --- a/packages/uniform/src/hooks/useFormContext/useFormContext.ts +++ b/packages/uniform/src/hooks/useFormContext/useFormContext.ts @@ -5,7 +5,7 @@ import { useContext } from 'react'; import { useFormContext as useHookFormContext } from 'react-hook-form'; import { ValidationSchemaContext } from '../../Form/subcomponents/FormContext'; -import slugify from '../../helpers/slugify'; +import { slugify } from '../../helpers'; // FIX: This fixes the problem that the innerType is not checked for optionals... const recursiveSearchInnerType = (schema: VetoSchema): boolean => { diff --git a/packages/uniform/src/partials/FieldValidationError/FieldValidationError.tsx b/packages/uniform/src/partials/FieldValidationError/FieldValidationError.tsx index f0649056..e646a6d6 100644 --- a/packages/uniform/src/partials/FieldValidationError/FieldValidationError.tsx +++ b/packages/uniform/src/partials/FieldValidationError/FieldValidationError.tsx @@ -1,6 +1,6 @@ import type { FieldError } from 'react-hook-form'; -import slugify from '../../helpers/slugify'; +import { slugify } from '../../helpers'; interface FieldValidationErrorProps { error: FieldError[] | Record; diff --git a/packages/uniform/tsup.config.ts b/packages/uniform/tsup.config.ts index 20bfaeea..aa80b0d9 100644 --- a/packages/uniform/tsup.config.ts +++ b/packages/uniform/tsup.config.ts @@ -1,21 +1,51 @@ -// eslint-disable-next-line import/no-extraneous-dependencies +/* eslint-disable import/no-extraneous-dependencies */ + +import fs from 'fs'; +import path from 'path'; + import { defineConfig } from 'tsup'; +function getAllFilePaths(dirPath: string): string[] { + return fs.readdirSync(dirPath).reduce((allFiles, file) => { + const fullPath = path.join(dirPath, file); + if (fs.statSync(fullPath).isDirectory()) { + return allFiles.concat(getAllFilePaths(fullPath)); + } + return allFiles.concat(`./${fullPath}`); + }, []); +} + export default defineConfig({ + entry: getAllFilePaths('./src') + .flat() + .filter((file) => file.endsWith('index.ts')), + format: ['esm', 'cjs'], + splitting: true, + sourcemap: true, clean: true, dts: true, - entry: [ - 'src', - '!src/**/__snapshots__/**', - '!src/**/*.module.css', - '!src/**/*.stories.*', - '!src/**/*.test.*', - ], - format: ['cjs', 'esm'], - loader: { - // for css modules - // see: https://github.com/egoist/tsup/issues/536 - '.css': 'local-css', + outDir: 'dist', + // update exports of package.json + onSuccess: async () => { + const packageJsonPath = './package.json'; + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); + const distIndexFiles = getAllFilePaths('./dist').filter((file) => + file.endsWith('index.js'), + ); + + packageJson.exports = distIndexFiles.reduce< + Record + >((exports, file) => { + const key = file.replace('dist/', '').replace('/index.js', ''); + // eslint-disable-next-line no-param-reassign + exports[key] = { + import: file, + require: file.replace('.js', '.cjs'), + types: file.replace('.js', '.d.ts'), + }; + return exports; + }, {}); + + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); }, - sourcemap: true, });