@intlayer/vue-compiler 9.3.2 → 9.3.3

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.
@@ -8,13 +8,18 @@ _vue_compiler_sfc = require_runtime.__toESM(_vue_compiler_sfc);
8
8
  * Extracts intlayer dictionary field usage from a Vue SFC for a set of
9
9
  * plain variable bindings (i.e. `const content = useIntlayer('key')`).
10
10
  *
11
- * Two access patterns are recognised:
11
+ * Three access patterns are recognised:
12
12
  *
13
- * 1. `varName.value.fieldName` in script blocks
14
- * Vue's `useIntlayer` returns a reactive `Ref<Content>`, so fields are
15
- * accessed one level deeper via `.value`.
13
+ * 1. `varName.fieldName` in script blocks
14
+ * `useIntlayer` returns a deep reactive proxy, so content fields are read
15
+ * directly on the variable (`content.title`, `content.count(2)`), the
16
+ * `.value` unwrapping happening on the leaf node instead.
16
17
  *
17
- * 2. `varName.fieldName` in the template block
18
+ * 2. `varName.value.fieldName` in script blocks
19
+ * Legacy `Ref<Content>` accessor indirection, where fields live one level
20
+ * deeper than the variable.
21
+ *
22
+ * 3. `varName.fieldName` in the template block
18
23
  * Inside `<template>`, Vue automatically unwraps top-level refs, so
19
24
  * fields are accessed directly without `.value`.
20
25
  *
@@ -25,6 +30,21 @@ _vue_compiler_sfc = require_runtime.__toESM(_vue_compiler_sfc);
25
30
  /** Escapes special regex characters in a string used as a regex literal. */
26
31
  const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
27
32
  /**
33
+ * Runs `fieldRegex` over `source` and adds every captured field name to
34
+ * `fields`, skipping the `value` accessor of an intlayer node (it addresses the
35
+ * node wrapper, not a content field).
36
+ *
37
+ * Over-approximating is safe here: an extra name only keeps a field that could
38
+ * have been pruned, while a missing name removes content the app reads.
39
+ */
40
+ const collectFieldNames = (source, fieldRegex, fields) => {
41
+ fieldRegex.lastIndex = 0;
42
+ for (let match = fieldRegex.exec(source); match !== null; match = fieldRegex.exec(source)) {
43
+ const field = match[1];
44
+ if (field && field !== "value") fields.add(field);
45
+ }
46
+ };
47
+ /**
28
48
  * Splits a Vue SFC source string into its script and template regions.
29
49
  *
30
50
  * Uses `@vue/compiler-sfc` to get exact character offsets so that nested
@@ -68,17 +88,9 @@ const extractVueIntlayerFieldUsage = (code, plainVariables) => {
68
88
  for (const { variableName, dictionaryKey } of plainVariables) {
69
89
  const fields = /* @__PURE__ */ new Set();
70
90
  const esc = escapeRegExp(variableName);
71
- const scriptRe = new RegExp(`\\b${esc}\\.value\\.(\\w+)`, "g");
72
- scriptRe.lastIndex = 0;
73
- for (let m = scriptRe.exec(scriptSource); m !== null; m = scriptRe.exec(scriptSource)) {
74
- const field = m[1];
75
- if (field !== "value") fields.add(field);
76
- }
77
- if (templateSource) {
78
- const templateRe = new RegExp(`\\b${esc}\\.(\\w+)`, "g");
79
- templateRe.lastIndex = 0;
80
- for (let m = templateRe.exec(templateSource); m !== null; m = templateRe.exec(templateSource)) fields.add(m[1]);
81
- }
91
+ collectFieldNames(scriptSource, new RegExp(`\\b${esc}\\.(\\w+)`, "g"), fields);
92
+ collectFieldNames(scriptSource, new RegExp(`\\b${esc}\\.value\\.(\\w+)`, "g"), fields);
93
+ if (templateSource) collectFieldNames(templateSource, new RegExp(`\\b${esc}\\.(\\w+)`, "g"), fields);
82
94
  if (fields.size > 0) result.set(dictionaryKey, fields);
83
95
  }
84
96
  return result;
@@ -1 +1 @@
1
- {"version":3,"file":"extractVueFieldUsage.cjs","names":["vueSfc"],"sources":["../../src/extractVueFieldUsage.ts"],"sourcesContent":["/**\n * Extracts intlayer dictionary field usage from a Vue SFC for a set of\n * plain variable bindings (i.e. `const content = useIntlayer('key')`).\n *\n * Two access patterns are recognised:\n *\n * 1. `varName.value.fieldName` in script blocks\n * Vue's `useIntlayer` returns a reactive `Ref<Content>`, so fields are\n * accessed one level deeper via `.value`.\n *\n * 2. `varName.fieldName` in the template block\n * Inside `<template>`, Vue automatically unwraps top-level refs, so\n * fields are accessed directly without `.value`.\n *\n * The template block is extracted via `@vue/compiler-sfc` so that nested\n * `<template v-for>` / `<template v-if>` tags do not confuse the parser.\n * Falls back to a greedy regex when the package is unavailable.\n */\n\nimport vueSfc from '@vue/compiler-sfc';\n\n/** Escapes special regex characters in a string used as a regex literal. */\nconst escapeRegExp = (str: string): string =>\n str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n/** Input descriptor for a single plain variable binding. */\nexport type PlainVariableInfo = {\n /** The local variable name in the script (`content` in `const content = useIntlayer(…)`). */\n variableName: string;\n /** The intlayer dictionary key passed to `useIntlayer`. */\n dictionaryKey: string;\n};\n\n/**\n * Splits a Vue SFC source string into its script and template regions.\n *\n * Uses `@vue/compiler-sfc` to get exact character offsets so that nested\n * `<template>` tags (used by Vue directives like `v-for` / `v-if`) never\n * cause the template region to be mis-identified.\n */\nconst splitVueSfc = (\n code: string\n): { scriptSource: string; templateSource: string } => {\n try {\n const { descriptor } = (\n vueSfc.parse as (src: string) => { descriptor: any }\n )(code);\n\n const templateSource: string = descriptor.template?.content ?? '';\n\n const scriptParts: string[] = [];\n if (descriptor.script?.content) scriptParts.push(descriptor.script.content);\n if (descriptor.scriptSetup?.content)\n scriptParts.push(descriptor.scriptSetup.content);\n const scriptSource = scriptParts.join('\\n');\n\n return { scriptSource, templateSource };\n } catch {\n // @vue/compiler-sfc not available or parse failed.\n // Fall back: use a greedy regex to capture everything between the\n // outermost <template> tags (robust against nested tags unlike the\n // non-greedy variant).\n const templateMatch = /<template(?:[^>]*)>([\\s\\S]*)<\\/template>/i.exec(\n code\n );\n const templateSource = templateMatch ? templateMatch[1] : '';\n const scriptSource = code.replace(/<template[\\s\\S]*<\\/template>/gi, '');\n return { scriptSource, templateSource };\n }\n};\n\n/**\n * Analyzes a Vue SFC source string and returns the top-level content field\n * names that are statically accessed for each plain intlayer variable binding.\n *\n * @param code - Full `.vue` file source.\n * @param plainVariables - List of plain variable bindings to analyse.\n * @returns Map from dictionary key to the set of accessed top-level field names.\n * If no fields can be determined for a given key it is omitted from the\n * map so the caller can fall back to `'all'`.\n */\nexport const extractVueIntlayerFieldUsage = (\n code: string,\n plainVariables: PlainVariableInfo[]\n): Map<string, Set<string>> => {\n const result = new Map<string, Set<string>>();\n\n if (plainVariables.length === 0) return result;\n\n const { scriptSource, templateSource } = splitVueSfc(code);\n\n for (const { variableName, dictionaryKey } of plainVariables) {\n const fields = new Set<string>();\n const esc = escapeRegExp(variableName);\n\n // ── 1. Script pattern: varName.value.fieldName ─────────────────────────\n // Vue's reactive ref accessor; the actual content fields live one level\n // deeper than the variable itself.\n const scriptRe = new RegExp(`\\\\b${esc}\\\\.value\\\\.(\\\\w+)`, 'g');\n scriptRe.lastIndex = 0;\n for (\n let m = scriptRe.exec(scriptSource);\n m !== null;\n m = scriptRe.exec(scriptSource)\n ) {\n const field = m[1];\n // Skip chained `.value.value` artefacts\n if (field !== 'value') fields.add(field);\n }\n\n // ── 2. Template pattern: varName.fieldName ─────────────────────────────\n // Inside `<template>` Vue auto-unwraps refs, so content is accessed\n // directly without `.value`.\n if (templateSource) {\n const templateRe = new RegExp(`\\\\b${esc}\\\\.(\\\\w+)`, 'g');\n\n templateRe.lastIndex = 0;\n for (\n let m = templateRe.exec(templateSource);\n m !== null;\n m = templateRe.exec(templateSource)\n ) {\n fields.add(m[1]);\n }\n }\n\n if (fields.size > 0) {\n result.set(dictionaryKey, fields);\n }\n }\n\n return result;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAM,gBAAgB,QACpB,IAAI,QAAQ,uBAAuB,MAAM;;;;;;;;AAiB3C,MAAM,eACJ,SACqD;CACrD,IAAI;EACF,MAAM,EAAE,eACNA,0BAAO,MACP,IAAI;EAEN,MAAM,iBAAyB,WAAW,UAAU,WAAW;EAE/D,MAAM,cAAwB,CAAC;EAC/B,IAAI,WAAW,QAAQ,SAAS,YAAY,KAAK,WAAW,OAAO,OAAO;EAC1E,IAAI,WAAW,aAAa,SAC1B,YAAY,KAAK,WAAW,YAAY,OAAO;EAGjD,OAAO;GAAE,cAFY,YAAY,KAAK,IAElB;GAAG;EAAe;CACxC,QAAQ;EAKN,MAAM,gBAAgB,4CAA4C,KAChE,IACF;EACA,MAAM,iBAAiB,gBAAgB,cAAc,KAAK;EAE1D,OAAO;GAAE,cADY,KAAK,QAAQ,kCAAkC,EAChD;GAAG;EAAe;CACxC;AACF;;;;;;;;;;;AAYA,MAAa,gCACX,MACA,mBAC6B;CAC7B,MAAM,yBAAS,IAAI,IAAyB;CAE5C,IAAI,eAAe,WAAW,GAAG,OAAO;CAExC,MAAM,EAAE,cAAc,mBAAmB,YAAY,IAAI;CAEzD,KAAK,MAAM,EAAE,cAAc,mBAAmB,gBAAgB;EAC5D,MAAM,yBAAS,IAAI,IAAY;EAC/B,MAAM,MAAM,aAAa,YAAY;EAKrC,MAAM,WAAW,IAAI,OAAO,MAAM,IAAI,oBAAoB,GAAG;EAC7D,SAAS,YAAY;EACrB,KACE,IAAI,IAAI,SAAS,KAAK,YAAY,GAClC,MAAM,MACN,IAAI,SAAS,KAAK,YAAY,GAC9B;GACA,MAAM,QAAQ,EAAE;GAEhB,IAAI,UAAU,SAAS,OAAO,IAAI,KAAK;EACzC;EAKA,IAAI,gBAAgB;GAClB,MAAM,aAAa,IAAI,OAAO,MAAM,IAAI,YAAY,GAAG;GAEvD,WAAW,YAAY;GACvB,KACE,IAAI,IAAI,WAAW,KAAK,cAAc,GACtC,MAAM,MACN,IAAI,WAAW,KAAK,cAAc,GAElC,OAAO,IAAI,EAAE,EAAE;EAEnB;EAEA,IAAI,OAAO,OAAO,GAChB,OAAO,IAAI,eAAe,MAAM;CAEpC;CAEA,OAAO;AACT"}
1
+ {"version":3,"file":"extractVueFieldUsage.cjs","names":["vueSfc"],"sources":["../../src/extractVueFieldUsage.ts"],"sourcesContent":["/**\n * Extracts intlayer dictionary field usage from a Vue SFC for a set of\n * plain variable bindings (i.e. `const content = useIntlayer('key')`).\n *\n * Three access patterns are recognised:\n *\n * 1. `varName.fieldName` in script blocks\n * `useIntlayer` returns a deep reactive proxy, so content fields are read\n * directly on the variable (`content.title`, `content.count(2)`), the\n * `.value` unwrapping happening on the leaf node instead.\n *\n * 2. `varName.value.fieldName` in script blocks\n * Legacy `Ref<Content>` accessor indirection, where fields live one level\n * deeper than the variable.\n *\n * 3. `varName.fieldName` in the template block\n * Inside `<template>`, Vue automatically unwraps top-level refs, so\n * fields are accessed directly without `.value`.\n *\n * The template block is extracted via `@vue/compiler-sfc` so that nested\n * `<template v-for>` / `<template v-if>` tags do not confuse the parser.\n * Falls back to a greedy regex when the package is unavailable.\n */\n\nimport vueSfc from '@vue/compiler-sfc';\n\n/** Escapes special regex characters in a string used as a regex literal. */\nconst escapeRegExp = (str: string): string =>\n str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n/**\n * Runs `fieldRegex` over `source` and adds every captured field name to\n * `fields`, skipping the `value` accessor of an intlayer node (it addresses the\n * node wrapper, not a content field).\n *\n * Over-approximating is safe here: an extra name only keeps a field that could\n * have been pruned, while a missing name removes content the app reads.\n */\nconst collectFieldNames = (\n source: string,\n fieldRegex: RegExp,\n fields: Set<string>\n): void => {\n fieldRegex.lastIndex = 0;\n\n for (\n let match = fieldRegex.exec(source);\n match !== null;\n match = fieldRegex.exec(source)\n ) {\n const field = match[1];\n if (field && field !== 'value') fields.add(field);\n }\n};\n\n/** Input descriptor for a single plain variable binding. */\nexport type PlainVariableInfo = {\n /** The local variable name in the script (`content` in `const content = useIntlayer(…)`). */\n variableName: string;\n /** The intlayer dictionary key passed to `useIntlayer`. */\n dictionaryKey: string;\n};\n\n/**\n * Splits a Vue SFC source string into its script and template regions.\n *\n * Uses `@vue/compiler-sfc` to get exact character offsets so that nested\n * `<template>` tags (used by Vue directives like `v-for` / `v-if`) never\n * cause the template region to be mis-identified.\n */\nconst splitVueSfc = (\n code: string\n): { scriptSource: string; templateSource: string } => {\n try {\n const { descriptor } = (\n vueSfc.parse as (src: string) => { descriptor: any }\n )(code);\n\n const templateSource: string = descriptor.template?.content ?? '';\n\n const scriptParts: string[] = [];\n if (descriptor.script?.content) scriptParts.push(descriptor.script.content);\n if (descriptor.scriptSetup?.content)\n scriptParts.push(descriptor.scriptSetup.content);\n const scriptSource = scriptParts.join('\\n');\n\n return { scriptSource, templateSource };\n } catch {\n // @vue/compiler-sfc not available or parse failed.\n // Fall back: use a greedy regex to capture everything between the\n // outermost <template> tags (robust against nested tags unlike the\n // non-greedy variant).\n const templateMatch = /<template(?:[^>]*)>([\\s\\S]*)<\\/template>/i.exec(\n code\n );\n const templateSource = templateMatch ? templateMatch[1] : '';\n const scriptSource = code.replace(/<template[\\s\\S]*<\\/template>/gi, '');\n return { scriptSource, templateSource };\n }\n};\n\n/**\n * Analyzes a Vue SFC source string and returns the top-level content field\n * names that are statically accessed for each plain intlayer variable binding.\n *\n * @param code - Full `.vue` file source.\n * @param plainVariables - List of plain variable bindings to analyse.\n * @returns Map from dictionary key to the set of accessed top-level field names.\n * If no fields can be determined for a given key it is omitted from the\n * map so the caller can fall back to `'all'`.\n */\nexport const extractVueIntlayerFieldUsage = (\n code: string,\n plainVariables: PlainVariableInfo[]\n): Map<string, Set<string>> => {\n const result = new Map<string, Set<string>>();\n\n if (plainVariables.length === 0) return result;\n\n const { scriptSource, templateSource } = splitVueSfc(code);\n\n for (const { variableName, dictionaryKey } of plainVariables) {\n const fields = new Set<string>();\n const esc = escapeRegExp(variableName);\n\n // ── 1. Script pattern: varName.fieldName ───────────────────────────────\n // The proxy returned by `useIntlayer` exposes content fields directly, so\n // scripts read them without any indirection. `.value` is the leaf-node\n // accessor, never a content field — the deeper name is picked up by the\n // `.value.` pattern below.\n collectFieldNames(\n scriptSource,\n new RegExp(`\\\\b${esc}\\\\.(\\\\w+)`, 'g'),\n fields\n );\n\n // ── 2. Script pattern: varName.value.fieldName ─────────────────────────\n // Legacy reactive ref accessor; the actual content fields live one level\n // deeper than the variable itself.\n collectFieldNames(\n scriptSource,\n new RegExp(`\\\\b${esc}\\\\.value\\\\.(\\\\w+)`, 'g'),\n fields\n );\n\n // ── 3. Template pattern: varName.fieldName ─────────────────────────────\n // Inside `<template>` Vue auto-unwraps refs, so content is accessed\n // directly without `.value`.\n if (templateSource) {\n collectFieldNames(\n templateSource,\n new RegExp(`\\\\b${esc}\\\\.(\\\\w+)`, 'g'),\n fields\n );\n }\n\n if (fields.size > 0) {\n result.set(dictionaryKey, fields);\n }\n }\n\n return result;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,MAAM,gBAAgB,QACpB,IAAI,QAAQ,uBAAuB,MAAM;;;;;;;;;AAU3C,MAAM,qBACJ,QACA,YACA,WACS;CACT,WAAW,YAAY;CAEvB,KACE,IAAI,QAAQ,WAAW,KAAK,MAAM,GAClC,UAAU,MACV,QAAQ,WAAW,KAAK,MAAM,GAC9B;EACA,MAAM,QAAQ,MAAM;EACpB,IAAI,SAAS,UAAU,SAAS,OAAO,IAAI,KAAK;CAClD;AACF;;;;;;;;AAiBA,MAAM,eACJ,SACqD;CACrD,IAAI;EACF,MAAM,EAAE,eACNA,0BAAO,MACP,IAAI;EAEN,MAAM,iBAAyB,WAAW,UAAU,WAAW;EAE/D,MAAM,cAAwB,CAAC;EAC/B,IAAI,WAAW,QAAQ,SAAS,YAAY,KAAK,WAAW,OAAO,OAAO;EAC1E,IAAI,WAAW,aAAa,SAC1B,YAAY,KAAK,WAAW,YAAY,OAAO;EAGjD,OAAO;GAAE,cAFY,YAAY,KAAK,IAElB;GAAG;EAAe;CACxC,QAAQ;EAKN,MAAM,gBAAgB,4CAA4C,KAChE,IACF;EACA,MAAM,iBAAiB,gBAAgB,cAAc,KAAK;EAE1D,OAAO;GAAE,cADY,KAAK,QAAQ,kCAAkC,EAChD;GAAG;EAAe;CACxC;AACF;;;;;;;;;;;AAYA,MAAa,gCACX,MACA,mBAC6B;CAC7B,MAAM,yBAAS,IAAI,IAAyB;CAE5C,IAAI,eAAe,WAAW,GAAG,OAAO;CAExC,MAAM,EAAE,cAAc,mBAAmB,YAAY,IAAI;CAEzD,KAAK,MAAM,EAAE,cAAc,mBAAmB,gBAAgB;EAC5D,MAAM,yBAAS,IAAI,IAAY;EAC/B,MAAM,MAAM,aAAa,YAAY;EAOrC,kBACE,cACA,IAAI,OAAO,MAAM,IAAI,YAAY,GAAG,GACpC,MACF;EAKA,kBACE,cACA,IAAI,OAAO,MAAM,IAAI,oBAAoB,GAAG,GAC5C,MACF;EAKA,IAAI,gBACF,kBACE,gBACA,IAAI,OAAO,MAAM,IAAI,YAAY,GAAG,GACpC,MACF;EAGF,IAAI,OAAO,OAAO,GAChB,OAAO,IAAI,eAAe,MAAM;CAEpC;CAEA,OAAO;AACT"}
@@ -5,13 +5,18 @@ import vueSfc from "@vue/compiler-sfc";
5
5
  * Extracts intlayer dictionary field usage from a Vue SFC for a set of
6
6
  * plain variable bindings (i.e. `const content = useIntlayer('key')`).
7
7
  *
8
- * Two access patterns are recognised:
8
+ * Three access patterns are recognised:
9
9
  *
10
- * 1. `varName.value.fieldName` in script blocks
11
- * Vue's `useIntlayer` returns a reactive `Ref<Content>`, so fields are
12
- * accessed one level deeper via `.value`.
10
+ * 1. `varName.fieldName` in script blocks
11
+ * `useIntlayer` returns a deep reactive proxy, so content fields are read
12
+ * directly on the variable (`content.title`, `content.count(2)`), the
13
+ * `.value` unwrapping happening on the leaf node instead.
13
14
  *
14
- * 2. `varName.fieldName` in the template block
15
+ * 2. `varName.value.fieldName` in script blocks
16
+ * Legacy `Ref<Content>` accessor indirection, where fields live one level
17
+ * deeper than the variable.
18
+ *
19
+ * 3. `varName.fieldName` in the template block
15
20
  * Inside `<template>`, Vue automatically unwraps top-level refs, so
16
21
  * fields are accessed directly without `.value`.
17
22
  *
@@ -22,6 +27,21 @@ import vueSfc from "@vue/compiler-sfc";
22
27
  /** Escapes special regex characters in a string used as a regex literal. */
23
28
  const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
24
29
  /**
30
+ * Runs `fieldRegex` over `source` and adds every captured field name to
31
+ * `fields`, skipping the `value` accessor of an intlayer node (it addresses the
32
+ * node wrapper, not a content field).
33
+ *
34
+ * Over-approximating is safe here: an extra name only keeps a field that could
35
+ * have been pruned, while a missing name removes content the app reads.
36
+ */
37
+ const collectFieldNames = (source, fieldRegex, fields) => {
38
+ fieldRegex.lastIndex = 0;
39
+ for (let match = fieldRegex.exec(source); match !== null; match = fieldRegex.exec(source)) {
40
+ const field = match[1];
41
+ if (field && field !== "value") fields.add(field);
42
+ }
43
+ };
44
+ /**
25
45
  * Splits a Vue SFC source string into its script and template regions.
26
46
  *
27
47
  * Uses `@vue/compiler-sfc` to get exact character offsets so that nested
@@ -65,17 +85,9 @@ const extractVueIntlayerFieldUsage = (code, plainVariables) => {
65
85
  for (const { variableName, dictionaryKey } of plainVariables) {
66
86
  const fields = /* @__PURE__ */ new Set();
67
87
  const esc = escapeRegExp(variableName);
68
- const scriptRe = new RegExp(`\\b${esc}\\.value\\.(\\w+)`, "g");
69
- scriptRe.lastIndex = 0;
70
- for (let m = scriptRe.exec(scriptSource); m !== null; m = scriptRe.exec(scriptSource)) {
71
- const field = m[1];
72
- if (field !== "value") fields.add(field);
73
- }
74
- if (templateSource) {
75
- const templateRe = new RegExp(`\\b${esc}\\.(\\w+)`, "g");
76
- templateRe.lastIndex = 0;
77
- for (let m = templateRe.exec(templateSource); m !== null; m = templateRe.exec(templateSource)) fields.add(m[1]);
78
- }
88
+ collectFieldNames(scriptSource, new RegExp(`\\b${esc}\\.(\\w+)`, "g"), fields);
89
+ collectFieldNames(scriptSource, new RegExp(`\\b${esc}\\.value\\.(\\w+)`, "g"), fields);
90
+ if (templateSource) collectFieldNames(templateSource, new RegExp(`\\b${esc}\\.(\\w+)`, "g"), fields);
79
91
  if (fields.size > 0) result.set(dictionaryKey, fields);
80
92
  }
81
93
  return result;
@@ -1 +1 @@
1
- {"version":3,"file":"extractVueFieldUsage.mjs","names":[],"sources":["../../src/extractVueFieldUsage.ts"],"sourcesContent":["/**\n * Extracts intlayer dictionary field usage from a Vue SFC for a set of\n * plain variable bindings (i.e. `const content = useIntlayer('key')`).\n *\n * Two access patterns are recognised:\n *\n * 1. `varName.value.fieldName` in script blocks\n * Vue's `useIntlayer` returns a reactive `Ref<Content>`, so fields are\n * accessed one level deeper via `.value`.\n *\n * 2. `varName.fieldName` in the template block\n * Inside `<template>`, Vue automatically unwraps top-level refs, so\n * fields are accessed directly without `.value`.\n *\n * The template block is extracted via `@vue/compiler-sfc` so that nested\n * `<template v-for>` / `<template v-if>` tags do not confuse the parser.\n * Falls back to a greedy regex when the package is unavailable.\n */\n\nimport vueSfc from '@vue/compiler-sfc';\n\n/** Escapes special regex characters in a string used as a regex literal. */\nconst escapeRegExp = (str: string): string =>\n str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n/** Input descriptor for a single plain variable binding. */\nexport type PlainVariableInfo = {\n /** The local variable name in the script (`content` in `const content = useIntlayer(…)`). */\n variableName: string;\n /** The intlayer dictionary key passed to `useIntlayer`. */\n dictionaryKey: string;\n};\n\n/**\n * Splits a Vue SFC source string into its script and template regions.\n *\n * Uses `@vue/compiler-sfc` to get exact character offsets so that nested\n * `<template>` tags (used by Vue directives like `v-for` / `v-if`) never\n * cause the template region to be mis-identified.\n */\nconst splitVueSfc = (\n code: string\n): { scriptSource: string; templateSource: string } => {\n try {\n const { descriptor } = (\n vueSfc.parse as (src: string) => { descriptor: any }\n )(code);\n\n const templateSource: string = descriptor.template?.content ?? '';\n\n const scriptParts: string[] = [];\n if (descriptor.script?.content) scriptParts.push(descriptor.script.content);\n if (descriptor.scriptSetup?.content)\n scriptParts.push(descriptor.scriptSetup.content);\n const scriptSource = scriptParts.join('\\n');\n\n return { scriptSource, templateSource };\n } catch {\n // @vue/compiler-sfc not available or parse failed.\n // Fall back: use a greedy regex to capture everything between the\n // outermost <template> tags (robust against nested tags unlike the\n // non-greedy variant).\n const templateMatch = /<template(?:[^>]*)>([\\s\\S]*)<\\/template>/i.exec(\n code\n );\n const templateSource = templateMatch ? templateMatch[1] : '';\n const scriptSource = code.replace(/<template[\\s\\S]*<\\/template>/gi, '');\n return { scriptSource, templateSource };\n }\n};\n\n/**\n * Analyzes a Vue SFC source string and returns the top-level content field\n * names that are statically accessed for each plain intlayer variable binding.\n *\n * @param code - Full `.vue` file source.\n * @param plainVariables - List of plain variable bindings to analyse.\n * @returns Map from dictionary key to the set of accessed top-level field names.\n * If no fields can be determined for a given key it is omitted from the\n * map so the caller can fall back to `'all'`.\n */\nexport const extractVueIntlayerFieldUsage = (\n code: string,\n plainVariables: PlainVariableInfo[]\n): Map<string, Set<string>> => {\n const result = new Map<string, Set<string>>();\n\n if (plainVariables.length === 0) return result;\n\n const { scriptSource, templateSource } = splitVueSfc(code);\n\n for (const { variableName, dictionaryKey } of plainVariables) {\n const fields = new Set<string>();\n const esc = escapeRegExp(variableName);\n\n // ── 1. Script pattern: varName.value.fieldName ─────────────────────────\n // Vue's reactive ref accessor; the actual content fields live one level\n // deeper than the variable itself.\n const scriptRe = new RegExp(`\\\\b${esc}\\\\.value\\\\.(\\\\w+)`, 'g');\n scriptRe.lastIndex = 0;\n for (\n let m = scriptRe.exec(scriptSource);\n m !== null;\n m = scriptRe.exec(scriptSource)\n ) {\n const field = m[1];\n // Skip chained `.value.value` artefacts\n if (field !== 'value') fields.add(field);\n }\n\n // ── 2. Template pattern: varName.fieldName ─────────────────────────────\n // Inside `<template>` Vue auto-unwraps refs, so content is accessed\n // directly without `.value`.\n if (templateSource) {\n const templateRe = new RegExp(`\\\\b${esc}\\\\.(\\\\w+)`, 'g');\n\n templateRe.lastIndex = 0;\n for (\n let m = templateRe.exec(templateSource);\n m !== null;\n m = templateRe.exec(templateSource)\n ) {\n fields.add(m[1]);\n }\n }\n\n if (fields.size > 0) {\n result.set(dictionaryKey, fields);\n }\n }\n\n return result;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAM,gBAAgB,QACpB,IAAI,QAAQ,uBAAuB,MAAM;;;;;;;;AAiB3C,MAAM,eACJ,SACqD;CACrD,IAAI;EACF,MAAM,EAAE,eACN,OAAO,MACP,IAAI;EAEN,MAAM,iBAAyB,WAAW,UAAU,WAAW;EAE/D,MAAM,cAAwB,CAAC;EAC/B,IAAI,WAAW,QAAQ,SAAS,YAAY,KAAK,WAAW,OAAO,OAAO;EAC1E,IAAI,WAAW,aAAa,SAC1B,YAAY,KAAK,WAAW,YAAY,OAAO;EAGjD,OAAO;GAAE,cAFY,YAAY,KAAK,IAElB;GAAG;EAAe;CACxC,QAAQ;EAKN,MAAM,gBAAgB,4CAA4C,KAChE,IACF;EACA,MAAM,iBAAiB,gBAAgB,cAAc,KAAK;EAE1D,OAAO;GAAE,cADY,KAAK,QAAQ,kCAAkC,EAChD;GAAG;EAAe;CACxC;AACF;;;;;;;;;;;AAYA,MAAa,gCACX,MACA,mBAC6B;CAC7B,MAAM,yBAAS,IAAI,IAAyB;CAE5C,IAAI,eAAe,WAAW,GAAG,OAAO;CAExC,MAAM,EAAE,cAAc,mBAAmB,YAAY,IAAI;CAEzD,KAAK,MAAM,EAAE,cAAc,mBAAmB,gBAAgB;EAC5D,MAAM,yBAAS,IAAI,IAAY;EAC/B,MAAM,MAAM,aAAa,YAAY;EAKrC,MAAM,WAAW,IAAI,OAAO,MAAM,IAAI,oBAAoB,GAAG;EAC7D,SAAS,YAAY;EACrB,KACE,IAAI,IAAI,SAAS,KAAK,YAAY,GAClC,MAAM,MACN,IAAI,SAAS,KAAK,YAAY,GAC9B;GACA,MAAM,QAAQ,EAAE;GAEhB,IAAI,UAAU,SAAS,OAAO,IAAI,KAAK;EACzC;EAKA,IAAI,gBAAgB;GAClB,MAAM,aAAa,IAAI,OAAO,MAAM,IAAI,YAAY,GAAG;GAEvD,WAAW,YAAY;GACvB,KACE,IAAI,IAAI,WAAW,KAAK,cAAc,GACtC,MAAM,MACN,IAAI,WAAW,KAAK,cAAc,GAElC,OAAO,IAAI,EAAE,EAAE;EAEnB;EAEA,IAAI,OAAO,OAAO,GAChB,OAAO,IAAI,eAAe,MAAM;CAEpC;CAEA,OAAO;AACT"}
1
+ {"version":3,"file":"extractVueFieldUsage.mjs","names":[],"sources":["../../src/extractVueFieldUsage.ts"],"sourcesContent":["/**\n * Extracts intlayer dictionary field usage from a Vue SFC for a set of\n * plain variable bindings (i.e. `const content = useIntlayer('key')`).\n *\n * Three access patterns are recognised:\n *\n * 1. `varName.fieldName` in script blocks\n * `useIntlayer` returns a deep reactive proxy, so content fields are read\n * directly on the variable (`content.title`, `content.count(2)`), the\n * `.value` unwrapping happening on the leaf node instead.\n *\n * 2. `varName.value.fieldName` in script blocks\n * Legacy `Ref<Content>` accessor indirection, where fields live one level\n * deeper than the variable.\n *\n * 3. `varName.fieldName` in the template block\n * Inside `<template>`, Vue automatically unwraps top-level refs, so\n * fields are accessed directly without `.value`.\n *\n * The template block is extracted via `@vue/compiler-sfc` so that nested\n * `<template v-for>` / `<template v-if>` tags do not confuse the parser.\n * Falls back to a greedy regex when the package is unavailable.\n */\n\nimport vueSfc from '@vue/compiler-sfc';\n\n/** Escapes special regex characters in a string used as a regex literal. */\nconst escapeRegExp = (str: string): string =>\n str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n/**\n * Runs `fieldRegex` over `source` and adds every captured field name to\n * `fields`, skipping the `value` accessor of an intlayer node (it addresses the\n * node wrapper, not a content field).\n *\n * Over-approximating is safe here: an extra name only keeps a field that could\n * have been pruned, while a missing name removes content the app reads.\n */\nconst collectFieldNames = (\n source: string,\n fieldRegex: RegExp,\n fields: Set<string>\n): void => {\n fieldRegex.lastIndex = 0;\n\n for (\n let match = fieldRegex.exec(source);\n match !== null;\n match = fieldRegex.exec(source)\n ) {\n const field = match[1];\n if (field && field !== 'value') fields.add(field);\n }\n};\n\n/** Input descriptor for a single plain variable binding. */\nexport type PlainVariableInfo = {\n /** The local variable name in the script (`content` in `const content = useIntlayer(…)`). */\n variableName: string;\n /** The intlayer dictionary key passed to `useIntlayer`. */\n dictionaryKey: string;\n};\n\n/**\n * Splits a Vue SFC source string into its script and template regions.\n *\n * Uses `@vue/compiler-sfc` to get exact character offsets so that nested\n * `<template>` tags (used by Vue directives like `v-for` / `v-if`) never\n * cause the template region to be mis-identified.\n */\nconst splitVueSfc = (\n code: string\n): { scriptSource: string; templateSource: string } => {\n try {\n const { descriptor } = (\n vueSfc.parse as (src: string) => { descriptor: any }\n )(code);\n\n const templateSource: string = descriptor.template?.content ?? '';\n\n const scriptParts: string[] = [];\n if (descriptor.script?.content) scriptParts.push(descriptor.script.content);\n if (descriptor.scriptSetup?.content)\n scriptParts.push(descriptor.scriptSetup.content);\n const scriptSource = scriptParts.join('\\n');\n\n return { scriptSource, templateSource };\n } catch {\n // @vue/compiler-sfc not available or parse failed.\n // Fall back: use a greedy regex to capture everything between the\n // outermost <template> tags (robust against nested tags unlike the\n // non-greedy variant).\n const templateMatch = /<template(?:[^>]*)>([\\s\\S]*)<\\/template>/i.exec(\n code\n );\n const templateSource = templateMatch ? templateMatch[1] : '';\n const scriptSource = code.replace(/<template[\\s\\S]*<\\/template>/gi, '');\n return { scriptSource, templateSource };\n }\n};\n\n/**\n * Analyzes a Vue SFC source string and returns the top-level content field\n * names that are statically accessed for each plain intlayer variable binding.\n *\n * @param code - Full `.vue` file source.\n * @param plainVariables - List of plain variable bindings to analyse.\n * @returns Map from dictionary key to the set of accessed top-level field names.\n * If no fields can be determined for a given key it is omitted from the\n * map so the caller can fall back to `'all'`.\n */\nexport const extractVueIntlayerFieldUsage = (\n code: string,\n plainVariables: PlainVariableInfo[]\n): Map<string, Set<string>> => {\n const result = new Map<string, Set<string>>();\n\n if (plainVariables.length === 0) return result;\n\n const { scriptSource, templateSource } = splitVueSfc(code);\n\n for (const { variableName, dictionaryKey } of plainVariables) {\n const fields = new Set<string>();\n const esc = escapeRegExp(variableName);\n\n // ── 1. Script pattern: varName.fieldName ───────────────────────────────\n // The proxy returned by `useIntlayer` exposes content fields directly, so\n // scripts read them without any indirection. `.value` is the leaf-node\n // accessor, never a content field — the deeper name is picked up by the\n // `.value.` pattern below.\n collectFieldNames(\n scriptSource,\n new RegExp(`\\\\b${esc}\\\\.(\\\\w+)`, 'g'),\n fields\n );\n\n // ── 2. Script pattern: varName.value.fieldName ─────────────────────────\n // Legacy reactive ref accessor; the actual content fields live one level\n // deeper than the variable itself.\n collectFieldNames(\n scriptSource,\n new RegExp(`\\\\b${esc}\\\\.value\\\\.(\\\\w+)`, 'g'),\n fields\n );\n\n // ── 3. Template pattern: varName.fieldName ─────────────────────────────\n // Inside `<template>` Vue auto-unwraps refs, so content is accessed\n // directly without `.value`.\n if (templateSource) {\n collectFieldNames(\n templateSource,\n new RegExp(`\\\\b${esc}\\\\.(\\\\w+)`, 'g'),\n fields\n );\n }\n\n if (fields.size > 0) {\n result.set(dictionaryKey, fields);\n }\n }\n\n return result;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,MAAM,gBAAgB,QACpB,IAAI,QAAQ,uBAAuB,MAAM;;;;;;;;;AAU3C,MAAM,qBACJ,QACA,YACA,WACS;CACT,WAAW,YAAY;CAEvB,KACE,IAAI,QAAQ,WAAW,KAAK,MAAM,GAClC,UAAU,MACV,QAAQ,WAAW,KAAK,MAAM,GAC9B;EACA,MAAM,QAAQ,MAAM;EACpB,IAAI,SAAS,UAAU,SAAS,OAAO,IAAI,KAAK;CAClD;AACF;;;;;;;;AAiBA,MAAM,eACJ,SACqD;CACrD,IAAI;EACF,MAAM,EAAE,eACN,OAAO,MACP,IAAI;EAEN,MAAM,iBAAyB,WAAW,UAAU,WAAW;EAE/D,MAAM,cAAwB,CAAC;EAC/B,IAAI,WAAW,QAAQ,SAAS,YAAY,KAAK,WAAW,OAAO,OAAO;EAC1E,IAAI,WAAW,aAAa,SAC1B,YAAY,KAAK,WAAW,YAAY,OAAO;EAGjD,OAAO;GAAE,cAFY,YAAY,KAAK,IAElB;GAAG;EAAe;CACxC,QAAQ;EAKN,MAAM,gBAAgB,4CAA4C,KAChE,IACF;EACA,MAAM,iBAAiB,gBAAgB,cAAc,KAAK;EAE1D,OAAO;GAAE,cADY,KAAK,QAAQ,kCAAkC,EAChD;GAAG;EAAe;CACxC;AACF;;;;;;;;;;;AAYA,MAAa,gCACX,MACA,mBAC6B;CAC7B,MAAM,yBAAS,IAAI,IAAyB;CAE5C,IAAI,eAAe,WAAW,GAAG,OAAO;CAExC,MAAM,EAAE,cAAc,mBAAmB,YAAY,IAAI;CAEzD,KAAK,MAAM,EAAE,cAAc,mBAAmB,gBAAgB;EAC5D,MAAM,yBAAS,IAAI,IAAY;EAC/B,MAAM,MAAM,aAAa,YAAY;EAOrC,kBACE,cACA,IAAI,OAAO,MAAM,IAAI,YAAY,GAAG,GACpC,MACF;EAKA,kBACE,cACA,IAAI,OAAO,MAAM,IAAI,oBAAoB,GAAG,GAC5C,MACF;EAKA,IAAI,gBACF,kBACE,gBACA,IAAI,OAAO,MAAM,IAAI,YAAY,GAAG,GACpC,MACF;EAGF,IAAI,OAAO,OAAO,GAChB,OAAO,IAAI,eAAe,MAAM;CAEpC;CAEA,OAAO;AACT"}
@@ -3,13 +3,18 @@
3
3
  * Extracts intlayer dictionary field usage from a Vue SFC for a set of
4
4
  * plain variable bindings (i.e. `const content = useIntlayer('key')`).
5
5
  *
6
- * Two access patterns are recognised:
6
+ * Three access patterns are recognised:
7
7
  *
8
- * 1. `varName.value.fieldName` in script blocks
9
- * Vue's `useIntlayer` returns a reactive `Ref<Content>`, so fields are
10
- * accessed one level deeper via `.value`.
8
+ * 1. `varName.fieldName` in script blocks
9
+ * `useIntlayer` returns a deep reactive proxy, so content fields are read
10
+ * directly on the variable (`content.title`, `content.count(2)`), the
11
+ * `.value` unwrapping happening on the leaf node instead.
11
12
  *
12
- * 2. `varName.fieldName` in the template block
13
+ * 2. `varName.value.fieldName` in script blocks
14
+ * Legacy `Ref<Content>` accessor indirection, where fields live one level
15
+ * deeper than the variable.
16
+ *
17
+ * 3. `varName.fieldName` in the template block
13
18
  * Inside `<template>`, Vue automatically unwraps top-level refs, so
14
19
  * fields are accessed directly without `.value`.
15
20
  *
@@ -1 +1 @@
1
- {"version":3,"file":"extractVueFieldUsage.d.ts","names":[],"sources":["../../src/extractVueFieldUsage.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;KA0BY;;EAEV;;EAEA;;;;;;;;;;;;cAmDW,+BAA4B,cAC3B,gBACI,wBACf,YAAY"}
1
+ {"version":3,"file":"extractVueFieldUsage.d.ts","names":[],"sources":["../../src/extractVueFieldUsage.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;KAwDY;;EAEV;;EAEA;;;;;;;;;;;;cAmDW,+BAA4B,cAC3B,gBACI,wBACf,YAAY"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/vue-compiler",
3
- "version": "9.3.2",
3
+ "version": "9.3.3",
4
4
  "private": false,
5
5
  "description": "Vite-compatible compiler plugin for Vue with Intlayer, providing HMR support, file transformation, and optimized dictionary loading for Vue applications.",
6
6
  "keywords": [
@@ -78,8 +78,8 @@
78
78
  },
79
79
  "dependencies": {
80
80
  "@babel/core": "8.0.1",
81
- "@intlayer/config": "9.3.2",
82
- "@intlayer/types": "9.3.2",
81
+ "@intlayer/config": "9.3.3",
82
+ "@intlayer/types": "9.3.3",
83
83
  "@vue/compiler-sfc": ">=3.0.0",
84
84
  "fast-glob": "3.3.3",
85
85
  "magic-string": "1.1.0"
@@ -95,7 +95,7 @@
95
95
  "rimraf": "6.1.3",
96
96
  "tsdown": "0.22.14",
97
97
  "typescript": "7.0.2",
98
- "vitest": "4.1.10"
98
+ "vitest": "4.1.11"
99
99
  },
100
100
  "peerDependencies": {
101
101
  "@babel/core": ">=6.0.0",