babel-standalone icon indicating copy to clipboard operation
babel-standalone copied to clipboard

Change `type` attribute to other to use with native esm type="module"

Open mizchi opened this issue 6 years ago • 7 comments

Native es modules needs type='module' to compile. babel-standalone need this field so I can't use both.

In Chrome Canary (64)

<script type="module">
import xxx from '/deps/xxx.js';
</script>

mizchi avatar Nov 02 '17 12:11 mizchi

I'm not quite sure how you'd use both together... Can you elaborate on how it'd work? I don't know a lot about ES modules.

Daniel15 avatar Nov 04 '17 22:11 Daniel15

My use case is as follows: a react-based library uses ES6 export. Therefore it has to be imported within a

But the script cannot have both type= "module" and type="text/babel".

The workaround I found is to use another script, and to pass the library exports via window... According to this article the second script should be "defer" but it works without.

<script type="module" >
    import {Comp} from "./myLib.js"
    window.Comp=Comp
</script>
<script defer type="text/babel">
     ReactDOM.render(
		  <Comp/>,
		  document.getElementById('root'));
</script>
<div id="root"></div>

cristianbogdan avatar Apr 23 '18 14:04 cristianbogdan

Actually it is also possible to skip ES6 import and parse the library with Babel so all imports and exports are transpiled (and not really used, but at least the library has the same source code in standalone mode as in node.js use)

<script>var exports = {}; var require=()=>React;</script>
<script type="text/babel"  src="./myLib.js"></script>
<script type="text/babel">
     ReactDOM.render(
		  <Comp/>,
		  document.getElementById('root'));
</script>
<div id="root"></div>

cristianbogdan avatar Apr 23 '18 19:04 cristianbogdan

I just came across this same issue. Where I would like the contents of a <script type='module'> to be transpiled by babel standalone.

Is there a particular reason you chose to use the type attribute as a flag to indicate that the script should be transformed? Or could it have been any attribute like class or data-babel?

lukejacksonn avatar Jun 09 '18 19:06 lukejacksonn

You need to use a type that's not a natively supported one (like text/javascript), otherwise the browser tries to execute the script.

Sent from my phone.

On Sat, Jun 9, 2018, 12:47 PM Luke Jackson [email protected] wrote:

I just came across this same issue. Where I would like the contents of a

Is there a particular reason you chose to use the type attribute as a flag to indicate that the script should be transformed? Or could it have been any attribute like class or data-babel?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/babel/babel-standalone/issues/96#issuecomment-395994115, or mute the thread https://github.com/notifications/unsubscribe-auth/AEuWKvCXhc9gt4MFMAFuYuUx6Z33ulLAks5t7CZTgaJpZM4QPofh .

DanBuild avatar Jun 09 '18 20:06 DanBuild

Humm.. then would it be possible to specify the type of the outputted script.. maybe by saying something like <script type='text/babel/module'>...</script> which outputs:

<script type='module'>${transformedCode}</script>

lukejacksonn avatar Jun 09 '18 21:06 lukejacksonn

Also unable to use the ES6 module attribute type with text/babel in my React JSX apps. I think using a data attribute for babel may be the way to go, freeing up the type attribute for the ES6 module type.

MikelMNJ avatar Jun 23 '18 18:06 MikelMNJ