forked from ibm-js/requirejs-dplugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromise.js
51 lines (50 loc) · 1.33 KB
/
Promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Promise plugin.
*
* This plugin returns an ES6 compliant Promise implementation. It returns the implementation from the
* browser if there is one. If the browser does not support Promise, this plugin returns the lie.js
* Promise shim.
*
* @example:
* To create a promise:
* ```
* require(["requirejs-dplugins/Promise!"], function (Promise){
* var promise = new Promise(function (resolve, reject) {
* ...
* });
* });
* ```
*
* @module requirejs-dplugins/Promise
*/
/* global Promise */
define(["require"], function (require) {
var writeFile;
var lieId = "lie/dist/lie";
return {
load: function (name, req, onload, config) {
config = config || {};
if (config.isBuild) {
onload();
} else if (typeof Promise === "function") {
onload(Promise);
} else {
// Use absolute path to allow map configuration.
// Also use a variable to avoid RequireJS detection at build time so it is not included in the
// layer.
require([lieId], function (lie) {
onload(lie);
});
}
},
writeFile: function (pluginName, resource, require, write) {
writeFile = write;
},
onLayerEnd: function () {
var fs = require("fs");
var url = require.toUrl(lieId + ".js");
// copy lie to the build output.
writeFile(url, fs.readFileSync(url));
}
};
});