Skip to content

Commit

Permalink
fix(uniform): add logic to add exports with tsup
Browse files Browse the repository at this point in the history
  • Loading branch information
toxsick committed Apr 15, 2024
1 parent 6bb9bc7 commit 589c05c
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 27 deletions.
43 changes: 35 additions & 8 deletions packages/uniform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -68,4 +95,4 @@
"@types/react-dom": "18.2.22",
"@types/slug": "5.0.8"
}
}
}
2 changes: 1 addition & 1 deletion packages/uniform/src/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/uniform/src/SubmitButton/SubmitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/uniform/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './slugify/slugify';
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, FieldError[]>;
Expand Down
58 changes: 44 additions & 14 deletions packages/uniform/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -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<string[]>((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<string, { import: string; require: string; types: string }>
>((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,
});

0 comments on commit 589c05c

Please sign in to comment.