Javascript ES6:从 URL 导入模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34607252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:38:03  来源:igfitidea点击:

ES6: import module from URL

javascriptecmascript-6es6-module-loader

提问by madox2

Is it possible to import javascript module from external url in ES6?

是否可以从 ES6 中的外部 url 导入 javascript 模块?

I tried (using babel-node):

我试过(使用 babel-node):

import mymodule from 'http://...mysite.../myscript.js';
// Error: Cannot find module 'http://...mysite.../myscript.js'

采纳答案by Benjamin Gruenbaum

2018 Update: The module loader spec is now a part of the ES Spec - what you are describing is allowed and possible with <script type="module">in browsers and with a custom --loaderwith Node.js as well as with Deno if you're into that.

2018 年更新:模块加载器规范现在是 ES 规范的一部分 - 您所描述的内容<script type="module">在浏览器中是允许和可能的,并且可以--loader使用 Node.js 以及 Deno进行自定义,如果您对此感兴趣的话。



The module loader spec and the import/export syntax are separate. So this is a property of the module loader (not a part of the ES spec). If you use a module loader that supports plugins like SystemJS.

模块加载器规范和导入/导出语法是分开的。所以这是模块加载器的一个属性(不是 ES 规范的一部分)。如果您使用支持SystemJS 等插件的模块加载器。

回答by madcampos

TL;DR:

特尔;博士:

For now, no.

目前,没有

Long answer:

长答案:

There are two different specs: the ES6 defines the syntax to exporting/importing. And there is the Loader Specthat actually defines howthis modules will load.

有两种不同的规范:ES6 定义了导出/导入的语法。还有Loader Spec实际上定义这个模块将如何加载。

Spec-speak aside, the important part for us developers is:

撇开规范不谈,对我们开发人员来说,重要的部分是:

The JavaScript Loader allows host environments, like Node.js and browsers, to fetch and load modules on demand. It provides a hookable pipeline, to allow front-end packaging solutions like Browserify, WebPack and jspm to hook into the loading process.

This division provides a single format that developers can use in all JavaScript environments, and a separate loading mechanism for each environment. For example, a Node Loader would load its modules from the file system, using its own module lookup algorithm, while a Browser Loader would fetch modules and use browser-supplied packaging formats.

(...)

The primary goal is to make as much of this process as possible consistent between Node and Browser environments. For example, if a JavaScript program wants to translate .coffeefiles to JavaScript on the fly, the Loader defines a "translate" hook that can be used. This allows programs to participate in the loading process, even though some details (specifically, the process of getting a particular module from its host-defined storage) will be different between environments.

JavaScript 加载器允许主机环境(如 Node.js 和浏览器)按需获取和加载模块。它提供了一个可挂钩的管道,以允许 Browserify、WebPack 和 jspm 等前端打包解决方案挂钩到加载过程中。

这种划分提供了开发人员可以在所有 JavaScript 环境中使用的单一格式,并为每个环境提供了单独的加载机制。例如,节点加载器会使用自己的模块查找算法从文件系统加载其模块,而浏览器加载器会获取模块并使用浏览器提供的打包格式。

(……)

主要目标是使 Node 和浏览器环境之间的此过程尽可能保持一致。例如,如果一个 JavaScript 程序想要动态地将.coffee文件翻译成 JavaScript,Loader 定义了一个可以使用的“翻译”钩子。这允许程序参与加载过程,即使某些细节(特别是从其主机定义的存储中获取特定模块的过程)在环境之间会有所不同。

So we depend on the host environment (node, browser, babel, etc) to resolve/load the modules for us and provide hooks to the process.

所以我们依赖宿主环境(节点、浏览器、babel 等)来为我们解析/加载模块并为进程提供钩子。

回答by BaptWaels

You could also use scriptjswhich in my case requires less configs.

您还可以使用scriptjs,在我的情况下,它需要较少的配置。

var scriptjs = require('scriptjs');

scriptjs('https://api.mapbox.com/mapbox.js/v3.0.1/mapbox.standalone.js', function() {
    L.mapbox.accessToken = 'MyToken';
});