Skip to content
This repository has been archived by the owner on Jun 29, 2018. It is now read-only.

How to use plugins modules exports and imports? #60

Closed
stefek99 opened this issue Sep 21, 2016 · 10 comments
Closed

How to use plugins modules exports and imports? #60

stefek99 opened this issue Sep 21, 2016 · 10 comments

Comments

@stefek99
Copy link

https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export

default export

I have following code:

<!doctype html>
<html>
<head>
    <title>Babel Test</title>
</head>
<body>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.14.0/babel.min.js"></script>

     <script data-plugins="transform-es2015-modules-umd" type="text/babel">
        export default function cube(x) {
          return x * x * x;
        }   
    </script>

</body>
</html>

I can export function cube but how do I import it?


Note that data-plugins="transform-es2015-modules-umd" is required, otherwise it will throw ReferenceError: exports is not defined

@Daniel15
Copy link
Member

I'm not sure how ES6 modules would work within the context of a browser - How would you define the module name, for example?

@stefek99
Copy link
Author

I'm sorry, not an expert here, and I know my case was pretty edgy...

I think I wanted to edit source file of some library and have it loaded directly into browser as I was working on limited environment (no admin rights, cannot install anything)

@marcioacmauricio
Copy link

I also need to use exports defaults in my work completion university course, if you can find a solution I will be grateful if you share

@ChrisCinelli
Copy link

I think if you have a jsFiddle, I may want to import external files. For example from rawgit.com

@marcioacmauricio
Copy link

@brownieboy
Copy link

brownieboy commented Feb 13, 2017

Native module loading is coming to browsers. In fact, it's already available in Firefox, Safari and Edge, but only in pre-release versions, and then hidden behind a flag. Here's a very good blog post that gives an overview of the current state of play:
https://blog.hospodarets.com/native-ecmascript-modules-the-first-overview

I tested native module loading, using version 54 of the Firefox Nightly build with the dom.moduleScripts.enabled boolean flag set to true. This is bleeding edge stuff to be sure, but I can confirm that it does work!

It would be great if babel-standalone could somehow work together with native module loading to transpile React, for example.

How would one integrate the two though? One stumbling block is that native module loading requires that the initial script is loaded as<script type="module", whereas babel-standalone requires <script type="text/babel" .

See also issue #71

@chobo2
Copy link

chobo2 commented May 16, 2017

What is the solution to get this to work?

@jeffrafter
Copy link
Contributor

test.html:

<!doctype html>
<html>
<head>
  <title>Babel Test</title>
</head>
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.14.0/babel.min.js"></script>
  <script data-plugins="transform-es2015-modules-umd" type="text/babel" src="./cube.js"></script>
  <script data-plugins="transform-es2015-modules-umd" type="text/babel">
    import cube from 'cube'
    console.log(cube(3))
  </script>
</body>
</html>

cube.js

export default function cube(x) {
  return x * x * x;
}

@ryanpcmcquen
Copy link

Is there a solution for this? Should we move this to the main Babel repo for more exposure (now that standalone has moved)?

@Daniel15
Copy link
Member

Daniel15 commented Dec 5, 2017

I think babel/babel#6223 should actually fix this.

If that PR doesn't fix it, let's open a new issue in the Babel repo 😃

@Daniel15 Daniel15 closed this as completed Dec 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants