UNPKG

flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

1 lines 5.31 kB
{"version":3,"file":"wrap-default-export.cjs","sources":["../../../src/cli/utils/wrap-default-export.ts"],"sourcesContent":["import { getJsType } from \"./get-js-type\";\n\n/**\n * Wraps the default export of a JavaScript module with a given function.\n *\n * @param {string} content - The content of the JavaScript module.\n * @param {string} withFunction - The function to wrap the default export with.\n * @returns {string} The modified content with the default export wrapped.\n */\nexport function wrapDefaultExport(content: string, withFunction: string): string {\n const { isCJS, isESM } = getJsType(content);\n\n if (!isCJS && !isESM) {\n return content;\n }\n\n let wrappedContent = content;\n\n const config = isCJS ? EXPORT_CONFIGS.cjs : EXPORT_CONFIGS.esm;\n\n // Skip if it's a class/interface export\n if (!content.match(config.skipPattern)) {\n const lastExportMatch = wrappedContent.match(config.matchPattern);\n if (lastExportMatch) {\n const [fullMatch, prefix, rest] = lastExportMatch;\n const { exportValue, restContent } = extractExportValue(rest);\n const replacement = createWrappedReplacement(prefix, exportValue, withFunction, restContent);\n\n // Replace only the last occurrence\n const index = wrappedContent.lastIndexOf(fullMatch);\n wrappedContent = wrappedContent.slice(0, index) + replacement + wrappedContent.slice(index + fullMatch.length);\n }\n }\n\n return wrappedContent;\n}\n\nconst EXPORT_CONFIGS = {\n esm: {\n skipPattern: /export\\s+default\\s+(?:class|interface|abstract\\s+class)\\s+/,\n matchPattern: /(export\\s+default\\s+)([\\s\\S]*$)/m,\n },\n cjs: {\n skipPattern: /module\\.exports\\s*=\\s*(?:class|interface|abstract\\s+class)\\s+/,\n matchPattern: /(module\\.exports\\s*=\\s*)([\\s\\S]*$)/m,\n },\n};\n\n/**\n * Extracts the export value from a string, handling nested structures\n */\nfunction extractExportValue(rest: string): { exportValue: string; restContent: string } {\n let depth = 0;\n let i = 0;\n\n // Parse the export value handling nested parentheses and braces\n for (i = 0; i < rest.length; i++) {\n const char = rest[i];\n if (char === \"(\" || char === \"{\") depth++;\n if (char === \")\" || char === \"}\") depth--;\n\n // Break on semicolon or newline if we're not inside parentheses/braces\n if (depth === 0 && (char === \";\" || char === \"\\n\")) {\n return {\n exportValue: rest.slice(0, char === \";\" ? i + 1 : i),\n restContent: rest.slice(char === \";\" ? i + 1 : i),\n };\n }\n }\n\n // If we didn't find a terminator, use the whole rest\n return {\n exportValue: rest,\n restContent: \"\",\n };\n}\n\n/**\n * Creates a wrapped replacement for an export statement\n */\nfunction createWrappedReplacement(\n prefix: string,\n exportValue: string,\n withFunction: string,\n restContent: string,\n): string {\n const trimmedValue = exportValue.trim();\n const hasTrailingSemi = trimmedValue.endsWith(\";\");\n return `${prefix}${withFunction}(${trimmedValue.replace(/;$/, \"\")})${hasTrailingSemi ? \";\" : \"\"}${restContent}`;\n}\n"],"names":["getJsType"],"mappings":";;;;AAEO,SAAS,iBAAiB,CAAC,OAAO,EAAE,YAAY,EAAE;AACzD,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAGA,mBAAS,CAAC,OAAO,CAAC;AAC7C,EAAE,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,EAAE;AACxB,IAAI,OAAO,OAAO;AAClB;AACA,EAAE,IAAI,cAAc,GAAG,OAAO;AAC9B,EAAE,MAAM,MAAM,GAAG,KAAK,GAAG,cAAc,CAAC,GAAG,GAAG,cAAc,CAAC,GAAG;AAChE,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE;AAC1C,IAAI,MAAM,eAAe,GAAG,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC;AACrE,IAAI,IAAI,eAAe,EAAE;AACzB,MAAM,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,eAAe;AACvD,MAAM,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,GAAG,kBAAkB,CAAC,IAAI,CAAC;AACnE,MAAM,MAAM,WAAW,GAAG,wBAAwB,CAAC,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE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