From e92f20cf887092eb7c2c5f360ac2eba7db00acea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 20 Apr 2020 20:10:10 +0100 Subject: [PATCH] Fix WebPack compatibility by using dynamic require --- source-map-support.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index 40be2b1..04180b4 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -14,6 +14,16 @@ try { var bufferFrom = require('buffer-from'); +/** + * Requires a module which is protected against bundler minification. + * + * @param {NodeModule} mod + * @param {string} request + */ +function dynamicRequire(mod, request) { + return mod.require(request); +} + // Only install once if called multiple times var errorFormatterInstalled = false; var uncaughtShimInstalled = false; @@ -565,9 +575,8 @@ exports.install = function(options) { // Worker threads. Workers pass the error to the main thread as an event, // rather than printing something to stderr and exiting. try { - // Don't let browserify try to resolve this require(), it's pointless - // and breaks the build process. - var worker_threads = require('worker_' + 'threads'); + // We need to use `dynamicRequire` because `require` on it's own will be optimized by WebPack/Browserify. + var worker_threads = dynamicRequire(module, 'worker_threads'); if (worker_threads.isMainThread === false) { installHandler = false; }