@intlayer/vue-transformer 8.1.2 → 8.1.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.
|
@@ -1,29 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
-
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
-
__defProp(to, key, {
|
|
14
|
-
get: ((k) => from[k]).bind(null, key),
|
|
15
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
-
value: mod,
|
|
24
|
-
enumerable: true
|
|
25
|
-
}) : target, mod));
|
|
26
|
-
|
|
27
|
-
//#endregion
|
|
28
|
-
|
|
29
|
-
exports.__toESM = __toESM;
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));exports.__toESM=s;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,73 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,
|
|
2
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
|
-
let node_fs_promises = require("node:fs/promises");
|
|
4
|
-
let _vue_compiler_sfc = require("@vue/compiler-sfc");
|
|
5
|
-
let magic_string = require("magic-string");
|
|
6
|
-
magic_string = require_runtime.__toESM(magic_string);
|
|
7
|
-
let ts_morph = require("ts-morph");
|
|
8
|
-
|
|
9
|
-
//#region src/index.ts
|
|
10
|
-
const ATTRIBUTES_TO_EXTRACT = [
|
|
11
|
-
"title",
|
|
12
|
-
"placeholder",
|
|
13
|
-
"alt",
|
|
14
|
-
"aria-label",
|
|
15
|
-
"label"
|
|
16
|
-
];
|
|
17
|
-
const processVueFile = async (filePath, componentKey, packageName, tools, save = true) => {
|
|
18
|
-
const { generateKey, shouldExtract, extractTsContent } = tools;
|
|
19
|
-
const code = await (0, node_fs_promises.readFile)(filePath, "utf-8");
|
|
20
|
-
const sfc = (0, _vue_compiler_sfc.parse)(code);
|
|
21
|
-
const magic = new magic_string.default(code);
|
|
22
|
-
const extractedContent = {};
|
|
23
|
-
const existingKeys = /* @__PURE__ */ new Set();
|
|
24
|
-
if (sfc.descriptor.template) {
|
|
25
|
-
const walkVueAst = (node) => {
|
|
26
|
-
if (node.type === 2) {
|
|
27
|
-
const text = node.content;
|
|
28
|
-
if (shouldExtract(text)) {
|
|
29
|
-
const key = generateKey(text, existingKeys);
|
|
30
|
-
existingKeys.add(key);
|
|
31
|
-
extractedContent[key] = text.replace(/\s+/g, " ").trim();
|
|
32
|
-
magic.overwrite(node.loc.start.offset, node.loc.end.offset, `{{ content.${key} }}`);
|
|
33
|
-
}
|
|
34
|
-
} else if (node.type === 1) node.props.forEach((prop) => {
|
|
35
|
-
if (prop.type === 6 && ATTRIBUTES_TO_EXTRACT.includes(prop.name) && prop.value) {
|
|
36
|
-
const text = prop.value.content;
|
|
37
|
-
if (shouldExtract(text)) {
|
|
38
|
-
const key = generateKey(text, existingKeys);
|
|
39
|
-
existingKeys.add(key);
|
|
40
|
-
extractedContent[key] = text.trim();
|
|
41
|
-
magic.overwrite(prop.loc.start.offset, prop.loc.end.offset, `:${prop.name}="content.${key}"`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
if (node.children) node.children.forEach(walkVueAst);
|
|
46
|
-
};
|
|
47
|
-
walkVueAst(sfc.descriptor.template.ast);
|
|
48
|
-
}
|
|
49
|
-
const scriptBlock = sfc.descriptor.scriptSetup || sfc.descriptor.script;
|
|
50
|
-
if (scriptBlock) {
|
|
51
|
-
const scriptContent = scriptBlock.content;
|
|
52
|
-
const scriptOffset = scriptBlock.loc.start.offset;
|
|
53
|
-
const { extractedContent: scriptExtracted, replacements } = extractTsContent(new ts_morph.Project({ skipAddingFilesFromTsConfig: true }).createSourceFile("temp.ts", scriptContent), existingKeys);
|
|
54
|
-
Object.assign(extractedContent, scriptExtracted);
|
|
55
|
-
for (const { node, key } of replacements) {
|
|
56
|
-
const start = scriptOffset + node.getStart();
|
|
57
|
-
const end = scriptOffset + node.getEnd();
|
|
58
|
-
magic.overwrite(start, end, `content.${key}`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
if (Object.keys(extractedContent).length === 0) return null;
|
|
62
|
-
const importStmt = `import { useIntlayer } from '${packageName}';`;
|
|
63
|
-
const contentDecl = `const content = useIntlayer('${componentKey}');`;
|
|
64
|
-
if (sfc.descriptor.scriptSetup) magic.appendLeft(sfc.descriptor.scriptSetup.loc.start.offset, `\n${importStmt}\n${contentDecl}\n`);
|
|
65
|
-
else if (sfc.descriptor.script) magic.appendLeft(sfc.descriptor.script.loc.start.offset, `\n${importStmt}\n${contentDecl}\n`);
|
|
66
|
-
else magic.prepend(`<script setup>\n${importStmt}\n${contentDecl}\n<\/script>\n`);
|
|
67
|
-
if (save) await (0, node_fs_promises.writeFile)(filePath, magic.toString());
|
|
68
|
-
return extractedContent;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
//#endregion
|
|
72
|
-
exports.processVueFile = processVueFile;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./_virtual/_rolldown/runtime.cjs`);let t=require(`node:fs/promises`),n=require(`@vue/compiler-sfc`),r=require(`magic-string`);r=e.__toESM(r);let i=require(`ts-morph`);const a=[`title`,`placeholder`,`alt`,`aria-label`,`label`],o=async(e,o,s,c,l=!0)=>{let{generateKey:u,shouldExtract:d,extractTsContent:f}=c,p=await(0,t.readFile)(e,`utf-8`),m=(0,n.parse)(p),h=new r.default(p),g={},_=new Set;if(m.descriptor.template){let e=t=>{if(t.type===2){let e=t.content;if(d(e)){let n=u(e,_);_.add(n),g[n]=e.replace(/\s+/g,` `).trim(),h.overwrite(t.loc.start.offset,t.loc.end.offset,`{{ content.${n} }}`)}}else t.type===1&&t.props.forEach(e=>{if(e.type===6&&a.includes(e.name)&&e.value){let t=e.value.content;if(d(t)){let n=u(t,_);_.add(n),g[n]=t.trim(),h.overwrite(e.loc.start.offset,e.loc.end.offset,`:${e.name}="content.${n}"`)}}});t.children&&t.children.forEach(e)};e(m.descriptor.template.ast)}let v=m.descriptor.scriptSetup||m.descriptor.script;if(v){let e=v.content,t=v.loc.start.offset,{extractedContent:n,replacements:r}=f(new i.Project({skipAddingFilesFromTsConfig:!0}).createSourceFile(`temp.ts`,e),_);Object.assign(g,n);for(let{node:e,key:n}of r){let r=t+e.getStart(),i=t+e.getEnd();h.overwrite(r,i,`content.${n}`)}}if(Object.keys(g).length===0)return null;let y=`import { useIntlayer } from '${s}';`,b=`const content = useIntlayer('${o}');`;return m.descriptor.scriptSetup?h.appendLeft(m.descriptor.scriptSetup.loc.start.offset,`\n${y}\n${b}\n`):m.descriptor.script?h.appendLeft(m.descriptor.script.loc.start.offset,`\n${y}\n${b}\n`):h.prepend(`<script setup>\n${y}\n${b}\n<\/script>\n`),l&&await(0,t.writeFile)(e,h.toString()),g};exports.processVueFile=o;
|
|
73
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["MagicString","Project"],"sources":["../../src/index.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { parse as parseVue } from '@vue/compiler-sfc';\nimport MagicString from 'magic-string';\nimport { type Node, Project, type SourceFile } from 'ts-morph';\n\ntype TsReplacement = {\n node: Node;\n key: string;\n type: 'jsx-text' | 'jsx-attribute' | 'string-literal';\n};\n\ntype Tools = {\n generateKey: (text: string, existingKeys: Set<string>) => string;\n shouldExtract: (text: string) => boolean;\n extractTsContent: (\n sourceFile: SourceFile,\n existingKeys: Set<string>\n ) => {\n extractedContent: Record<string, string>;\n replacements: TsReplacement[];\n };\n};\n\n// Kept local as it's specific to Vue attributes, though shared list is 'title', 'placeholder' etc.\n// If we want to strictly mutualize, we can pass it too.\nconst ATTRIBUTES_TO_EXTRACT = [\n 'title',\n 'placeholder',\n 'alt',\n 'aria-label',\n 'label',\n];\n\nexport const processVueFile = async (\n filePath: string,\n componentKey: string,\n packageName: string,\n tools: Tools,\n save: boolean = true\n) => {\n const { generateKey, shouldExtract, extractTsContent } = tools;\n const code = await readFile(filePath, 'utf-8');\n const sfc = parseVue(code);\n const magic = new MagicString(code);\n\n const extractedContent: Record<string, string> = {};\n const existingKeys = new Set<string>();\n\n if (sfc.descriptor.template) {\n const walkVueAst = (node: any) => {\n if (node.type === 2) {\n // NodeTypes.TEXT\n const text = node.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.replace(/\\s+/g, ' ').trim();\n magic.overwrite(\n node.loc.start.offset,\n node.loc.end.offset,\n `{{ content.${key} }}`\n );\n }\n } else if (node.type === 1) {\n // NodeTypes.ELEMENT\n node.props.forEach((prop: any) => {\n if (\n prop.type === 6 && // NodeTypes.ATTRIBUTE\n ATTRIBUTES_TO_EXTRACT.includes(prop.name) &&\n prop.value\n ) {\n const text = prop.value.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.trim();\n magic.overwrite(\n prop.loc.start.offset,\n prop.loc.end.offset,\n `:${prop.name}=\"content.${key}\"`\n );\n }\n }\n });\n }\n\n if (node.children) {\n node.children.forEach(walkVueAst);\n }\n };\n walkVueAst(sfc.descriptor.template.ast);\n }\n\n const scriptBlock = sfc.descriptor.scriptSetup || sfc.descriptor.script;\n if (scriptBlock) {\n const scriptContent = scriptBlock.content;\n const scriptOffset = scriptBlock.loc.start.offset;\n const project = new Project({ skipAddingFilesFromTsConfig: true });\n const sourceFile = project.createSourceFile('temp.ts', scriptContent);\n\n const { extractedContent: scriptExtracted, replacements } =\n extractTsContent(sourceFile, existingKeys);\n Object.assign(extractedContent, scriptExtracted);\n\n for (const { node, key } of replacements) {\n // Calculate absolute pos\n const start = scriptOffset + node.getStart();\n const end = scriptOffset + node.getEnd();\n magic.overwrite(start, end, `content.${key}`);\n }\n }\n\n if (Object.keys(extractedContent).length === 0) return null;\n\n // Inject Script\n const importStmt = `import { useIntlayer } from '${packageName}';`;\n const contentDecl = `const content = useIntlayer('${componentKey}');`;\n\n if (sfc.descriptor.scriptSetup) {\n magic.appendLeft(\n sfc.descriptor.scriptSetup.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else if (sfc.descriptor.script) {\n magic.appendLeft(\n sfc.descriptor.script.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else {\n magic.prepend(`<script setup>\\n${importStmt}\\n${contentDecl}\\n</script>\\n`);\n }\n\n if (save) {\n await writeFile(filePath, magic.toString());\n }\n return extractedContent;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["MagicString","Project"],"sources":["../../src/index.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { parse as parseVue } from '@vue/compiler-sfc';\nimport MagicString from 'magic-string';\nimport { type Node, Project, type SourceFile } from 'ts-morph';\n\ntype TsReplacement = {\n node: Node;\n key: string;\n type: 'jsx-text' | 'jsx-attribute' | 'string-literal';\n};\n\ntype Tools = {\n generateKey: (text: string, existingKeys: Set<string>) => string;\n shouldExtract: (text: string) => boolean;\n extractTsContent: (\n sourceFile: SourceFile,\n existingKeys: Set<string>\n ) => {\n extractedContent: Record<string, string>;\n replacements: TsReplacement[];\n };\n};\n\n// Kept local as it's specific to Vue attributes, though shared list is 'title', 'placeholder' etc.\n// If we want to strictly mutualize, we can pass it too.\nconst ATTRIBUTES_TO_EXTRACT = [\n 'title',\n 'placeholder',\n 'alt',\n 'aria-label',\n 'label',\n];\n\nexport const processVueFile = async (\n filePath: string,\n componentKey: string,\n packageName: string,\n tools: Tools,\n save: boolean = true\n) => {\n const { generateKey, shouldExtract, extractTsContent } = tools;\n const code = await readFile(filePath, 'utf-8');\n const sfc = parseVue(code);\n const magic = new MagicString(code);\n\n const extractedContent: Record<string, string> = {};\n const existingKeys = new Set<string>();\n\n if (sfc.descriptor.template) {\n const walkVueAst = (node: any) => {\n if (node.type === 2) {\n // NodeTypes.TEXT\n const text = node.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.replace(/\\s+/g, ' ').trim();\n magic.overwrite(\n node.loc.start.offset,\n node.loc.end.offset,\n `{{ content.${key} }}`\n );\n }\n } else if (node.type === 1) {\n // NodeTypes.ELEMENT\n node.props.forEach((prop: any) => {\n if (\n prop.type === 6 && // NodeTypes.ATTRIBUTE\n ATTRIBUTES_TO_EXTRACT.includes(prop.name) &&\n prop.value\n ) {\n const text = prop.value.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.trim();\n magic.overwrite(\n prop.loc.start.offset,\n prop.loc.end.offset,\n `:${prop.name}=\"content.${key}\"`\n );\n }\n }\n });\n }\n\n if (node.children) {\n node.children.forEach(walkVueAst);\n }\n };\n walkVueAst(sfc.descriptor.template.ast);\n }\n\n const scriptBlock = sfc.descriptor.scriptSetup || sfc.descriptor.script;\n if (scriptBlock) {\n const scriptContent = scriptBlock.content;\n const scriptOffset = scriptBlock.loc.start.offset;\n const project = new Project({ skipAddingFilesFromTsConfig: true });\n const sourceFile = project.createSourceFile('temp.ts', scriptContent);\n\n const { extractedContent: scriptExtracted, replacements } =\n extractTsContent(sourceFile, existingKeys);\n Object.assign(extractedContent, scriptExtracted);\n\n for (const { node, key } of replacements) {\n // Calculate absolute pos\n const start = scriptOffset + node.getStart();\n const end = scriptOffset + node.getEnd();\n magic.overwrite(start, end, `content.${key}`);\n }\n }\n\n if (Object.keys(extractedContent).length === 0) return null;\n\n // Inject Script\n const importStmt = `import { useIntlayer } from '${packageName}';`;\n const contentDecl = `const content = useIntlayer('${componentKey}');`;\n\n if (sfc.descriptor.scriptSetup) {\n magic.appendLeft(\n sfc.descriptor.scriptSetup.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else if (sfc.descriptor.script) {\n magic.appendLeft(\n sfc.descriptor.script.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else {\n magic.prepend(`<script setup>\\n${importStmt}\\n${contentDecl}\\n</script>\\n`);\n }\n\n if (save) {\n await writeFile(filePath, magic.toString());\n }\n return extractedContent;\n};\n"],"mappings":"2PAyBA,MAAM,EAAwB,CAC5B,QACA,cACA,MACA,aACA,QACD,CAEY,EAAiB,MAC5B,EACA,EACA,EACA,EACA,EAAgB,KACb,CACH,GAAM,CAAE,cAAa,gBAAe,oBAA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O,OAAO,EAAkB,EAAgB,CAEhD,IAAK,GAAM,CAAE,OAAM,SAAS,EAAc,CAExC,IAAM,EAAQ,EAAe,EAAK,UAAU,CACtC,EAAM,EAAe,EAAK,QAAQ,CACxC,EAAM,UAAU,EAAO,EAAK,WAAW,IAAM,EAIjD,GAAI,OAAO,KAAK,EAAiB,CAAC,SAAW,EAAG,OAAO,KAGvD,IAAM,EAAa,gCAAgC,EAAY,IACzD,EAAc,gCAAgC,EAAa,KAmBjE,OAjBI,EAAI,WAAW,YACjB,EAAM,WACJ,EAAI,WAAW,YAAY,IAAI,MAAM,OACrC,KAAK,EAAW,IAAI,EAAY,IACjC,CACQ,EAAI,WAAW,OACxB,EAAM,WACJ,EAAI,WAAW,OAAO,IAAI,MAAM,OAChC,KAAK,EAAW,IAAI,EAAY,IACjC,CAED,EAAM,QAAQ,mBAAmB,EAAW,IAAI,EAAY,gBAAe,CAGzE,GACF,MAAA,EAAA,EAAA,WAAgB,EAAU,EAAM,UAAU,CAAC,CAEtC"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,70 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { parse } from "@vue/compiler-sfc";
|
|
3
|
-
import MagicString from "magic-string";
|
|
4
|
-
import { Project } from "ts-morph";
|
|
5
|
-
|
|
6
|
-
//#region src/index.ts
|
|
7
|
-
const ATTRIBUTES_TO_EXTRACT = [
|
|
8
|
-
"title",
|
|
9
|
-
"placeholder",
|
|
10
|
-
"alt",
|
|
11
|
-
"aria-label",
|
|
12
|
-
"label"
|
|
13
|
-
];
|
|
14
|
-
const processVueFile = async (filePath, componentKey, packageName, tools, save = true) => {
|
|
15
|
-
const { generateKey, shouldExtract, extractTsContent } = tools;
|
|
16
|
-
const code = await readFile(filePath, "utf-8");
|
|
17
|
-
const sfc = parse(code);
|
|
18
|
-
const magic = new MagicString(code);
|
|
19
|
-
const extractedContent = {};
|
|
20
|
-
const existingKeys = /* @__PURE__ */ new Set();
|
|
21
|
-
if (sfc.descriptor.template) {
|
|
22
|
-
const walkVueAst = (node) => {
|
|
23
|
-
if (node.type === 2) {
|
|
24
|
-
const text = node.content;
|
|
25
|
-
if (shouldExtract(text)) {
|
|
26
|
-
const key = generateKey(text, existingKeys);
|
|
27
|
-
existingKeys.add(key);
|
|
28
|
-
extractedContent[key] = text.replace(/\s+/g, " ").trim();
|
|
29
|
-
magic.overwrite(node.loc.start.offset, node.loc.end.offset, `{{ content.${key} }}`);
|
|
30
|
-
}
|
|
31
|
-
} else if (node.type === 1) node.props.forEach((prop) => {
|
|
32
|
-
if (prop.type === 6 && ATTRIBUTES_TO_EXTRACT.includes(prop.name) && prop.value) {
|
|
33
|
-
const text = prop.value.content;
|
|
34
|
-
if (shouldExtract(text)) {
|
|
35
|
-
const key = generateKey(text, existingKeys);
|
|
36
|
-
existingKeys.add(key);
|
|
37
|
-
extractedContent[key] = text.trim();
|
|
38
|
-
magic.overwrite(prop.loc.start.offset, prop.loc.end.offset, `:${prop.name}="content.${key}"`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
if (node.children) node.children.forEach(walkVueAst);
|
|
43
|
-
};
|
|
44
|
-
walkVueAst(sfc.descriptor.template.ast);
|
|
45
|
-
}
|
|
46
|
-
const scriptBlock = sfc.descriptor.scriptSetup || sfc.descriptor.script;
|
|
47
|
-
if (scriptBlock) {
|
|
48
|
-
const scriptContent = scriptBlock.content;
|
|
49
|
-
const scriptOffset = scriptBlock.loc.start.offset;
|
|
50
|
-
const { extractedContent: scriptExtracted, replacements } = extractTsContent(new Project({ skipAddingFilesFromTsConfig: true }).createSourceFile("temp.ts", scriptContent), existingKeys);
|
|
51
|
-
Object.assign(extractedContent, scriptExtracted);
|
|
52
|
-
for (const { node, key } of replacements) {
|
|
53
|
-
const start = scriptOffset + node.getStart();
|
|
54
|
-
const end = scriptOffset + node.getEnd();
|
|
55
|
-
magic.overwrite(start, end, `content.${key}`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (Object.keys(extractedContent).length === 0) return null;
|
|
59
|
-
const importStmt = `import { useIntlayer } from '${packageName}';`;
|
|
60
|
-
const contentDecl = `const content = useIntlayer('${componentKey}');`;
|
|
61
|
-
if (sfc.descriptor.scriptSetup) magic.appendLeft(sfc.descriptor.scriptSetup.loc.start.offset, `\n${importStmt}\n${contentDecl}\n`);
|
|
62
|
-
else if (sfc.descriptor.script) magic.appendLeft(sfc.descriptor.script.loc.start.offset, `\n${importStmt}\n${contentDecl}\n`);
|
|
63
|
-
else magic.prepend(`<script setup>\n${importStmt}\n${contentDecl}\n<\/script>\n`);
|
|
64
|
-
if (save) await writeFile(filePath, magic.toString());
|
|
65
|
-
return extractedContent;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
//#endregion
|
|
69
|
-
export { processVueFile };
|
|
1
|
+
import{readFile as e,writeFile as t}from"node:fs/promises";import{parse as n}from"@vue/compiler-sfc";import r from"magic-string";import{Project as i}from"ts-morph";const a=[`title`,`placeholder`,`alt`,`aria-label`,`label`],o=async(o,s,c,l,u=!0)=>{let{generateKey:d,shouldExtract:f,extractTsContent:p}=l,m=await e(o,`utf-8`),h=n(m),g=new r(m),_={},v=new Set;if(h.descriptor.template){let e=t=>{if(t.type===2){let e=t.content;if(f(e)){let n=d(e,v);v.add(n),_[n]=e.replace(/\s+/g,` `).trim(),g.overwrite(t.loc.start.offset,t.loc.end.offset,`{{ content.${n} }}`)}}else t.type===1&&t.props.forEach(e=>{if(e.type===6&&a.includes(e.name)&&e.value){let t=e.value.content;if(f(t)){let n=d(t,v);v.add(n),_[n]=t.trim(),g.overwrite(e.loc.start.offset,e.loc.end.offset,`:${e.name}="content.${n}"`)}}});t.children&&t.children.forEach(e)};e(h.descriptor.template.ast)}let y=h.descriptor.scriptSetup||h.descriptor.script;if(y){let e=y.content,t=y.loc.start.offset,{extractedContent:n,replacements:r}=p(new i({skipAddingFilesFromTsConfig:!0}).createSourceFile(`temp.ts`,e),v);Object.assign(_,n);for(let{node:e,key:n}of r){let r=t+e.getStart(),i=t+e.getEnd();g.overwrite(r,i,`content.${n}`)}}if(Object.keys(_).length===0)return null;let b=`import { useIntlayer } from '${c}';`,x=`const content = useIntlayer('${s}');`;return h.descriptor.scriptSetup?g.appendLeft(h.descriptor.scriptSetup.loc.start.offset,`\n${b}\n${x}\n`):h.descriptor.script?g.appendLeft(h.descriptor.script.loc.start.offset,`\n${b}\n${x}\n`):g.prepend(`<script setup>\n${b}\n${x}\n<\/script>\n`),u&&await t(o,g.toString()),_};export{o as processVueFile};
|
|
70
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["parseVue"],"sources":["../../src/index.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { parse as parseVue } from '@vue/compiler-sfc';\nimport MagicString from 'magic-string';\nimport { type Node, Project, type SourceFile } from 'ts-morph';\n\ntype TsReplacement = {\n node: Node;\n key: string;\n type: 'jsx-text' | 'jsx-attribute' | 'string-literal';\n};\n\ntype Tools = {\n generateKey: (text: string, existingKeys: Set<string>) => string;\n shouldExtract: (text: string) => boolean;\n extractTsContent: (\n sourceFile: SourceFile,\n existingKeys: Set<string>\n ) => {\n extractedContent: Record<string, string>;\n replacements: TsReplacement[];\n };\n};\n\n// Kept local as it's specific to Vue attributes, though shared list is 'title', 'placeholder' etc.\n// If we want to strictly mutualize, we can pass it too.\nconst ATTRIBUTES_TO_EXTRACT = [\n 'title',\n 'placeholder',\n 'alt',\n 'aria-label',\n 'label',\n];\n\nexport const processVueFile = async (\n filePath: string,\n componentKey: string,\n packageName: string,\n tools: Tools,\n save: boolean = true\n) => {\n const { generateKey, shouldExtract, extractTsContent } = tools;\n const code = await readFile(filePath, 'utf-8');\n const sfc = parseVue(code);\n const magic = new MagicString(code);\n\n const extractedContent: Record<string, string> = {};\n const existingKeys = new Set<string>();\n\n if (sfc.descriptor.template) {\n const walkVueAst = (node: any) => {\n if (node.type === 2) {\n // NodeTypes.TEXT\n const text = node.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.replace(/\\s+/g, ' ').trim();\n magic.overwrite(\n node.loc.start.offset,\n node.loc.end.offset,\n `{{ content.${key} }}`\n );\n }\n } else if (node.type === 1) {\n // NodeTypes.ELEMENT\n node.props.forEach((prop: any) => {\n if (\n prop.type === 6 && // NodeTypes.ATTRIBUTE\n ATTRIBUTES_TO_EXTRACT.includes(prop.name) &&\n prop.value\n ) {\n const text = prop.value.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.trim();\n magic.overwrite(\n prop.loc.start.offset,\n prop.loc.end.offset,\n `:${prop.name}=\"content.${key}\"`\n );\n }\n }\n });\n }\n\n if (node.children) {\n node.children.forEach(walkVueAst);\n }\n };\n walkVueAst(sfc.descriptor.template.ast);\n }\n\n const scriptBlock = sfc.descriptor.scriptSetup || sfc.descriptor.script;\n if (scriptBlock) {\n const scriptContent = scriptBlock.content;\n const scriptOffset = scriptBlock.loc.start.offset;\n const project = new Project({ skipAddingFilesFromTsConfig: true });\n const sourceFile = project.createSourceFile('temp.ts', scriptContent);\n\n const { extractedContent: scriptExtracted, replacements } =\n extractTsContent(sourceFile, existingKeys);\n Object.assign(extractedContent, scriptExtracted);\n\n for (const { node, key } of replacements) {\n // Calculate absolute pos\n const start = scriptOffset + node.getStart();\n const end = scriptOffset + node.getEnd();\n magic.overwrite(start, end, `content.${key}`);\n }\n }\n\n if (Object.keys(extractedContent).length === 0) return null;\n\n // Inject Script\n const importStmt = `import { useIntlayer } from '${packageName}';`;\n const contentDecl = `const content = useIntlayer('${componentKey}');`;\n\n if (sfc.descriptor.scriptSetup) {\n magic.appendLeft(\n sfc.descriptor.scriptSetup.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else if (sfc.descriptor.script) {\n magic.appendLeft(\n sfc.descriptor.script.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else {\n magic.prepend(`<script setup>\\n${importStmt}\\n${contentDecl}\\n</script>\\n`);\n }\n\n if (save) {\n await writeFile(filePath, magic.toString());\n }\n return extractedContent;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["parseVue"],"sources":["../../src/index.ts"],"sourcesContent":["import { readFile, writeFile } from 'node:fs/promises';\nimport { parse as parseVue } from '@vue/compiler-sfc';\nimport MagicString from 'magic-string';\nimport { type Node, Project, type SourceFile } from 'ts-morph';\n\ntype TsReplacement = {\n node: Node;\n key: string;\n type: 'jsx-text' | 'jsx-attribute' | 'string-literal';\n};\n\ntype Tools = {\n generateKey: (text: string, existingKeys: Set<string>) => string;\n shouldExtract: (text: string) => boolean;\n extractTsContent: (\n sourceFile: SourceFile,\n existingKeys: Set<string>\n ) => {\n extractedContent: Record<string, string>;\n replacements: TsReplacement[];\n };\n};\n\n// Kept local as it's specific to Vue attributes, though shared list is 'title', 'placeholder' etc.\n// If we want to strictly mutualize, we can pass it too.\nconst ATTRIBUTES_TO_EXTRACT = [\n 'title',\n 'placeholder',\n 'alt',\n 'aria-label',\n 'label',\n];\n\nexport const processVueFile = async (\n filePath: string,\n componentKey: string,\n packageName: string,\n tools: Tools,\n save: boolean = true\n) => {\n const { generateKey, shouldExtract, extractTsContent } = tools;\n const code = await readFile(filePath, 'utf-8');\n const sfc = parseVue(code);\n const magic = new MagicString(code);\n\n const extractedContent: Record<string, string> = {};\n const existingKeys = new Set<string>();\n\n if (sfc.descriptor.template) {\n const walkVueAst = (node: any) => {\n if (node.type === 2) {\n // NodeTypes.TEXT\n const text = node.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.replace(/\\s+/g, ' ').trim();\n magic.overwrite(\n node.loc.start.offset,\n node.loc.end.offset,\n `{{ content.${key} }}`\n );\n }\n } else if (node.type === 1) {\n // NodeTypes.ELEMENT\n node.props.forEach((prop: any) => {\n if (\n prop.type === 6 && // NodeTypes.ATTRIBUTE\n ATTRIBUTES_TO_EXTRACT.includes(prop.name) &&\n prop.value\n ) {\n const text = prop.value.content;\n if (shouldExtract(text)) {\n const key = generateKey(text, existingKeys);\n existingKeys.add(key);\n extractedContent[key] = text.trim();\n magic.overwrite(\n prop.loc.start.offset,\n prop.loc.end.offset,\n `:${prop.name}=\"content.${key}\"`\n );\n }\n }\n });\n }\n\n if (node.children) {\n node.children.forEach(walkVueAst);\n }\n };\n walkVueAst(sfc.descriptor.template.ast);\n }\n\n const scriptBlock = sfc.descriptor.scriptSetup || sfc.descriptor.script;\n if (scriptBlock) {\n const scriptContent = scriptBlock.content;\n const scriptOffset = scriptBlock.loc.start.offset;\n const project = new Project({ skipAddingFilesFromTsConfig: true });\n const sourceFile = project.createSourceFile('temp.ts', scriptContent);\n\n const { extractedContent: scriptExtracted, replacements } =\n extractTsContent(sourceFile, existingKeys);\n Object.assign(extractedContent, scriptExtracted);\n\n for (const { node, key } of replacements) {\n // Calculate absolute pos\n const start = scriptOffset + node.getStart();\n const end = scriptOffset + node.getEnd();\n magic.overwrite(start, end, `content.${key}`);\n }\n }\n\n if (Object.keys(extractedContent).length === 0) return null;\n\n // Inject Script\n const importStmt = `import { useIntlayer } from '${packageName}';`;\n const contentDecl = `const content = useIntlayer('${componentKey}');`;\n\n if (sfc.descriptor.scriptSetup) {\n magic.appendLeft(\n sfc.descriptor.scriptSetup.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else if (sfc.descriptor.script) {\n magic.appendLeft(\n sfc.descriptor.script.loc.start.offset,\n `\\n${importStmt}\\n${contentDecl}\\n`\n );\n } else {\n magic.prepend(`<script setup>\\n${importStmt}\\n${contentDecl}\\n</script>\\n`);\n }\n\n if (save) {\n await writeFile(filePath, magic.toString());\n }\n return extractedContent;\n};\n"],"mappings":"oKAyBA,MAAM,EAAwB,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