|
| 1 | +export default function afterEffectsJsx(options = {}) { |
| 2 | + return { |
| 3 | + name: "after-effects-jsx", // this name will show up in warnings and errors |
| 4 | + generateBundle(options = {}, bundle, isWrite) { |
| 5 | + // format each file |
| 6 | + // to be ae-jsx |
| 7 | + for (const file in bundle) { |
| 8 | + const originalFile = bundle[file]; |
| 9 | + const originalCode = originalFile.code; |
| 10 | + const exportNames = originalCode |
| 11 | + .split("\n") |
| 12 | + .filter((line) => line.startsWith("exports.")) |
| 13 | + .map((name) => |
| 14 | + name.replace(/exports\..+\s=\s/g, "").replace(";", "") |
| 15 | + ); |
| 16 | + |
| 17 | + // Remove code rollup adds |
| 18 | + let fixedCode = originalCode |
| 19 | + .replace("'use strict';", "") |
| 20 | + .replace( |
| 21 | + "Object.defineProperty(exports, '__esModule', { value: true });", |
| 22 | + "" |
| 23 | + ) |
| 24 | + .replace(/exports\..+\s=\s(({([^;]*)})|.+);/g, ""); |
| 25 | + |
| 26 | + // Replace exported value definitions |
| 27 | + // with jsx compliant (json) syntax |
| 28 | + exportNames.forEach((name) => { |
| 29 | + fixedCode = fixedCode |
| 30 | + .replace(`function ${name}`, `"${name}": function`) |
| 31 | + .replace(`const ${name} =`, `"${name}": `) |
| 32 | + .replace(`let ${name} =`, `"${name}": `) |
| 33 | + .replace(`var ${name} =`, `"${name}": `) |
| 34 | + .replace(`}\n"${name}"`, `}\n"${name}"`) |
| 35 | + .replace(`;\n"${name}"`, `,\n"${name}"`); |
| 36 | + }); |
| 37 | + |
| 38 | + // Separate exported items |
| 39 | + // with commas not semi-colons |
| 40 | + let codeLines = fixedCode.split("\n").map((line) => { |
| 41 | + let newLine = line; |
| 42 | + exportNames.forEach((name) => { |
| 43 | + if (line.startsWith(`"${name}"`)) { |
| 44 | + newLine = newLine.replace(";", ","); |
| 45 | + } |
| 46 | + }); |
| 47 | + if (newLine === "}") { |
| 48 | + newLine = "},"; |
| 49 | + } |
| 50 | + return newLine; |
| 51 | + }); |
| 52 | + |
| 53 | + // Indent code |
| 54 | + fixedCode = codeLines.map((line) => ` ${line}`).join("\n"); |
| 55 | + // Wrap in braces |
| 56 | + const newCode = `{\n ${fixedCode.trim()}\n}`; |
| 57 | + |
| 58 | + // Add replace code of file with modified |
| 59 | + bundle[file].code = newCode; |
| 60 | + } |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
0 commit comments