如何导入自定义ES6模块?

时间:2020-11-12 14:42:03

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

我在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. 我应该使用带有closure-compiler-v20200504.jar标志的相对于--js_module_root的路径。但是,将第1行更改为
import { reorder } from '../home/static/global/mjs/modules/reorder.min.mjs';

也不起作用。

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

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

尝试放置../../static/global/js/modules/reoder.mjs或什至../../global/js/modules/reoder.mjs其中一个可以处理此问题:)

相关问题