htl-to-js 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/README.md +99 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -425,6 +425,69 @@ Example `i18n/es.json`:
425
425
 
426
426
  When no dictionary is passed (or when a key is missing), the original string is used as fallback.
427
427
 
428
+ ### Pluralization
429
+
430
+ Add `count=<expr>` alongside `@ i18n` to select the plural or singular form at runtime:
431
+
432
+ ```html
433
+ <!-- HTL -->
434
+ <span>${'item' @ i18n, count=n}</span>
435
+ ```
436
+
437
+ Generated:
438
+
439
+ ```js
440
+ <span>${_htlI18nPlural('item', n, _i18n)}</span>
441
+ ```
442
+
443
+ The dictionary entry for a pluralizable key is an array `[singular, plural]`:
444
+
445
+ ```json
446
+ {
447
+ "item": ["1 item", "{0} items"]
448
+ }
449
+ ```
450
+
451
+ `_htlI18nPlural` picks index `0` when `count === 1` and index `1` otherwise, then substitutes `{0}` with the count value.
452
+
453
+ ### Locale fallback chains
454
+
455
+ When targeting a specific locale (e.g. `es_MX`), you can supply a chain of dictionaries. Keys from earlier dictionaries override later ones, so the most specific locale should come first.
456
+
457
+ **Programmatic API:**
458
+
459
+ ```ts
460
+ const primary = parseI18nXml(fs.readFileSync('i18n/es_MX.xml', 'utf8'));
461
+ const fallback = parseI18nXml(fs.readFileSync('i18n/es.xml', 'utf8'));
462
+
463
+ const js = transpile(source, {
464
+ filename: 'card.html',
465
+ i18nDict: primary,
466
+ i18nFallbackDicts: [fallback],
467
+ });
468
+ ```
469
+
470
+ **Webpack loader (`i18nFallbackPaths` option):**
471
+
472
+ ```js
473
+ use: {
474
+ loader: require.resolve('htl-to-js/loader'),
475
+ options: {
476
+ i18nPath: path.resolve(__dirname, 'i18n/es_MX.xml'),
477
+ i18nFallbackPaths: [
478
+ path.resolve(__dirname, 'i18n/es.xml'),
479
+ path.resolve(__dirname, 'i18n/en.xml'),
480
+ ],
481
+ },
482
+ }
483
+ ```
484
+
485
+ **CLI — multiple `--i18n` flags (first = primary, rest = fallbacks):**
486
+
487
+ ```bash
488
+ npx htl-gen --i18n i18n/es_MX.xml --i18n i18n/es.xml --i18n i18n/en.xml "src/**/*.html"
489
+ ```
490
+
428
491
  ### Loading a dictionary from AEM XML
429
492
 
430
493
  AEM stores i18n dictionaries as JCR XML files (`en.xml`, `es.xml`, etc.) with the standard `sling:MessageEntry` format:
@@ -598,6 +661,38 @@ options: {
598
661
 
599
662
  At runtime, the `_resourceWrappers` parameter can override or extend the static config.
600
663
 
664
+ ### `format`
665
+
666
+ Controls the module format of the generated code. Defaults to `'cjs'`.
667
+
668
+ | Value | Output |
669
+ |---|---|
670
+ | `'cjs'` (default) | `module.exports = { createFoo }` |
671
+ | `'esm'` | `export { createFoo }` |
672
+
673
+ ```ts
674
+ const js = transpile(source, { filename: 'card.html', format: 'esm' });
675
+ ```
676
+
677
+ In the webpack loader pass it as a loader option:
678
+
679
+ ```js
680
+ use: {
681
+ loader: require.resolve('htl-to-js/loader'),
682
+ options: { format: 'esm' },
683
+ }
684
+ ```
685
+
686
+ In the CLI use `--esm`:
687
+
688
+ ```bash
689
+ npx htl-gen --esm "components/**/*.html"
690
+ ```
691
+
692
+ When `format: 'esm'` and `data-sly-use` references a local `.js` or `.json` file, the generated code emits `import` declarations instead of `require()` calls.
693
+
694
+ ---
695
+
601
696
  ### `modelTransforms`
602
697
 
603
698
  Object mapping `data-sly-use` class-name patterns to property injections. Enables build-time property merging based on the use class. All keys are merged as computed properties into the model variable.
@@ -797,12 +892,16 @@ Each option has a single responsibility:
797
892
 
798
893
  ```ts
799
894
  import { transpile } from 'htl-to-js';
895
+ import { parseI18nXml } from 'htl-to-js/parseI18nXml';
800
896
  import fs from 'fs';
801
897
 
802
898
  const source = fs.readFileSync('accordion.html', 'utf8');
803
899
  const jsModule = transpile(source, {
804
900
  filename: 'accordion.html',
901
+ format: 'cjs', // 'cjs' (default) | 'esm'
805
902
  omitAttrs: [],
903
+ i18nDict: {}, // bake a dictionary into the module
904
+ i18nFallbackDicts: [], // additional fallback dictionaries (lower priority)
806
905
  });
807
906
 
808
907
  console.log(jsModule);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "htl-to-js",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Webpack loader and CLI that transpiles AEM HTL (Sightly) templates into JavaScript template string functions.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",