(options = {})
| 136 | // TODO: add JS support similar to componentToHtml() |
| 137 | export const componentToLiquid: TranspilerGenerator<ToLiquidOptions> = |
| 138 | (options = {}) => |
| 139 | ({ component }) => { |
| 140 | let json = fastClone(component); |
| 141 | if (options.plugins) { |
| 142 | json = runPreJsonPlugins({ json, plugins: options.plugins }); |
| 143 | } |
| 144 | const css = collectCss(json); |
| 145 | stripMetaProperties(json); |
| 146 | if (options.plugins) { |
| 147 | json = runPostJsonPlugins({ json, plugins: options.plugins }); |
| 148 | } |
| 149 | let str = json.children.map((item) => blockToLiquid(item)).join('\n'); |
| 150 | |
| 151 | if (css.trim().length) { |
| 152 | str += `<style>${css}</style>`; |
| 153 | } |
| 154 | |
| 155 | if (options.reactive) { |
| 156 | const stateObjectString = getStateObjectStringFromComponent(json); |
| 157 | if (stateObjectString.trim().length > 4) { |
| 158 | str += `<script reactive> |
| 159 | export default { |
| 160 | state: ${stateObjectString} |
| 161 | } |
| 162 | </script>`; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (options.plugins) { |
| 167 | str = runPreCodePlugins({ json, code: str, plugins: options.plugins }); |
| 168 | } |
| 169 | if (options.prettier !== false) { |
| 170 | try { |
| 171 | str = format(str, { |
| 172 | parser: 'html', |
| 173 | htmlWhitespaceSensitivity: 'ignore', |
| 174 | plugins: [ |
| 175 | // To support running in browsers |
| 176 | require('prettier/parser-html'), |
| 177 | require('prettier/parser-postcss'), |
| 178 | require('prettier/parser-babel'), |
| 179 | ], |
| 180 | }); |
| 181 | } catch (err) { |
| 182 | console.warn('Could not prettify', { string: str }, err); |
| 183 | } |
| 184 | } |
| 185 | if (options.plugins) { |
| 186 | str = runPostCodePlugins({ json, code: str, plugins: options.plugins }); |
| 187 | } |
| 188 | return str; |
| 189 | }; |
no test coverage detected