简体   繁体   English

NW.js + Babel:ES6导入正常但不能导出吗?

[英]NW.js + Babel: ES6 import working but not export?

I'm building a NW.js app, currently with babel-standalone and React. 我正在构建一个NW.js应用程序,当前使用babel-standalone和React。 I can use ES6 import, but ES6 export on the other hand does not work, console spits out unexpected token export . 我可以使用ES6导入,但是另一方面,ES6导出不起作用,控制台吐出了unexpected token export What's going on? 这是怎么回事?

index.html: index.html:

<html>
<head>
    <meta charset="utf-8">
    <script src="assets/react.min.js" charset="utf-8"></script>
    <script src="assets/react-dom.min.js" charset="utf-8"></script>
    <script src="assets/babel.min.js" charset="utf-8"></script>
</head>
<body>
    <script type="text/babel" src="script/App.js"></script>
</body>
</html>

(yes, Babel indeed works, since React stuff inside runs OK) (是的,Babel确实有效,因为里面的React东西运行正常)

In app.js: 在app.js中:

import Lib from "./script/lib.js";

(and it's indeed exporting lib.js correctly, since that is the file responsible for the error) (并且确实可以正确导出lib.js,因为这是导致错误的文件)

In script/lib.js: 在script / lib.js中:

export default class {...};

I'm aware I can use Node modules instead, or even HTML script loading, but that's beside the point. 我知道我可以改为使用Node模块,甚至可以使用HTML脚本加载,但这是重点。 I want to know why export doesn't work even if Babel doesn't seem to be broken, and even import works fine. 我想知道为什么即使Babel似乎没有损坏, export也不起作用,甚至import正常工作。

The problem is that Babel doesn't see files that were loaded via require , and they are loaded as they are, without transpilation. 问题是Babel看不到通过require加载的文件,并且它们按原样加载,没有进行编译。

There can be several ways to work this around, but the easiest one will be using Babel at build step. 可以采用多种方法来解决此问题,但最简单的方法是在构建步骤中使用Babel。

Process your source code and then load processed code nw.js environment. 处理您的源代码,然后加载处理后的代码nw.js环境。 The example how to do that you can find at this boilerplate project 此样板项目中可以找到的示例操作方法

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

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