Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Babel to transpile, import and export in the client, without Webpack?

Can I use Babel to compile JSX and export vars through the global namespace?

I don't want to run a random Webpack server.

I'm already wrapping my head around ES6, JSX, Babel, and React, and couldn't care less about another library that complicates such a simple task

Ultimately I would like to export my React class and import in another. Theoretically, it should only block until the dependencies are met, and I don't understand how this could be an anti-pattern, since all of my code and external dependencies are cached locally.

This is default behavior for <script> tags, just not <script type="text/babel">

<script type="text/babel">
    var message = "hello world";
</script>

<script type="text/babel">
    console.log(message); // undefined
</script>

I'm fine with using ES6 export and import, but not another random file server

like image 983
neaumusic Avatar asked Jul 03 '16 03:07

neaumusic


People also ask

Can I use Babel without Webpack?

When (not) to use @babel/standalone. If you're using Babel in production, you should normally not use @babel/standalone. Instead, you should use a build system running on Node. js, such as Webpack, Rollup, or Parcel, to transpile your JS ahead of time.

Can I use import without Webpack?

It's time to fully embrace import/export With JavaScript modules fully supported in every major browser, savvy developers can now deliver production-ready apps without Webpack.

Does webpack transpile?

Webpack bundles our code and outputs a transpiled version down to the target as specified in the configuration file. In our webpack configuration file, module rules allow us to specify different loaders, which is an easy way to display loaders.

What is Babel vs Webpack?

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


1 Answers

EDIT: Apparently export and import functionality was removed from Babel. I'm not sure why, but it has to do with ES6 compliance and possibly security?

Anyways, if you're determined to put them in separate files for dev purposes:

Put the class on a shared object (window)

SuperClass.js must be included before SubClass.js

class MySuperClass () {
    constructor (config) {
        super(config);
    }
}

window.MySuperClass = MySuperClass;

var MySuperClass = window.MySuperClass;

class MySubClass extends MySuperClass () {
    constructor (config) {
        super(config);
    }
}

I'm not sure if this works for very large classes that take Babel a while to transpile

It seems to work so far, will update if I find another solution

like image 62
neaumusic Avatar answered Oct 20 '22 22:10

neaumusic