【问题标题】:How do I import custom ES6 modules?如何导入自定义 ES6 模块?
【发布时间】:2020-11-21 07:47:58
【问题描述】:

我在http://localhost:8000/static/home/js/edit_sites.js 有一个JavaScript 文件,它试图访问localhost:8000/static/global/js/modules/reorder.mjs 的模块。

edit_sites.js

import { reorder } from '/static/global/js/modules/reorder.mjs';

$(() => {
  reorder($('.screenshot-list'));
});

reorder.mjs

export const reorder = ($ul) => {
  // reorder screenshots by clicking arrows
  const $up = $ul.find('.controls :first-child');
  const $down = $ul.find('.controls :last-child');
  const paddingBottom = 8;

  $up.on('click', function () {
    const $li = $(this).parents('li');

    if (!$li.is(':first-child')) {
      const $prev = $li.prev();

      $li.animate(
        { bottom: `${$prev.outerHeight() + paddingBottom}px` },
        500,
        function () {
          $(this).after($prev.detach()).prop('style', '');
        },
      );

      $prev.animate(
        { top: `${$li.outerHeight() + paddingBottom}px` },
        500,
        function () {
          $(this).prop('style', '');
        },
      );
    }
  });

  $down.on('click', () => {
    const $li = $(this).parents('li');

    if (!$li.is(':last-child')) {
      const $next = $li.next();

      $li.animate(
        { top: `${$next.outerHeight() + paddingBottom}px` },
        500,
        function () {
          $(this).before($next.detach()).prop('style', '');
        },
      );

      $next.animate(
        { bottom: `${$li.outerHeight() + paddingBottom}px` },
        500,
        function () {
          $(this).prop('style', '');
        },
      );
    }
  });

  // update value of hidden order field on submit
  $('form').on('submit', function () {
    let order = '[';

    $ul.children('li').each(function () {
      order += `${$(this).data('id')}`;

      if (!$(this).is(':last-child')) {
        order += ', ';
      }
    });

    order += ']';

    $('input[name="order"]').val(order);

    return true;
  });
};

但是,当我尝试使用 Closure 缩小它时,我收到以下错误消息:

java -jar bin/closure-compiler-v20200504.jar --language_in=STABLE --js home/static/home/js/edit_sites.js --js_output_file home/static/home/js/edit_sites.min.js
home/static/home/js/edit_sites.js:1: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "/static/global/js/modules/reorder.mjs"
import { reorder } from '/static/global/js/modules/reorder.mjs';
^

1 error(s), 0 warning(s)

我想知道我应该如何定义路径,因为多次尝试都没有成功。我尝试过的一些(许多)路径:

import { reorder } from './home/static/global/js/modules/reorder.mjs';
import { reorder } from './static/global/js/modules/reorder.mjs';
import { reorder } from '/home/matt/Repositories/project-dir/home/static/global/js/modules/reorder.mjs';

我认为部分问题是 Closure 试图从与脚本不同的位置访问模块。我是编写模块的新手,所以我不完全确定. 目录在哪里。闭包在/home/matt/Repositories/project-dir 中执行,但是当我从那里停止我的路径时,我得到一个错误。这是我的静态目录结构,省略了不相关的文件,以防您想直观地浏览:

arch-laptop static > tree (pwd)
/home/matt/Repositories/project-dir/home/static
├── global
│   ├── img
│   ├── js
│   │   └── modules
│   │       └── reorder.mjs
│   └── sass
├── home
│   ├── css
│   ├── img
│   ├── js
│   │   └── edit_sites.js
│   └── sass
└── lib
    ├── css
    └── js

13 directories, 55 files

编辑

我应该在我原来的帖子中明确说明我的最终目标是:

  1. 缩小这两个文件,然后
  2. 正确使用它们。

尽管在这里收到了有用的信息,但我仍然停留在第一步。我已经能够缩小 ES6 模块 reorder.mjsreorder.min.mjs,但不能缩小常规 ES6 文件 edit_sites.js

我发现了一个可能有用的 Closure 编译选项:

java -jar ./bin/closure-compiler-v20201102.jar --help | less -R
…
JS Modules:
 --js_module_root VAL                   : Path prefixes to be removed from ES6
                                          & CommonJS modules.
…

...所以我更新了 edit_sites.js 的第 1 行,以包含新创建的 reorder.min.mjs 文件的路径,相对于 edit_sites.js(如下面的两个答案所建议的):

import { reorder } from '../../global/mjs/modules/reorder.min.mjs';

...但是当我尝试编译时,我得到了和以前一样的错误:

java -jar bin/closure-compiler-v20201102.jar --js_module_root ../../global/mjs/modules --js home/static/home/js/edit_sites.js --js_output_file home/static/home/js/edit_sites.min.js
home/static/home/js/edit_sites.js:2:0: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "../../global/mjs/modules/reorder.min.mjs"
  2| import { reorder } from '../../global/mjs/modules/reorder.min.mjs';
     ^

1 error(s), 0 warning(s)

我想知道是否:

  1. 我应该在常规 JS 文件中使用 import 语句。例如,我尝试从 edit_sites.js 中删除导入,将其缩小而不会出现错误,然后使用以下 HTML 进行导入:
<script type="module" src="/static/global/mjs/reorder.min.mjs"></script>
<script src="/static/home/js/edit_sites.min.js"></script>

        但是,我在控制台中收到此错误:

11:23:23.859 Uncaught ReferenceError: assignment to undeclared variable reorder
    <anonymous> http://localhost:8000/static/global/mjs/reorder.min.mjs:8
reorder.min.mjs:8:69
  1. 我应该使用带有--js_module_root 标志的相对于closure-compiler-v20200504.jar 的路径。但是,将第 1 行更改为
import { reorder } from '../home/static/global/mjs/modules/reorder.min.mjs';

        也不行。

【问题讨论】:

    标签: javascript es6-modules google-closure-compiler


    【解决方案1】:

    我会使用 '../' 后退几个文件夹,然后使用 '/:name' 前进。

    尝试: 从'../../global/js/modules/reorder.mjs'导入{重新排序};

    此外,如果您的文本编辑器具有智能感知功能,它应该会在您键入“/”后向您推荐文件夹。对于这样的项目,我建议使用 vscode 或 sublime。

    【讨论】:

    • 我用的是VS Code,IntelliSense已经帮我建立了路径../../global/js/modules/,但是我无法选择文件。它出现在侧边栏中,但不出现在上下文菜单中。这可能与为什么 Closure 找不到它有关吗?另外,我尝试touching 文件,但没有帮助。
    • 如果是 .js 文件,您只能选择带有 IntelliSense 的文件。您的文件是一个 .mjs 文件,这意味着您必须在确定文件夹路径后自己键入 reorder.mjs。我刚刚在 VS Code 中为您的项目建模,导入 reorder.mjs 没有问题。我使用的文件路径是import {reorder} from '../../global/js/modules/reorder.mjs';
    • 我在问题中添加了更多内容。请看一下,看看有没有你或我没有尝试过的东西。
    【解决方案2】:

    闭包正在寻找模块文件,所以路径不正确;

    尝试输入../../static/global/js/modules/reoder.mjs 甚至../../global/js/modules/reoder.mjs 其中一个可以处理这个问题:)

    【讨论】:

    • 那些没有用,我知道错字。 reoderreorder
    猜你喜欢
    • 2018-03-18
    • 2017-03-07
    • 2023-03-22
    • 2020-08-24
    • 2019-01-10
    • 2018-03-24
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多