Skip to content

Commit 9cbaf25

Browse files
committed
Enhance README with development setup instructions and improve clarity; fix import path in index.ts
1 parent 6e5bee0 commit 9cbaf25

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,99 @@ You can customize `useAJVForm` using the following options:
218218
## Usage in Practice
219219
220220
To see `useAJVForm` in action, visit our [Yail Storybook](https://yail.programmer.network/?path=/docs/input-forms--docs). Click on `Show code` to explore practical usage examples of the hook.
221+
222+
223+
Sure! Your explanation and example are already clear, but it could be slightly improved for clarity, readability, and providing additional flexibility. Here's an improved version that is concise, polished, and includes a few additional tips.
224+
225+
---
226+
227+
## Development
228+
229+
To continuously improve or extend this library during development, the most efficient approach is to configure a **Vite alias** within your main project. This allows you to reference the library’s source code (`index.ts`) directly, skipping the need to build or publish it repeatedly.
230+
231+
#### Step 1: Setup Conditional Alias in `vite.config.ts`
232+
233+
Update your Vite configuration to conditionally alias the library path when using your local version:
234+
235+
```ts
236+
import path from "path";
237+
import { defineConfig } from "vite";
238+
239+
export default defineConfig(() => {
240+
// Check if the USE_LOCAL_AJV environment variable is true
241+
const useLocalAjv = process.env.USE_LOCAL_AJV === "true";
242+
243+
return {
244+
resolve: {
245+
alias: {
246+
// Use a local alias only when USE_LOCAL_AJV is enabled
247+
...(useLocalAjv && {
248+
"@programmer_network/use-ajv-form": path.resolve(
249+
__dirname,
250+
"../use-ajv-form/src"
251+
),
252+
}),
253+
},
254+
},
255+
// Other Vite configurations go here
256+
};
257+
});
258+
```
259+
260+
This configuration will:
261+
- Use the **published library** (from npm) by default.
262+
- Use the **local library's source code** (`../use-ajv-form/src`) only when `USE_LOCAL_AJV` is set to `true`.
263+
264+
---
265+
266+
#### Step 2: Add a Development Script
267+
268+
Add a new script to your `package.json` to launch the dev server while referencing the local version of the library:
269+
270+
```json
271+
"scripts": {
272+
"dev": "vite",
273+
"dev:with-local-ajv": "USE_LOCAL_AJV=true vite"
274+
}
275+
```
276+
277+
- Run `npm run dev` (or `pnpm dev`) for the default behavior (using the npm version).
278+
- Run `npm run dev:with-local-ajv` (or `pnpm dev:with-local-ajv`) to reference the **local source code** of the library.
279+
280+
---
281+
282+
#### Step 3: Development Workflow
283+
284+
- **Directory Setup**: Ensure your library (`../use-ajv-form/`) exists in the same flat-level directory as your main project.
285+
```
286+
projects/
287+
├── main-project/
288+
└── use-ajv-form/ <-- The library
289+
```
290+
291+
- **To Use the Local Library**: Start your dev server with:
292+
```bash
293+
npm run dev:with-local-ajv
294+
```
295+
296+
- **To Switch to Published Library**: Start your dev server normally:
297+
```bash
298+
npm run dev
299+
```
300+
301+
---
302+
303+
#### Bonus: Cross-Platform Compatibility (Windows-Friendly)
304+
To ensure the `USE_LOCAL_AJV=true` works across all platforms (including Windows, where `export` syntax doesn’t work), install `cross-env`:
305+
306+
```bash
307+
npm install -D cross-env
308+
```
309+
310+
Then update your `package.json` scripts:
311+
312+
```json
313+
"scripts": {
314+
"dev:with-local-ajv": "cross-env USE_LOCAL_AJV=true vite"
315+
}
316+
```

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
UseFormReturn,
1212
useFormErrors,
1313
} from './utils/types';
14-
import Logger from 'utils/Logger';
15-
14+
import Logger from './utils/Logger';
1615
const useAJVForm = <T extends Record<string, any>>(
1716
initial: T,
1817
schema: JSONSchemaType<T> | SchemaObject,
@@ -27,7 +26,6 @@ const useAJVForm = <T extends Record<string, any>>(
2726
},
2827
): UseFormReturn<T> => {
2928
const ajvInstance = options?.ajv || ajvInternal;
30-
3129
const initialStateRef = useRef<IState<T>>(getInitial(initial));
3230

3331
const [state, setState] = useState<IState<T>>(getInitial(initial));

0 commit comments

Comments
 (0)