How do I include a custom method in a babel polyfill?
My goal:
I'm trying to babelify source files into a single output file, to be included in a web app.
My problem:
I have included a custom Promise
method in my source. When babel polyfills the promise, it attempts to call this method from <polyfilledPromise>.default.customMethod
. I have tried adding the custom method to the Promise prototype, but babel doesn't seem to care about the Promise
in my source at all.
My Question:
Is there a way to include a custom function in the promise that babel has polyfilled?
The cli command I'm using:
node_modules/.bin/browserify lib/index.js --s Verifier -o verifier.js -t [ babelify --presets [ babel-preset-latest] --plugins [ transform-async-to-generator transform-runtime ] ]
My deps:
"dependencies": {
"babel-core": "^6.26.3",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-regenerator-runtime": "^6.5.0",
"bigi": "^1.4.2",
"bitcoinjs-lib": "^2.3.0",
"bluebird": "^3.5.1",
"bs58": "^3.0.0",
"buffer": "^5.0.1",
"chai": "^3.5.0",
"debug": "^3.0.1",
"dev": "^0.1.3",
"ecurve": "^1.0.4",
"jsonld": "^0.4.11",
"mocha": "^3.5.3",
"sha256": "^0.2.0",
"string.prototype.startswith": "^0.2.0",
"tsify": "^3.0.3",
"verror": "^1.9.0",
"xmlhttprequest": "^1.8.0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"babel-cli": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"browserify": "^14.3.0",
"uglify-js": "^3.0.8"
}
The custom Promise method:
Promise.properRace = function (promises, count, results = ) {
// Source: https://www.jcore.com/2016/12/18/promise-me-you-wont-use-promise-race/
promises = Array.from(promises);
if (promises.length < count) {
return Promise.reject(new VerifierError("Could not confirm the transaction"));
}
let indexPromises = promises.map((p, index) => p.then(() => index).catch((err) => {
log(err);
throw index;
}));
return Promise.race(indexPromises).then(index => {
let p = promises.splice(index, 1)[0];
p.then(e => results.push(e));
if (count === 1) {
return results;
}
return Promise.properRace(promises, count - 1, results);
}).catch(index => {
promises.splice(index, 1);
return Promise.properRace(promises, count, results);
});
};
One example of babelified version:
_promise2.default.properRace(promises, count, results);
javascript babel babelify
add a comment |
My goal:
I'm trying to babelify source files into a single output file, to be included in a web app.
My problem:
I have included a custom Promise
method in my source. When babel polyfills the promise, it attempts to call this method from <polyfilledPromise>.default.customMethod
. I have tried adding the custom method to the Promise prototype, but babel doesn't seem to care about the Promise
in my source at all.
My Question:
Is there a way to include a custom function in the promise that babel has polyfilled?
The cli command I'm using:
node_modules/.bin/browserify lib/index.js --s Verifier -o verifier.js -t [ babelify --presets [ babel-preset-latest] --plugins [ transform-async-to-generator transform-runtime ] ]
My deps:
"dependencies": {
"babel-core": "^6.26.3",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-regenerator-runtime": "^6.5.0",
"bigi": "^1.4.2",
"bitcoinjs-lib": "^2.3.0",
"bluebird": "^3.5.1",
"bs58": "^3.0.0",
"buffer": "^5.0.1",
"chai": "^3.5.0",
"debug": "^3.0.1",
"dev": "^0.1.3",
"ecurve": "^1.0.4",
"jsonld": "^0.4.11",
"mocha": "^3.5.3",
"sha256": "^0.2.0",
"string.prototype.startswith": "^0.2.0",
"tsify": "^3.0.3",
"verror": "^1.9.0",
"xmlhttprequest": "^1.8.0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"babel-cli": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"browserify": "^14.3.0",
"uglify-js": "^3.0.8"
}
The custom Promise method:
Promise.properRace = function (promises, count, results = ) {
// Source: https://www.jcore.com/2016/12/18/promise-me-you-wont-use-promise-race/
promises = Array.from(promises);
if (promises.length < count) {
return Promise.reject(new VerifierError("Could not confirm the transaction"));
}
let indexPromises = promises.map((p, index) => p.then(() => index).catch((err) => {
log(err);
throw index;
}));
return Promise.race(indexPromises).then(index => {
let p = promises.splice(index, 1)[0];
p.then(e => results.push(e));
if (count === 1) {
return results;
}
return Promise.properRace(promises, count - 1, results);
}).catch(index => {
promises.splice(index, 1);
return Promise.properRace(promises, count, results);
});
};
One example of babelified version:
_promise2.default.properRace(promises, count, results);
javascript babel babelify
add a comment |
My goal:
I'm trying to babelify source files into a single output file, to be included in a web app.
My problem:
I have included a custom Promise
method in my source. When babel polyfills the promise, it attempts to call this method from <polyfilledPromise>.default.customMethod
. I have tried adding the custom method to the Promise prototype, but babel doesn't seem to care about the Promise
in my source at all.
My Question:
Is there a way to include a custom function in the promise that babel has polyfilled?
The cli command I'm using:
node_modules/.bin/browserify lib/index.js --s Verifier -o verifier.js -t [ babelify --presets [ babel-preset-latest] --plugins [ transform-async-to-generator transform-runtime ] ]
My deps:
"dependencies": {
"babel-core": "^6.26.3",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-regenerator-runtime": "^6.5.0",
"bigi": "^1.4.2",
"bitcoinjs-lib": "^2.3.0",
"bluebird": "^3.5.1",
"bs58": "^3.0.0",
"buffer": "^5.0.1",
"chai": "^3.5.0",
"debug": "^3.0.1",
"dev": "^0.1.3",
"ecurve": "^1.0.4",
"jsonld": "^0.4.11",
"mocha": "^3.5.3",
"sha256": "^0.2.0",
"string.prototype.startswith": "^0.2.0",
"tsify": "^3.0.3",
"verror": "^1.9.0",
"xmlhttprequest": "^1.8.0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"babel-cli": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"browserify": "^14.3.0",
"uglify-js": "^3.0.8"
}
The custom Promise method:
Promise.properRace = function (promises, count, results = ) {
// Source: https://www.jcore.com/2016/12/18/promise-me-you-wont-use-promise-race/
promises = Array.from(promises);
if (promises.length < count) {
return Promise.reject(new VerifierError("Could not confirm the transaction"));
}
let indexPromises = promises.map((p, index) => p.then(() => index).catch((err) => {
log(err);
throw index;
}));
return Promise.race(indexPromises).then(index => {
let p = promises.splice(index, 1)[0];
p.then(e => results.push(e));
if (count === 1) {
return results;
}
return Promise.properRace(promises, count - 1, results);
}).catch(index => {
promises.splice(index, 1);
return Promise.properRace(promises, count, results);
});
};
One example of babelified version:
_promise2.default.properRace(promises, count, results);
javascript babel babelify
My goal:
I'm trying to babelify source files into a single output file, to be included in a web app.
My problem:
I have included a custom Promise
method in my source. When babel polyfills the promise, it attempts to call this method from <polyfilledPromise>.default.customMethod
. I have tried adding the custom method to the Promise prototype, but babel doesn't seem to care about the Promise
in my source at all.
My Question:
Is there a way to include a custom function in the promise that babel has polyfilled?
The cli command I'm using:
node_modules/.bin/browserify lib/index.js --s Verifier -o verifier.js -t [ babelify --presets [ babel-preset-latest] --plugins [ transform-async-to-generator transform-runtime ] ]
My deps:
"dependencies": {
"babel-core": "^6.26.3",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-regenerator-runtime": "^6.5.0",
"bigi": "^1.4.2",
"bitcoinjs-lib": "^2.3.0",
"bluebird": "^3.5.1",
"bs58": "^3.0.0",
"buffer": "^5.0.1",
"chai": "^3.5.0",
"debug": "^3.0.1",
"dev": "^0.1.3",
"ecurve": "^1.0.4",
"jsonld": "^0.4.11",
"mocha": "^3.5.3",
"sha256": "^0.2.0",
"string.prototype.startswith": "^0.2.0",
"tsify": "^3.0.3",
"verror": "^1.9.0",
"xmlhttprequest": "^1.8.0"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"babel-cli": "^6.24.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"brfs": "^1.4.3",
"browserify": "^14.3.0",
"uglify-js": "^3.0.8"
}
The custom Promise method:
Promise.properRace = function (promises, count, results = ) {
// Source: https://www.jcore.com/2016/12/18/promise-me-you-wont-use-promise-race/
promises = Array.from(promises);
if (promises.length < count) {
return Promise.reject(new VerifierError("Could not confirm the transaction"));
}
let indexPromises = promises.map((p, index) => p.then(() => index).catch((err) => {
log(err);
throw index;
}));
return Promise.race(indexPromises).then(index => {
let p = promises.splice(index, 1)[0];
p.then(e => results.push(e));
if (count === 1) {
return results;
}
return Promise.properRace(promises, count - 1, results);
}).catch(index => {
promises.splice(index, 1);
return Promise.properRace(promises, count, results);
});
};
One example of babelified version:
_promise2.default.properRace(promises, count, results);
javascript babel babelify
javascript babel babelify
asked Jan 3 at 18:28
cweedcweed
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027857%2fhow-do-i-include-a-custom-method-in-a-babel-polyfill%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027857%2fhow-do-i-include-a-custom-method-in-a-babel-polyfill%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown