Last active
February 17, 2020 14:01
-
-
Save lencioni/6bccd9e5071271da4175776de29f25d0 to your computer and use it in GitHub Desktop.
Webpack module replacements for shims/polyfill packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function arrayIncludes(arr, search, fromIndex) { | |
return arr.includes(search, fromIndex); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function arrayPrototypeFlat(arr, depth) { | |
return arr.flat(depth); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function arrayProtoptypeFlatmap(arr, cb) { | |
return arr.flatMap(cb); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Function.prototype.bind; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function functionPrototypeName(fn) { | |
return fn.name; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Function.call.bind(Object.prototype.hasOwnProperty); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Array.isArray; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* https://twitter.com/lencioni/status/1199719613509980160 | |
* https://webpack.js.org/plugins/normal-module-replacement-plugin/ | |
* | |
* Usage: | |
* | |
* plugins: [ | |
* ...moduleReplacementsPlugins().map( | |
* ([regex, filePath]) => new webpack.NormalModuleReplacementPlugin(regex, filePath), | |
* ), | |
* ] | |
*/ | |
function moduleReplacementsPlugins() { | |
// Avoid unnecessary shims packages nested in our dependencies. | |
const moduleReplacements = [ | |
[/^object[-.]assign$/, require.resolve('./object.assign')], | |
[/^array-includes$/, require.resolve('./array.includes')], | |
[/^array\.prototype\.flat(ten)?$/, require.resolve('./array.prototype.flat')], | |
[/^array\.prototype\.flatmap$/, require.resolve('./array.prototype.flatmap')], | |
[/^function-bind$/, require.resolve('./function-bind')], | |
[/^function\.prototype\.name$/, require.resolve('./function.prototype.name')], | |
[/^has$/, require.resolve('./has')], | |
[/^is-?array$/, require.resolve('./isarray')], | |
[/^promise\.prototype\.finally$/, require.resolve('./promise.prototype.finally')], | |
[/^object-keys$/, require.resolve('./object.keys')], | |
[/^object\.entries$/, require.resolve('./object.entries')], | |
[/^object\.values$/, require.resolve('./object.values')], | |
[/^object\.fromentries$/, require.resolve('./object.fromentries')], | |
[/^object\.getownpropertydescriptors$/, require.resolve('./object.getownpropertydescriptors')], | |
[/^raf$/, require.resolve('./raf')], | |
[/^regexp\.prototype\.flags$/, require.resolve('./regexp.prototype.flags')], | |
[/^string\.prototype\.matchall$/, require.resolve('./string.prototype.matchall')], | |
[/^string\.prototype\.padstart$/, require.resolve('./string.prototype.padstart')], | |
[/^string\.prototype\.padend$/, require.resolve('./string.prototype.padend')], | |
[/^string\.prototype\.trim$/, require.resolve('./string.prototype.trim')], | |
[/^symbol\.prototype\.description$/, require.resolve('./symbol.prototype.description')], | |
]; | |
return moduleReplacements; | |
} | |
module.exports = moduleReplacementsPlugins; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Object.assign; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Object.entries; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Object.fromEntries; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Object.getOwnPropertyDescriptors; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Object.keys; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = Object.values; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function promisePrototypeFinally(promise, cb) { | |
return promise.finally(cb); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = window.requestAnimationFrame.bind(window); | |
module.exports.cancel = window.cancelAnimationFrame.bind(window); | |
module.exports.polyfill = function polyfill() {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function regexpPrototypeFlags(regexp) { | |
return regexp.flags; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function stringPrototypeMatchall(str, regex) { | |
return String(str).matchAll(regex); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function stringPrototypePadEnd(x, length, padStr) { | |
return String(x).padEnd(length, padStr); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function stringPrototypePadStart(x, length, padStr) { | |
return String(x).padStart(length, padStr); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function stringPrototypeTrim(x) { | |
return String(x).trim(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function symbolPrototypeDescription(symbol) { | |
return symbol.description; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment