@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
134 lines (132 loc) • 7.19 kB
JavaScript
import { __require } from "../../_virtual/_rolldown/runtime.mjs";
import * as recast from "recast";
//#region src/init/utils/configManipulation.ts
const { builders: b, namedTypes: n } = recast.types;
const injectImport = (ast, isCJS, importName, source) => {
if (ast.program.body.some((stmt) => {
if (isCJS) return n.VariableDeclaration.check(stmt) && stmt.declarations.some((decl) => n.VariableDeclarator.check(decl) && n.CallExpression.check(decl.init) && n.Identifier.check(decl.init.callee) && decl.init.callee.name === "require" && n.StringLiteral.check(decl.init.arguments[0]) && decl.init.arguments[0].value === source);
return n.ImportDeclaration.check(stmt) && (stmt.source.value === source || stmt.specifiers.some((spec) => n.ImportSpecifier.check(spec) && spec.imported.name === importName || n.ImportDefaultSpecifier.check(spec) && spec.local?.name === importName));
})) return;
const declaration = isCJS ? b.variableDeclaration("const", [b.variableDeclarator(b.identifier(`{ ${importName} }`), b.callExpression(b.identifier("require"), [b.stringLiteral(source)]))]) : b.importDeclaration([b.importSpecifier(b.identifier(importName))], b.stringLiteral(source));
ast.program.body.unshift(declaration);
};
const ensureTsCheck = (ast, content, extension) => {
if ([
"js",
"mjs",
"cjs"
].includes(extension) && !content.includes("@ts-check")) ast.program.body.unshift(b.commentLine(" @ts-check", true, false));
};
const updatePluginArray = (objExpr, propertyName, pluginName) => {
if (!objExpr || objExpr.type !== "ObjectExpression" && !n.ObjectExpression.check(objExpr)) return;
let prop = objExpr.properties.find((p) => {
if (!p?.key) return false;
return (p.key.name || p.key.value) === propertyName;
});
if (!prop) {
prop = b.property("init", b.identifier(propertyName), b.arrayExpression([]));
objExpr.properties.push(prop);
}
const arrayValue = prop.value;
if (arrayValue && (arrayValue.type === "ArrayExpression" || n.ArrayExpression.check(arrayValue))) {
if (!arrayValue.elements.some((el) => {
const callee = el?.callee;
if (!callee) return false;
const name = callee.name || callee.id?.name;
return name === pluginName || name === "il";
})) arrayValue.elements.push(b.callExpression(b.identifier(pluginName), []));
}
};
const genericRecastVisit = (ast, updateConfigObject, callNames = ["defineConfig"]) => {
recast.visit(ast, {
visitExportDefaultDeclaration(path) {
const decl = path.node.declaration;
if (n.ObjectExpression.check(decl)) updateConfigObject(decl);
else if (n.CallExpression.check(decl) && n.Identifier.check(decl.callee) && callNames.includes(decl.callee.name)) {
if (n.ObjectExpression.check(decl.arguments[0])) updateConfigObject(decl.arguments[0]);
} else if (n.Identifier.check(decl)) {
const name = decl.name;
ast.program.body.forEach((stmt) => {
if (n.VariableDeclaration.check(stmt)) stmt.declarations.forEach((vdecl) => {
if (n.VariableDeclarator.check(vdecl) && n.Identifier.check(vdecl.id) && vdecl.id.name === name && n.ObjectExpression.check(vdecl.init)) updateConfigObject(vdecl.init);
});
});
}
return false;
},
visitAssignmentExpression(path) {
const { left, right } = path.node;
if (n.MemberExpression.check(left) && recast.print(left).code === "module.exports") {
if (n.ObjectExpression.check(right)) updateConfigObject(right);
else if (n.CallExpression.check(right) && n.Identifier.check(right.callee) && callNames.includes(right.callee.name)) {
if (n.ObjectExpression.check(right.arguments[0])) updateConfigObject(right.arguments[0]);
}
}
return false;
}
});
};
const updateViteConfig = (content, extension) => {
const ast = recast.parse(content, { parser: __require("recast/parsers/typescript") });
ensureTsCheck(ast, content, extension);
injectImport(ast, extension === "cjs" || content.includes("module.exports") && !content.includes("import "), "intlayer", "vite-intlayer");
genericRecastVisit(ast, (obj) => updatePluginArray(obj, "plugins", "intlayer"));
return recast.print(ast).code;
};
const updateAstroConfig = (content, extension) => {
const ast = recast.parse(content, { parser: __require("recast/parsers/typescript") });
ensureTsCheck(ast, content, extension);
injectImport(ast, extension === "cjs" || content.includes("module.exports") && !content.includes("import "), "intlayer", "astro-intlayer");
genericRecastVisit(ast, (obj) => updatePluginArray(obj, "integrations", "intlayer"));
return recast.print(ast).code;
};
const updateNextConfig = (content, extension) => {
const ast = recast.parse(content, { parser: __require("recast/parsers/typescript") });
ensureTsCheck(ast, content, extension);
injectImport(ast, extension === "cjs" || content.includes("module.exports"), "withIntlayer", "next-intlayer/server");
recast.visit(ast, {
visitExportDefaultDeclaration(path) {
const declaration = path.node.declaration;
if (n.Expression.check(declaration) && !(n.CallExpression.check(declaration) && n.Identifier.check(declaration.callee) && declaration.callee.name === "withIntlayer")) path.get("declaration").replace(b.callExpression(b.identifier("withIntlayer"), [declaration]));
return false;
},
visitAssignmentExpression(path) {
const { left, right } = path.node;
if (n.MemberExpression.check(left) && recast.print(left).code === "module.exports" && !(n.CallExpression.check(right) && n.Identifier.check(right.callee) && right.callee.name === "withIntlayer")) path.get("right").replace(b.callExpression(b.identifier("withIntlayer"), [right]));
return false;
}
});
return recast.print(ast).code;
};
const updateNuxtConfig = (content, extension) => {
const ast = recast.parse(content, { parser: __require("recast/parsers/typescript") });
ensureTsCheck(ast, content, extension);
const updateConfigObject = (objExpr) => {
if (!objExpr || objExpr.type !== "ObjectExpression" && !n.ObjectExpression.check(objExpr)) return;
let modulesProp = objExpr.properties.find((p) => {
if (!p?.key) return false;
return (p.key.name || p.key.value) === "modules";
});
if (!modulesProp) {
modulesProp = b.property("init", b.identifier("modules"), b.arrayExpression([]));
objExpr.properties.push(modulesProp);
}
const modulesValue = modulesProp.value;
if (modulesValue && (modulesValue.type === "ArrayExpression" || n.ArrayExpression.check(modulesValue))) {
if (!modulesValue.elements.some((el) => {
if (n.StringLiteral.check(el) || el.type === "StringLiteral" || el.type === "Literal") return (el.value || el.extra?.rawValue) === "nuxt-intlayer";
return false;
})) modulesValue.elements.push(b.stringLiteral("nuxt-intlayer"));
}
};
genericRecastVisit(ast, updateConfigObject, ["defineNuxtConfig"]);
return recast.print(ast).code;
};
const updateSvelteConfig = (content, extension) => {
const ast = recast.parse(content, { parser: __require("recast/parsers/typescript") });
ensureTsCheck(ast, content, extension);
return recast.print(ast).code;
};
//#endregion
export { updateAstroConfig, updateNextConfig, updateNuxtConfig, updateSvelteConfig, updateViteConfig };
//# sourceMappingURL=configManipulation.mjs.map