|
| 1 | +const fs = require('fs'); |
| 2 | + |
| 3 | +const pluginName = 'babel-plugin-jsx-preview'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @returns {import('@babel/core').PluginObj} |
| 7 | + */ |
| 8 | +export default function babelPluginJsxPreview() { |
| 9 | + const wrapperTypes = ['div', 'Box', 'Stack']; |
| 10 | + |
| 11 | + /** |
| 12 | + * @type {import('@babel/core').types.JSXElement | import('@babel/core').types.JSXElement['children']} |
| 13 | + */ |
| 14 | + let previewNode = null; |
| 15 | + |
| 16 | + return { |
| 17 | + name: pluginName, |
| 18 | + visitor: { |
| 19 | + ExportDefaultDeclaration(path) { |
| 20 | + const { declaration } = path.node; |
| 21 | + if (declaration.type !== 'FunctionDeclaration') { |
| 22 | + return; |
| 23 | + } |
| 24 | + const { body } = declaration.body; |
| 25 | + /** |
| 26 | + * @type {import('@babel/core').types.ReturnStatement[]} |
| 27 | + */ |
| 28 | + const [lastReturn] = body |
| 29 | + .filter((statement) => { |
| 30 | + return statement.type === 'ReturnStatement'; |
| 31 | + }) |
| 32 | + .reverse(); |
| 33 | + |
| 34 | + const returnedJSX = lastReturn.argument; |
| 35 | + if (returnedJSX.type === 'JSXElement') { |
| 36 | + previewNode = returnedJSX; |
| 37 | + if ( |
| 38 | + wrapperTypes.includes(previewNode.openingElement.name.name) && |
| 39 | + previewNode.children.length > 0 |
| 40 | + ) { |
| 41 | + // Trim blank JSXText to normalize |
| 42 | + // return ( |
| 43 | + // <div /> |
| 44 | + // ) |
| 45 | + // and |
| 46 | + // return ( |
| 47 | + // <Stack> |
| 48 | + // <div /> |
| 49 | + // ^^^^ Blank JSXText including newline |
| 50 | + // </Stacke> |
| 51 | + // ) |
| 52 | + previewNode = previewNode.children.filter((child, index, children) => { |
| 53 | + const isSurroundingBlankJSXText = |
| 54 | + (index === 0 || index === children.length - 1) && |
| 55 | + child.type === 'JSXText' && |
| 56 | + !/[^\s]+/.test(child.value); |
| 57 | + return !isSurroundingBlankJSXText; |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + }, |
| 63 | + post(state) { |
| 64 | + const { maxLines, outputFilename } = state.opts.plugins.find((plugin) => { |
| 65 | + return plugin.key === pluginName; |
| 66 | + }).options; |
| 67 | + |
| 68 | + let hasPreview = false; |
| 69 | + if (previewNode !== null) { |
| 70 | + const [startNode, endNode] = Array.isArray(previewNode) |
| 71 | + ? [previewNode[0], previewNode.slice(-1)[0]] |
| 72 | + : [previewNode, previewNode]; |
| 73 | + const preview = state.code.slice(startNode.start, endNode.end); |
| 74 | + |
| 75 | + const previewLines = preview.split(/\n/); |
| 76 | + // The first line is already trimmed either due to trimmed blank JSXText or because it's a single node which babel already trims. |
| 77 | + // The last line is therefore the meassure for indendation |
| 78 | + const indendation = previewLines.slice(-1)[0].match(/^\s*/)[0].length; |
| 79 | + const deindentedPreviewLines = preview.split(/\n/).map((line, index) => { |
| 80 | + if (index === 0) { |
| 81 | + return line; |
| 82 | + } |
| 83 | + return line.slice(indendation); |
| 84 | + }); |
| 85 | + |
| 86 | + if (deindentedPreviewLines.length <= maxLines) { |
| 87 | + fs.writeFileSync(outputFilename, deindentedPreviewLines.join('\n')); |
| 88 | + hasPreview = true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (!hasPreview) { |
| 93 | + try { |
| 94 | + fs.unlinkSync(outputFilename); |
| 95 | + } catch (error) { |
| 96 | + // Would throw if the file doesn't exist. |
| 97 | + // But we do want to ensure that the file doesn't exist so the error is fine. |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + }; |
| 102 | +} |
0 commit comments