Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make script type both text/babel and module?

It works perfectly because I haven't included JSX, but when I replace the script type with text/babel, it doesn't work because the module fails to load. browser.js the Babel compiler.

Here... JSX works only when i replace script type with text/babel but the problem is module fails to load since the script is not module. Any idea how make it work with JSX?

<div id="root">

</div>
<script type="module">
    import  './react.min.js';
    import  './react-dom.min.js';
    import  './browser.js';
    class Hello extends React.Component {
        render() {
            return React.createElement('div', null, `Hello ${this.props.toWhat}`);
        }
    }
    ReactDOM.render(
        React.createElement(Hello, {toWhat: 'World'}, null),
        document.getElementById('root')
    );
</script>
like image 747
Shures Nepali Avatar asked Jan 03 '19 07:01

Shures Nepali


People also ask

What is type module in script?

A module is a function or group of similar functions. They are grouped together within a file and contain the code to execute a specific task when called into a larger application. You create modules to better organize and structure your codebase.

How do you add Babel in HTML?

Use it via UNPKG: https://unpkg.com/@babel/standalone/babel.min.js. This is a simple way to embed it on a webpage without having to do any other setup. Install via NPM: npm install --save @babel/standalone.

What is the difference between Webpack and Babel?

Babel can be classified as a tool in the "JavaScript Compilers" category, while Webpack is grouped under "JS Build Tools / JS Task Runners".

How do you use Babel standalone?

You can use babel-standalone to transpile ES6 to ES5 in a browser environment. You just need to load the “babel-standalone” in your script as highlighted below and write the script you want to transpile, in script tag with type “text/babel” or “text/jsx”. Babel will automatically compile and execute the script.


1 Answers

UPDATE July 2021

As per mh sattarian's answer you now don't need data-plugins="transform-es2015-modules-umd" to use native es6 module's import/export etc. Instead you simply add data-type="module"

Original Answer

Just in case if some one comes here looking for answer

There is a support for data-plugins and data-presets in babel standalone

<script data-plugins="transform-es2015-modules-umd" type="text/babel">

see more here Babel standalone

like image 119
nikhil mehta Avatar answered Sep 22 '22 02:09

nikhil mehta