如何将蓝鸟(从NPM中拉出)作为AMD模块加载到Dojo项目中? [英] How to load bluebird (pulled from NPM) into a Dojo project as an AMD module?

查看:133
本文介绍了如何将蓝鸟(从NPM中拉出)作为AMD模块加载到Dojo项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个Dojo项目,该项目使用了许多NPM软件包,其中一个是 bluebird,因为我需要在IE中使用Promise.我正在寻找将NPM软件包加载到我的项目中的最佳实践/推荐方法.

I am working on a Dojo project which uses a number of NPM packages, one of them being bluebird as I need to use Promise in IE. I am looking for the best practice/recommended way to load NPM packages into my project.

以下代码是说明我的问题的示例:

The following code is an example illustrating my question:

require([
  'dojo/dom',
  'dojo/request',
  'dojo/domReady!'
], function(dom, request) {

  var message = dom.byId('greeting');
  message.innerHTML = "Getting started";

  var p1 = new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve(true);
    }, 1000);
  });

  var p2 = new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve(true);
    }, 1000);
  });

  var p3 = new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve(true);
    }, 1000);
  });

  Promise.all([p1, p2, p3]).then(function() {
    message.innerHTML = "All requests have been completed";
  }, function(error) {
    message.innerHTML = error;
  });
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/1.2.2/bluebird.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js" data-dojo-config="async: true"></script>
<div id="greeting"></div>

为了在IE11中运行代码,我可以通过< script> 标签(如代码所示)加载bluebird库.但是,如果我想通过NPM管理该库,我知道我可以通过 npm install 将其拉出,然后使用< script> 标记从中加载该库磁盘中的 node_modules 文件夹,或通过 dojoConfig 引用 node_modules 文件夹中的库,但是无论哪种方式,我都需要进入 node_modules 文件夹并在其中引用包.我不确定是否建议这样做?例如,CommonJS明确建议不要这样做: http://requirejs.org/docs/node.html :

In order to run the code in IE11, I can load the bluebird library through a <script> tag (as shown in the code). However, if I want to manage the library by NPM, I know I can pull it through npm install, then either use a <script> tag to load the library from the node_modules folder in my disk, or reference the library from the node_modules folder through dojoConfig, but either way I will need to tap into the node_modules folder and reference the package there. I am not sure if it is even recommended to do so? For example, CommonJS clearly suggests NOT to do so: http://requirejs.org/docs/node.html:

使用npm将仅节点的软件包/模块安装到项目的node_modules目录中,但不要将RequireJS配置为在node_modules目录中查找.还要避免使用相对的模块ID来引用作为仅节点模块的模块.因此,请勿执行require("../node_modules/foo/foo"

Use npm to install Node-only packages/modules into the projects node_modules directory, but do not configure RequireJS to look inside the node_modules directory. Also avoid using relative module IDs to reference modules that are Node-only modules. So, do not do something like require("./node_modules/foo/foo"

如果我不应该通过其物理路径引用 node_modules 文件夹中的库(或其他NPM软件包),是否有更好的方法呢?我正在考虑将其加载到Dojo的 require([])调用中可能是最好的地方,但是我不确定如何做到这一点.

If I shouldn't reference the library (or any other NPM package) residing in the node_modules folder through its physical path, is there a better way to do so? I am thinking about loading it in Dojo's require([]) call might be the best place, but I am not exactly sure how to do so.

任何帮助将不胜感激.

推荐答案

您可以使用npm install或bower引入项目blubird.如果您的第三方库不在与dojo兼容的AMD中,则可以使用:

You can use npm install or bower to pull in your project blubird. If you thirdparty library is not in AMD which is compatible with dojo you can use:

define([...,"path/to/thirdparty.js"], function(){
   // ... your code
});

或使用:

require([...,"path/to/thirdparty.js"])

注意:

  • 您不需要使用< script> 加载库,可以像上面的代码段一样使用dojo为您加载它.
  • You do not need to load the library with <script>, you can use dojo to load it for you as in snippet above.

这篇关于如何将蓝鸟(从NPM中拉出)作为AMD模块加载到Dojo项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆