anywhichway / jsxdirect

A browser based JSX transpiler with automatic support for React, preact, and hyperapp.
MIT License
23 stars 1 forks source link

How to you load a script with this? #1

Open vans163 opened 5 years ago

vans163 commented 5 years ago

How do you load a script like ?

anywhichway commented 5 years ago

@vans163 This is not currently possible. Only JSX within the script tags is supported. That being said, it is not impossible to implement. There would just need to be some conditional logic looking for an src attribute and doing fetches to get the strings to process.

if(scripts.length===0) scripts = document.querySelectorAll("[type='text/jsx']");
    scripts.forEach(el => { // change into a for of loop
                        // look for 'src' attribute, if found, fetch text content
                        // otherwise user innerHTML
            const script = JSXTranspile(el.innerHTML,options),
                node = document.createElement("script");
        for(const attr of [].slice.call(el.attributes)) node.setAttribute(attr.name,el.attributes[attr.name]);
        node.type = "text/javascript";
        node.innerHTML = script;
        el.parentElement.replaceChild(node,el);
    });
vans163 commented 5 years ago

This is cool yea, good to declutter the code a bit.