使用 ES6 模块和 ruby​​ on Rails 的资产管道

发布于 2025-01-13 01:52:18 字数 878 浏览 3 评论 0原文

我正在将旧的基于函数的语法 Javascript 转换为 ES6 类。 以前的代码是这样的:

var SW;
if(SW.app === undefined) SW.app = {};
SW.app.NullLicense = (function () {
    "use strict";

    var NullLicense;
    NullLicense = function () { };
    NullLicense.prototype.handleLicense = function () {
        var t1 = new SW.app.Utility();
        return t1;
    };
    return NullLicense;
}());

现在使用 ES6 模块,代码是这样的

import {Utility} from './Utility.js'
export class NullLicense  {
    "use strict";
    constructor(){ };
   handleLicense  () {
        var t1 = new Utility();
        return t1;
    }
};

由于以前每个模块都连接到全局 SW 变量,所以我们不必担心依赖关系问题。 在 app/asset 中,我们有清单文件 application.js 。它加载代码库中的所有 js 文件。现在,由于我们在每个文件中都有 exportimport 命令,我收到此错误,导出声明可能只出现在模块的顶层 。 我想知道解决这个问题的方法是什么。

谢谢

I am converting old functional-based syntax Javascript to ES6 classes .
Previously code was like this :

var SW;
if(SW.app === undefined) SW.app = {};
SW.app.NullLicense = (function () {
    "use strict";

    var NullLicense;
    NullLicense = function () { };
    NullLicense.prototype.handleLicense = function () {
        var t1 = new SW.app.Utility();
        return t1;
    };
    return NullLicense;
}());

Now with ES6 modules the code is like this

import {Utility} from './Utility.js'
export class NullLicense  {
    "use strict";
    constructor(){ };
   handleLicense  () {
        var t1 = new Utility();
        return t1;
    }
};

Since previously every module was concatenated to global SW variable we didn't have to worry about the dependencies issue.
In app/asset we have our manifest file application.js . It loads all the js files in the code base. Now since we have export and import commands in every file , I am getting this error that export declarations may only appear at top level of a module.
I want to know what can be the workaround for this .

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2025-01-20 01:52:18

尝试使用 Webpacker gem 或 webpack。

Try using Webpacker gem or webpack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文