wuchale 0.15.2 → 0.15.4

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.
@@ -141,10 +141,10 @@ export class MixedVisitor {
141
141
  else {
142
142
  return msgs;
143
143
  }
144
- if (childrenNestedRanges.length) {
144
+ if (props.scope === 'markup' && iArg > 0 || childrenNestedRanges.length > 0) {
145
145
  this.wrapNested(msgInfo, iArg > 0, childrenNestedRanges, lastChildEnd);
146
146
  }
147
- else if (hasTextChild) {
147
+ else {
148
148
  // no need for component use
149
149
  let begin = '{';
150
150
  let end = ')}';
@@ -30,7 +30,7 @@ const resolveLoaderPath = (name) => new URL(`../../src/adapter-vanilla/loaders/$
30
30
  export const adapter = (args = defaultArgs) => {
31
31
  const { heuristic, pluralsFunc, runtime, ...rest } = deepMergeObjects(args, defaultArgs);
32
32
  return {
33
- transform: ({ content, filename, index, header }) => new Transformer(content, filename, index, heuristic, pluralsFunc, header.expr, runtime).transform(header.head),
33
+ transform: ({ content, filename, index, expr }) => new Transformer(content, filename, index, heuristic, pluralsFunc, expr, runtime).transform(),
34
34
  loaderExts: ['.js', '.ts'],
35
35
  defaultLoaders: async () => {
36
36
  if (rest.bundleLoad) {
@@ -71,7 +71,7 @@ export declare class Transformer {
71
71
  visitProgram: (node: Estree.Program) => Message[];
72
72
  processCommentDirectives: (data: string) => CommentDirectives;
73
73
  visit: (node: Estree.AnyNode) => Message[];
74
- finalize: (msgs: Message[], hmrHeaderIndex: number) => TransformOutput;
75
- transform: (headerHead: string) => TransformOutput;
74
+ finalize: (msgs: Message[], hmrHeaderIndex: number, additionalHeader?: string) => TransformOutput;
75
+ transform: () => TransformOutput;
76
76
  }
77
77
  export {};
@@ -427,29 +427,20 @@ export class Transformer {
427
427
  this.commentDirectives = commentDirectives; // restore
428
428
  return msgs;
429
429
  };
430
- finalize = (msgs, hmrHeaderIndex) => ({
430
+ finalize = (msgs, hmrHeaderIndex, additionalHeader = '') => ({
431
431
  msgs,
432
- output: hmrData => {
433
- if (msgs.length === 0) {
434
- return {};
435
- }
436
- if (hmrData) {
437
- this.mstr.prependRight(hmrHeaderIndex, `\nconst ${varNames.hmrUpdate} = ${JSON.stringify(hmrData)}\n`);
438
- }
432
+ output: header => {
433
+ this.mstr.prependRight(hmrHeaderIndex, `\n${header}\n${additionalHeader}\n`);
439
434
  return {
440
435
  code: this.mstr.toString(),
441
436
  map: this.mstr.generateMap(),
442
437
  };
443
438
  }
444
439
  });
445
- transform = (headerHead) => {
440
+ transform = () => {
446
441
  const [ast, comments] = parseScript(this.content);
447
442
  this.comments = comments;
448
443
  this.mstr = new MagicString(this.content);
449
- const msgs = this.visit(ast);
450
- if (msgs.length) {
451
- this.mstr.appendRight(this.getRealBodyStart(ast.body), headerHead + '\n');
452
- }
453
- return this.finalize(msgs, 0);
444
+ return this.finalize(this.visit(ast), this.getRealBodyStart(ast.body));
454
445
  };
455
446
  }
@@ -44,19 +44,18 @@ export type CatalogExpr = {
44
44
  };
45
45
  export type TransformHeader = {
46
46
  head: string;
47
- expr: CatalogExpr;
48
47
  };
49
48
  type TransformCtx = {
50
49
  content: string;
51
50
  filename: string;
52
51
  index: IndexTracker;
53
- header: TransformHeader;
52
+ expr: CatalogExpr;
54
53
  };
55
54
  export type HMRData = {
56
55
  version: number;
57
56
  data: Record<string, [number, CompiledElement][]>;
58
57
  };
59
- export type TransformOutputFunc = (hmrData: HMRData | null) => {
58
+ export type TransformOutputFunc = (header: string) => {
60
59
  code?: string;
61
60
  map?: any;
62
61
  };
@@ -64,7 +63,7 @@ export type TransformOutput = {
64
63
  output: TransformOutputFunc;
65
64
  msgs: Message[];
66
65
  };
67
- export type TransformFunc = (ctx: TransformCtx) => TransformOutput;
66
+ export type TransformFunc = (expr: TransformCtx) => TransformOutput;
68
67
  export type WrapFunc = (expr: string) => string;
69
68
  export type UseReactiveFunc = (details: {
70
69
  funcName?: string;
package/dist/handler.d.ts CHANGED
@@ -38,6 +38,10 @@ type GranularState = {
38
38
  type LoaderPathEmpty = {
39
39
  [K in keyof LoaderPath]: boolean;
40
40
  };
41
+ type TransformOutputCode = {
42
+ code?: string;
43
+ map?: any;
44
+ };
41
45
  export declare class AdapterHandler {
42
46
  #private;
43
47
  key: string;
@@ -73,9 +77,6 @@ export declare class AdapterHandler {
73
77
  ignore: string[];
74
78
  }];
75
79
  savePoAndCompile: (loc: string) => Promise<void>;
76
- transform: (content: string, filename: string, hmrVersion?: number, ssr?: boolean) => Promise<{
77
- code?: string;
78
- map?: any;
79
- }>;
80
+ transform: (content: string, filename: string, hmrVersion?: number, ssr?: boolean) => Promise<TransformOutputCode>;
80
81
  }
81
82
  export {};
package/dist/handler.js CHANGED
@@ -15,6 +15,13 @@ const defaultPluralRule = {
15
15
  nplurals: 2,
16
16
  plural: 'n == 1 ? 0 : 1',
17
17
  };
18
+ const getFuncPlain = '_w_load_';
19
+ const getFuncReactive = getFuncPlain + 'rx_';
20
+ const catalogsVarName = '_w_catalogs_';
21
+ const bundledCatalogExpr = {
22
+ plain: `${getFuncPlain}(${catalogsVarName})`,
23
+ reactive: `${getFuncReactive}(${catalogsVarName})`,
24
+ };
18
25
  const objKeyLocale = (locale) => locale.includes('-') ? `'${locale}'` : locale;
19
26
  export async function loadPOFile(filename) {
20
27
  return new Promise((res, rej) => {
@@ -467,7 +474,7 @@ export class AdapterHandler {
467
474
  }
468
475
  `;
469
476
  };
470
- #prepareHeader = (filename, loadID, hasHmr, ssr) => {
477
+ #prepareHeader = (filename, loadID, hmrData, ssr) => {
471
478
  let loaderRelTo = filename;
472
479
  if (this.#adapter.writeFiles.transformed) {
473
480
  loaderRelTo = resolve(this.outDir + '/' + filename);
@@ -478,36 +485,29 @@ export class AdapterHandler {
478
485
  }
479
486
  const importsFuncs = [];
480
487
  const runtimeConf = this.#adapter.runtime;
481
- let getFuncPlain = '_w_load_';
482
- let getFuncReactive = getFuncPlain + 'rx_';
483
488
  let head = [];
484
- if (hasHmr) {
485
- const getFuncPlainHmr = getFuncPlain;
486
- const getFuncReactiveHmr = getFuncReactive;
487
- getFuncPlain += 'hmr_';
488
- getFuncReactive += 'hmr_';
489
+ let getFuncImportPlain = getFuncPlain;
490
+ let getFuncImportReactive = getFuncReactive;
491
+ if (hmrData != null) {
492
+ head.push(`const ${varNames.hmrUpdate} = ${JSON.stringify(hmrData)}`);
493
+ getFuncImportPlain += 'hmr_';
494
+ getFuncImportReactive += 'hmr_';
489
495
  if (runtimeConf.plain?.importName) {
490
- head.push(this.#hmrUpdateFunc(getFuncPlainHmr, getFuncPlain));
496
+ head.push(this.#hmrUpdateFunc(getFuncPlain, getFuncImportPlain));
491
497
  }
492
498
  if (runtimeConf.reactive?.importName) {
493
- head.push(this.#hmrUpdateFunc(getFuncReactiveHmr, getFuncReactive));
499
+ head.push(this.#hmrUpdateFunc(getFuncReactive, getFuncImportReactive));
494
500
  }
495
501
  }
496
- this.#putImportSpec(runtimeConf.plain?.importName, getFuncPlain, importsFuncs);
497
- this.#putImportSpec(runtimeConf.reactive?.importName, getFuncReactive, importsFuncs);
502
+ this.#putImportSpec(runtimeConf.plain?.importName, getFuncImportPlain, importsFuncs);
503
+ this.#putImportSpec(runtimeConf.reactive?.importName, getFuncImportReactive, importsFuncs);
498
504
  head = [
499
505
  `import ${varNames.rtWrap} from 'wuchale/runtime'`,
500
506
  `import ${importsFuncs.join(',')} from "${loaderPath}"`,
501
507
  ...head,
502
508
  ];
503
509
  if (!this.#adapter.bundleLoad) {
504
- return {
505
- head: head.join('\n'),
506
- expr: {
507
- plain: `${getFuncPlain}('${loadID}')`,
508
- reactive: `${getFuncReactive}('${loadID}')`,
509
- }
510
- };
510
+ return head.join('\n');
511
511
  }
512
512
  const imports = [];
513
513
  const objElms = [];
@@ -516,17 +516,19 @@ export class AdapterHandler {
516
516
  imports.push(`import * as ${locKW} from '${this.virtModEvent(loc, loadID)}'`);
517
517
  objElms.push(`${objKeyLocale(loc)}: ${locKW}`);
518
518
  }
519
- const catalogsVarName = '_w_catalogs_';
519
+ return [
520
+ ...imports,
521
+ ...head,
522
+ `const ${catalogsVarName} = {${objElms.join(',')}}`
523
+ ].join('\n');
524
+ };
525
+ #prepareRuntimeExpr = (loadID) => {
526
+ if (this.#adapter.bundleLoad) {
527
+ return bundledCatalogExpr;
528
+ }
520
529
  return {
521
- head: [
522
- ...imports,
523
- ...head,
524
- `const ${catalogsVarName} = {${objElms.join(',')}}`
525
- ].join('\n'),
526
- expr: {
527
- plain: `${getFuncPlain}(${catalogsVarName})`,
528
- reactive: `${getFuncReactive}(${catalogsVarName})`,
529
- }
530
+ plain: `${getFuncPlain}('${loadID}')`,
531
+ reactive: `${getFuncReactive}('${loadID}')`,
530
532
  };
531
533
  };
532
534
  transform = async (content, filename, hmrVersion = -1, ssr = false) => {
@@ -543,7 +545,7 @@ export class AdapterHandler {
543
545
  content,
544
546
  filename,
545
547
  index: indexTracker,
546
- header: this.#prepareHeader(filename, loadID, hmrVersion >= 0, ssr),
548
+ expr: this.#prepareRuntimeExpr(loadID),
547
549
  });
548
550
  const hmrKeys = {};
549
551
  for (const loc of this.#locales) {
@@ -632,21 +634,21 @@ export class AdapterHandler {
632
634
  this.#log.log(`Gemini translate ${color.cyan(untranslated.length)} items to ${color.cyan(getLanguageName(loc))} ${opType}`);
633
635
  await this.#geminiQueue[loc].running;
634
636
  }
635
- let hmrData = null;
636
- if (hmrVersion >= 0) {
637
- hmrData = { version: hmrVersion, data: {} };
638
- for (const loc of this.#locales) {
639
- hmrData.data[loc] = hmrKeys[loc]?.map(key => {
640
- const index = indexTracker.get(key);
641
- return [index, compiled[loc].items[index]];
642
- });
637
+ let output = {};
638
+ if (msgs.length) {
639
+ let hmrData = null;
640
+ if (hmrVersion >= 0) {
641
+ hmrData = { version: hmrVersion, data: {} };
642
+ for (const loc of this.#locales) {
643
+ hmrData.data[loc] = hmrKeys[loc]?.map(key => {
644
+ const index = indexTracker.get(key);
645
+ return [index, compiled[loc].items[index]];
646
+ });
647
+ }
643
648
  }
649
+ output = result.output(this.#prepareHeader(filename, loadID, hmrData, ssr));
644
650
  }
645
- const output = result.output(hmrData);
646
651
  await this.writeTransformed(filename, output.code ?? content);
647
- if (!msgs.length) {
648
- return {};
649
- }
650
652
  return output;
651
653
  };
652
654
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wuchale",
3
- "version": "0.15.2",
3
+ "version": "0.15.4",
4
4
  "description": "Protobuf-like i18n from plain code",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",