How to load bluebird (pulled from NPM) into a Dojo project as an AMD module?

老子叫甜甜 提交于 2019-12-08 06:49:42

问题


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>

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:

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"

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.

Any help will be appreciated.


回答1:


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
});

Or use:

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

Notes:

  • You do not need to load the library with <script>, you can use dojo to load it for you as in snippet above.


来源:https://stackoverflow.com/questions/40095312/how-to-load-bluebird-pulled-from-npm-into-a-dojo-project-as-an-amd-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!