简体   繁体   English

在没有Webpack的情况下,使用Babel在客户端中进行转换,导入和导出?

[英]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? 我可以使用Babel编译JSX并通过全局命名空间导出变量吗?

I don't want to run a random Webpack server. 我不想运行随机的Webpack服务器。

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 我已经把我的脑袋包裹在ES6,JSX,Babel和React周围,并且对另一个使这么简单的任务复杂化的库无关心

Ultimately I would like to export my React class and import in another. 最终我想导出我的React类并导入另一个。 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>标记的默认行为,而不是<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 我使用ES6导出和导入很好,但不是另一个随机文件服务器

EDIT: Apparently export and import functionality was removed from Babel. 编辑:显然从Babel中删除了导出和导入功能。 I'm not sure why, but it has to do with ES6 compliance and possibly security? 我不确定为什么,但它与ES6合规性和可能的​​安全性有关?

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 必须在SubClass.js之前包含SuperClass.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 我不确定这是否适用于让Babel有一段时间难以理解的非常大的课程

It seems to work so far, will update if I find another solution 它似乎工作到目前为止,如果我找到另一个解决方案将更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM