|
| 1 | +const { |
| 2 | + ReactCroissant, |
| 3 | + VueWaffle, |
| 4 | + ConvertDefaultModulePrefixSirup, |
| 5 | + SvelteWaffle, |
| 6 | +} = require("croffle"); |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * @param {import("croffle").Sirup} sirup |
| 11 | + */ |
| 12 | +function vueKeyconSirup(sirup) { |
| 13 | + sirup.requestId({ |
| 14 | + path: [/vue[3]?-keycon/g, "useKeycon"], |
| 15 | + named: ["isKeydown"], |
| 16 | + }, node => { |
| 17 | + // use ref |
| 18 | + return sirup.utils.createInlinePropertyAccess(node, "value"); |
| 19 | + }); |
| 20 | +} |
| 21 | +/** |
| 22 | + * @param {import("croffle").Sirup} sirup |
| 23 | + */ |
| 24 | +function svelteKeyconSirup(sirup){ |
| 25 | + sirup.requestId({ |
| 26 | + path: [/svelte-keycon/g, "useKeycon"], |
| 27 | + named: ["isKeydown"], |
| 28 | + }, node => { |
| 29 | + // use writable |
| 30 | + return sirup.ts.factory.createIdentifier(`$${node.name.escapedText}`); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * @param {import("croffle").Sirup} sirup |
| 36 | + */ |
| 37 | +function PreviewPropsSirup(sirup) { |
| 38 | + sirup.requestProps((node, { data }) => { |
| 39 | + if (!data.props) { |
| 40 | + data.props = []; |
| 41 | + } |
| 42 | + node.members.forEach(member => { |
| 43 | + const propName = member.name.escapedText; |
| 44 | + |
| 45 | + data.props.push(propName); |
| 46 | + }); |
| 47 | + |
| 48 | + return sirup.utils.copyInterfaceDeclaration(node, { members: [] }); |
| 49 | + }); |
| 50 | + |
| 51 | + sirup.requestLifeCycle("created", (node, { data }) => { |
| 52 | + if (!data.props || !data.props.length) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const statements = data.props.map(name => { |
| 57 | + return sirup.utils.createInlineCroffleAssignment( |
| 58 | + name, |
| 59 | + "Any", |
| 60 | + sirup.factory.createStringLiteral(`$preview_${name}`), |
| 61 | + ); |
| 62 | + }); |
| 63 | + const body = node.body; |
| 64 | + |
| 65 | + return sirup.utils.copyFunctionDeclaration( |
| 66 | + node, |
| 67 | + { |
| 68 | + body: sirup.factory.updateBlock(body, [ |
| 69 | + ...statements, |
| 70 | + ...body.statements, |
| 71 | + ]), |
| 72 | + }, |
| 73 | + ); |
| 74 | + }); |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * @type {import("@croffle/bakery").CroffleConfig[]} |
| 79 | + */ |
| 80 | +const config = [ |
| 81 | + { |
| 82 | + targets: "stories/**/+([0-9A-Za-z])-*/React*App.tsx", |
| 83 | + croissant: () => { |
| 84 | + const croissant = new ReactCroissant(); |
| 85 | + |
| 86 | + croissant.addSirup(PreviewPropsSirup); |
| 87 | + croissant.addSirup(sirup => { |
| 88 | + sirup.convertImport("@/react-moveable", "react-moveable"); |
| 89 | + }); |
| 90 | + croissant.addSirup(ConvertDefaultModulePrefixSirup); |
| 91 | + return croissant; |
| 92 | + }, |
| 93 | + waffle: [ |
| 94 | + // Vue 3 |
| 95 | + (defrosted) => { |
| 96 | + const hasKeycon = !!defrosted.allRequires["react-keycon"]; |
| 97 | + const waffle = new VueWaffle(); |
| 98 | + |
| 99 | + waffle.addSirup(ConvertDefaultModulePrefixSirup); |
| 100 | + |
| 101 | + |
| 102 | + if (hasKeycon) { |
| 103 | + waffle.addSirup( |
| 104 | + sirup => { |
| 105 | + sirup.convertImport("vue3-keycon", "vue-keycon"); |
| 106 | + }, |
| 107 | + vueKeyconSirup, |
| 108 | + ); |
| 109 | + } |
| 110 | + return { |
| 111 | + dist: `./{type}/{name}/App{ext}`, |
| 112 | + waffle, |
| 113 | + }; |
| 114 | + }, |
| 115 | + // Vue 2 |
| 116 | + (defrosted) => { |
| 117 | + const hasKeycon = !!defrosted.allRequires["react-keycon"]; |
| 118 | + const waffle = new VueWaffle({ |
| 119 | + useVue2: true, |
| 120 | + useOptionsAPI: !hasKeycon, |
| 121 | + }); |
| 122 | + |
| 123 | + waffle.addSirup(ConvertDefaultModulePrefixSirup); |
| 124 | + |
| 125 | + if (hasKeycon) { |
| 126 | + waffle.addSirup( |
| 127 | + sirup => { |
| 128 | + sirup.convertImport("vue-keycon", "vue2-keycon"); |
| 129 | + }, |
| 130 | + vueKeyconSirup, |
| 131 | + ); |
| 132 | + } |
| 133 | + return { |
| 134 | + dist: `./{type}/{name}/App{ext}`, |
| 135 | + waffle, |
| 136 | + }; |
| 137 | + }, |
| 138 | + // Svelte |
| 139 | + (defrosted) => { |
| 140 | + const hasKeycon = !!defrosted.allRequires["react-keycon"]; |
| 141 | + const waffle = new SvelteWaffle(); |
| 142 | + |
| 143 | + waffle.addSirup(ConvertDefaultModulePrefixSirup); |
| 144 | + |
| 145 | + if (hasKeycon) { |
| 146 | + waffle.addSirup(svelteKeyconSirup); |
| 147 | + } |
| 148 | + return { |
| 149 | + dist: `./{type}/{name}/App{ext}`, |
| 150 | + waffle, |
| 151 | + }; |
| 152 | + }, |
| 153 | + ], |
| 154 | + }, |
| 155 | +]; |
| 156 | + |
| 157 | + |
| 158 | +module.exports = config; |
0 commit comments