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by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@gotoandplay/fractal-tsx-adapter@0.13.0/src/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict';\n\nconst _          = require('lodash');\nconst path       = require('path');\nconst decache    = require('./decache.js');\nconst React      = require('react');\nconst ReactDOM   = require('react-dom/server');\nconst Adapter    = require('@frctl/fractal').Adapter;\nconst utils      = require('@frctl/fractal').utils;\n\nconst JsxParser = require('react-jsx-parser').default;\n\nconst DEFAULT_OPTIONS = {\n    renderMethod: 'renderToString',\n    ssr: true,\n    wrapperElements: []\n};\n\n/*\n * React Adapter\n * -------------\n */\nclass ReactAdapter extends Adapter {\n    constructor(source, app, options) {\n        super(null, source);\n        this._app = app;\n\n        if (options.renderMethod == 'renderToString') {\n            this._renderMethod = ReactDOM.renderToString;\n        } else {\n            this._renderMethod = ReactDOM.renderToStaticMarkup;\n        }\n        this.options = options;\n        this.components = {};\n    }\n\n    getAllComponents() {\n        const componentLibrary = {};\n\n        this._app.components.flatten().each((item) => {\n            let component = require(item.viewPath);\n\n            component = component.default || component;\n\n            componentLibrary[component.name] = component;\n        });\n\n        return componentLibrary;\n    }\n\n    wrapChildren(children) {\n        return React.createElement(JsxParser, {\n            jsx: children,\n            components: this.getAllComponents(),\n            renderInWrapper: false\n        });\n    }\n\n    wrapString(str) {\n        if (typeof str !== 'undefined' && typeof str === 'string') {\n            return this.wrapChildren(str);\n        }\n\n        return str;\n    }\n\n    getContext(context, parseJsxFrom) {\n        if (parseJsxFrom && parseJsxFrom.length) {\n            const newContext = JSON.parse(JSON.stringify(context));\n\n            for (const item of parseJsxFrom) {\n                const arr = item.split('.');\n\n                arr.reduce((object, item, index) => {\n                    if (index === arr.length - 1) {\n                        object[item] = this.wrapString(object[item]);\n                    }\n\n                    return object[item];\n                }, newContext);\n            }\n\n            return newContext;\n        }\n\n        return context;\n    }\n\n    getWrapperComponent(component) {\n        if (typeof component === 'string' && component.startsWith('@')) {\n            const comp = this._app.components.flatten().find(component);\n\n            if (comp) {\n                return require(comp.viewPath).default;\n            }\n        }\n\n        return component;\n    }\n\n    renderParentElements(children) {\n        if (this.options.wrapperElements.length) {\n            return this.options.wrapperElements.reverse().reduce((currentElement, wrapperObject) => {\n                const wrapperComponent = this.getWrapperComponent(wrapperObject.component);\n\n                return React.createElement(wrapperComponent, Object.assign({}, wrapperObject.props, {\n                    children: currentElement\n                }));\n            }, children);\n        }\n\n        return children;\n    }\n\n    render(path, str, context, meta) {\n        meta = meta || {};\n\n        global.app = {\n            publicFolder: this.getPath('/', meta)\n        };\n\n        if (((meta.env && meta.env.server && this.options.ssr) || meta.self.meta.ssr) && !meta.self.meta.cache) {\n            decache(path);\n        }\n\n        let component = require(path);\n\n        component = component.default || component;\n\n        meta.env.publicPath = this.getPath('/', meta);\n        meta.env.reactClass = component.name;\n\n        setEnv('_self', meta.self, context);\n        setEnv('_target', meta.target, context);\n        setEnv('_env', meta.env, context);\n        setEnv('_config', this._app.config(), context);\n\n        if (this.options.ssr || meta.env.ssr || meta.self.meta.ssr) {\n            const element = React.createElement(component, this.getContext(context, meta.self.meta.parseJsxFrom));\n            const parentElements = this.renderParentElements(element);\n            const html = this._renderMethod(parentElements);\n\n            return Promise.resolve(html);\n        }\n\n        return '';\n    }\n\n    renderLayout(path, str, context, meta) {\n        meta = meta || {};\n\n        meta.env.publicPath = this.getPath('/', meta);\n\n        setEnv('_self', meta.self, context);\n        setEnv('_target', meta.target, context);\n        setEnv('_env', meta.env, context);\n        setEnv('_config', this._app.config(), context);\n\n        global.app = {\n            publicFolder: this.getPath('/', meta)\n        };\n\n        if (meta.env.server && this.options.ssr) {\n            decache(path);\n        }\n\n        let component = require(path);\n\n        component = component.default || component;\n        const element   = React.createElement(component, context);\n        const html      = '<!DOCTYPE html>' + ReactDOM.renderToStaticMarkup(element);\n\n        return Promise.resolve(html);\n    }\n\n    getPath(assetPath, root) {\n        let returnPath = assetPath;\n        let fractal = this._source._app;\n\n        if (!root || !root.env || root.env.server) {\n            returnPath = assetPath;\n        } else {\n            returnPath = path.dirname(utils.relUrlPath(assetPath, _.get(root.env.request || root.request, 'path', '/'), fractal.web.get('builder.urls'))) + '/';\n        }\n\n        return returnPath;\n    }\n}\n\n/**\n * set environment variables\n * @param {[type]} key     [description]\n * @param {[type]} value   [description]\n * @param {[type]} context [description]\n * @returns {void}\n */\nfunction setEnv(key, value, context) {\n    if (_.isUndefined(context[key]) && !_.isUndefined(value)) {\n        context[key] = value;\n    }\n}\n\n/*\n * Adapter registration\n * --------------------\n */\nmodule.exports = function(config = {}) {\n    const options = _.assign({}, DEFAULT_OPTIONS, config);\n\n    return {\n        register(source, app) {\n            const registerTsconfig = () => {\n                require('tsconfig-paths').register();\n            };\n            const handleLoaded = () => {\n                require('ts-node').register({\n                    transpileOnly: true\n                });\n\n                registerTsconfig();\n            };\n            const handleUpdated = () => {\n                registerTsconfig();\n            };\n\n            app.components.on('loaded', handleLoaded);\n            app.components.on('updated', handleUpdated);\n\n            const adapter = new ReactAdapter(source, app, options);\n\n            return adapter;\n        }\n    };\n};\n"]}