@hkdigital/lib-core 0.5.71 → 0.5.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -11
- package/package.json +1 -1
- package/scripts/validate-imports.mjs +16 -1
package/README.md
CHANGED
|
@@ -303,17 +303,27 @@ pnpm run lint:imports
|
|
|
303
303
|
node node_modules/@hkdigital/lib-core/scripts/validate-imports.mjs
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
+
**Alias support:**
|
|
307
|
+
|
|
308
|
+
The validator automatically reads path aliases from your
|
|
309
|
+
`svelte.config.js` and applies the same barrel export validation rules
|
|
310
|
+
to alias imports. This ensures consistent import patterns across:
|
|
311
|
+
- Internal `$lib/` imports
|
|
312
|
+
- Project aliases like `$hklib-core`, `$hklib-pro`, etc.
|
|
313
|
+
- External `@hkdigital/*` package imports
|
|
314
|
+
|
|
306
315
|
**Validation rules (enforced for `src/lib/` files only):**
|
|
307
316
|
|
|
308
|
-
1. **
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
317
|
+
1. **Alias optimization** - Use `$lib` instead of `$src/lib` (built-in
|
|
318
|
+
SvelteKit alias)
|
|
319
|
+
2. **Cross-domain imports** - Use `$lib/` instead of `../../../`
|
|
320
|
+
3. **Prefer barrel exports** - Use higher-level export files when
|
|
321
|
+
available (applies to `$lib/`, aliases, and `@hkdigital/*` packages)
|
|
322
|
+
4. **Parent index.js imports** - Use `$lib/` or import specific files
|
|
323
|
+
5. **Non-standard extensions** - Include full extension (e.g.,
|
|
312
324
|
`.svelte.js`)
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
7. **External package optimization** - Suggests barrel exports for
|
|
316
|
-
`@hkdigital/*` packages
|
|
325
|
+
6. **Directory imports** - Write explicitly or create barrel export file
|
|
326
|
+
7. **File existence** - All import paths must resolve to existing files
|
|
317
327
|
|
|
318
328
|
**Barrel export preference:**
|
|
319
329
|
|
|
@@ -322,6 +332,10 @@ exports your target. This encourages shorter imports that can be
|
|
|
322
332
|
combined:
|
|
323
333
|
|
|
324
334
|
```js
|
|
335
|
+
// Optimize built-in aliases:
|
|
336
|
+
import { MyComponent } from '$src/lib/ui/components.js';
|
|
337
|
+
// → Use: import { MyComponent } from '$lib/ui/components.js';
|
|
338
|
+
|
|
325
339
|
// Internal imports - instead of deep imports:
|
|
326
340
|
import ProfileBlocks from '$lib/ui/components/profile-blocks/ProfileBlocks.svelte';
|
|
327
341
|
import Button from '$lib/ui/primitives/buttons/Button.svelte';
|
|
@@ -330,6 +344,14 @@ import Button from '$lib/ui/primitives/buttons/Button.svelte';
|
|
|
330
344
|
import { ProfileBlocks } from '$lib/ui/components.js';
|
|
331
345
|
import { Button } from '$lib/ui/primitives.js';
|
|
332
346
|
|
|
347
|
+
// Project aliases - instead of deep imports:
|
|
348
|
+
import { Logger } from '$hklib-core/logging/logger/Logger.js';
|
|
349
|
+
import { HttpClient } from '$hklib-core/network/http/HttpClient.js';
|
|
350
|
+
|
|
351
|
+
// Use barrel exports:
|
|
352
|
+
import { Logger } from '$hklib-core/logging.js';
|
|
353
|
+
import { HttpClient } from '$hklib-core/network/http.js';
|
|
354
|
+
|
|
333
355
|
// External imports - instead of deep imports:
|
|
334
356
|
import { TextButton } from '@hkdigital/lib-core/ui/primitives/buttons/index.js';
|
|
335
357
|
import { TextInput } from '@hkdigital/lib-core/ui/primitives/inputs/index.js';
|
|
@@ -341,7 +363,7 @@ import { TextButton, TextInput } from '@hkdigital/lib-core/ui/primitives.js';
|
|
|
341
363
|
The validator checks from highest to lowest level (`$lib/ui.js` →
|
|
342
364
|
`$lib/ui/components.js` → `$lib/ui/components/profile-blocks.js`) and
|
|
343
365
|
suggests the highest-level file that exports your target. The same
|
|
344
|
-
logic applies to external `@hkdigital/*` packages.
|
|
366
|
+
logic applies to project aliases and external `@hkdigital/*` packages.
|
|
345
367
|
|
|
346
368
|
**Routes are exempt from strict rules:**
|
|
347
369
|
|
|
@@ -353,6 +375,17 @@ files.
|
|
|
353
375
|
**Example output:**
|
|
354
376
|
|
|
355
377
|
```
|
|
378
|
+
Validating import paths...
|
|
379
|
+
|
|
380
|
+
Found project aliases:
|
|
381
|
+
$src → src
|
|
382
|
+
$examples → src/routes/examples
|
|
383
|
+
$hklib-core → src/lib
|
|
384
|
+
|
|
385
|
+
src/lib/ui/panels/Panel.svelte:3
|
|
386
|
+
from '$src/lib/ui/components.js'
|
|
387
|
+
=> from '$lib/ui/components.js' (use built-in $lib alias)
|
|
388
|
+
|
|
356
389
|
src/lib/ui/panels/Panel.svelte:6
|
|
357
390
|
from '../../../components/profile-blocks/ProfileBlocks.svelte'
|
|
358
391
|
=> from '$lib/ui/components.js' (use barrel export)
|
|
@@ -362,6 +395,10 @@ src/lib/ui/pages/Profile.svelte:8
|
|
|
362
395
|
=> from '$lib/ui/components.js' (use barrel export for shorter imports)
|
|
363
396
|
|
|
364
397
|
src/lib/forms/LoginForm.svelte:4
|
|
398
|
+
from '$hklib-core/logging/logger/Logger.js'
|
|
399
|
+
=> from '$hklib-core/logging.js' (use barrel export)
|
|
400
|
+
|
|
401
|
+
src/lib/forms/LoginForm.svelte:6
|
|
365
402
|
from '@hkdigital/lib-core/ui/primitives/buttons/index.js'
|
|
366
403
|
=> from '@hkdigital/lib-core/ui/primitives.js' (use barrel export)
|
|
367
404
|
|
|
@@ -370,9 +407,10 @@ src/routes/explorer/[...path]/+page.svelte:4
|
|
|
370
407
|
✅ Allowed in routes
|
|
371
408
|
```
|
|
372
409
|
|
|
373
|
-
**What gets checked for
|
|
410
|
+
**What gets checked for barrel export suggestions:**
|
|
374
411
|
|
|
375
|
-
The validator only suggests barrel exports for
|
|
412
|
+
The validator only suggests barrel exports (for `$lib/`, project aliases,
|
|
413
|
+
and external `@hkdigital/*` packages) for:
|
|
376
414
|
- Explicit `index.js` imports
|
|
377
415
|
- Component files (`.svelte`)
|
|
378
416
|
- Class files (capitalized `.js` files)
|
|
@@ -380,6 +418,20 @@ The validator only suggests barrel exports for:
|
|
|
380
418
|
Intentional imports like `helpers.js`, `config.js`, or other lowercase
|
|
381
419
|
utility files are assumed to be the public API and won't be flagged.
|
|
382
420
|
|
|
421
|
+
**Alias configuration:**
|
|
422
|
+
|
|
423
|
+
The validator automatically detects aliases in your `svelte.config.js`.
|
|
424
|
+
For example, if your config has:
|
|
425
|
+
```js
|
|
426
|
+
alias: {
|
|
427
|
+
$src: 'src',
|
|
428
|
+
$hklib-core: 'node_modules/@hkdigital/lib-core/dist'
|
|
429
|
+
}
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
The validator will apply the same barrel export rules to these aliases
|
|
433
|
+
as it does to `$lib/` and `@hkdigital/*` imports.
|
|
434
|
+
|
|
383
435
|
### Import Patterns and Export Structure
|
|
384
436
|
|
|
385
437
|
**Public exports use domain-specific files matching folder names:**
|
package/package.json
CHANGED
|
@@ -498,7 +498,22 @@ async function validateFile(filePath) {
|
|
|
498
498
|
const importPathRaw = importMatch[1];
|
|
499
499
|
|
|
500
500
|
// Strip query parameters (Vite asset imports like ?preset=render)
|
|
501
|
-
|
|
501
|
+
let importPath = importPathRaw.split('?')[0];
|
|
502
|
+
|
|
503
|
+
// Check if using $src/lib when $lib is available (built-in SvelteKit)
|
|
504
|
+
// Report the issue and normalize the path for further validation
|
|
505
|
+
let hasSrcLibIssue = false;
|
|
506
|
+
if (importPath.startsWith('$src/lib/') || importPath === '$src/lib') {
|
|
507
|
+
hasSrcLibIssue = true;
|
|
508
|
+
const optimizedPath = importPath.replace('$src/lib', '$lib');
|
|
509
|
+
errors.push(
|
|
510
|
+
`${relativePath}:${lineNum}\n` +
|
|
511
|
+
` from '${importPath}'\n` +
|
|
512
|
+
` => from '${optimizedPath}' (use built-in $lib alias)`
|
|
513
|
+
);
|
|
514
|
+
// Normalize for further validation
|
|
515
|
+
importPath = optimizedPath;
|
|
516
|
+
}
|
|
502
517
|
|
|
503
518
|
// Check if import uses a project alias
|
|
504
519
|
const isAliasImport = Object.keys(PROJECT_ALIASES).some(
|