MCPcopy Create free account
hub / github.com/craftreactnative/ui / addUnistylesToBabelConfig

Function addUnistylesToBabelConfig

cli/src/utils/build-config.ts:49–138  ·  view source on GitHub ↗
(
  targetPath: string,
  rootFolder: string
)

Source from the content-addressed store, hash-verified

47 * Modifies an existing babel.config.js file to add the unistyles plugin
48 */
49export async function addUnistylesToBabelConfig(
50 targetPath: string,
51 rootFolder: string
52): Promise<boolean> {
53 const babelConfigPath = path.join(targetPath, "babel.config.js");
54
55 try {
56 const content = await fs.readFile(babelConfigPath, "utf8");
57
58 // Check if unistyles plugin is already configured
59 if (content.includes(UNISTYLES_PLUGIN)) {
60 return false; // Already configured
61 }
62
63 // Parse the babel config to understand its structure
64 // This is a simplified approach - we'll try to add the plugin to the plugins array
65
66 // Look for plugins array - handle both single and multi-line formats
67 const pluginsRegex = /plugins\s*:\s*\[([^\]]*)\]/s;
68 const pluginsMatch = content.match(pluginsRegex);
69
70 const unistylesPluginConfig = `[
71 '${UNISTYLES_PLUGIN}',
72 {
73 // pass root folder of your application
74 // all files under this folder will be processed by the Babel plugin
75 root: '${rootFolder}',
76 },
77 ]`;
78
79 if (pluginsMatch) {
80 // Plugins array exists, add our plugin to it
81 const existingPlugins = pluginsMatch[1].trim();
82
83 let newPluginsContent: string;
84 if (existingPlugins === "") {
85 // Empty plugins array
86 newPluginsContent = `\n ${unistylesPluginConfig}\n `;
87 } else {
88 // Has existing plugins, add ours with a comma
89 newPluginsContent = `${existingPlugins},\n ${unistylesPluginConfig}`;
90 }
91
92 const newContent = content.replace(
93 pluginsRegex,
94 `plugins: [${newPluginsContent}]`
95 );
96
97 await fs.writeFile(babelConfigPath, newContent, "utf8");
98 return true;
99 } else {
100 // No plugins array found, add one
101 // Look for the return statement and its closing brace
102 const returnObjectRegex = /(return\s*\{)([\s\S]*?)(\n\s*})/;
103 const returnMatch = content.match(returnObjectRegex);
104
105 if (returnMatch) {
106 const returnStart = returnMatch[1]; // "return {"

Callers 1

setupBabelConfigFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected