diff --git a/src/display/api.js b/src/display/api.js index b66b7d857..262f16f3c 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals __non_webpack_require__ */ /* eslint no-var: error */ /** @@ -1471,6 +1470,7 @@ const PDFWorker = (function PDFWorkerClosure() { let fakeWorkerCapability; if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { + // eslint-disable-next-line no-undef if (isNodeJS && typeof __non_webpack_require__ === 'function') { // Workers aren't supported in Node.js, force-disabling them there. isWorkerDisabled = true; @@ -1534,8 +1534,22 @@ const PDFWorker = (function PDFWorkerClosure() { return worker.WorkerMessageHandler; } if ((typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) && + // eslint-disable-next-line no-undef (isNodeJS && typeof __non_webpack_require__ === 'function')) { - const worker = __non_webpack_require__(getWorkerSrc()); + // Since bundlers, such as Webpack, cannot be told to leave `require` + // statements alone we are thus forced to jump through hoops in order + // to prevent `Critical dependency: ...` warnings in third-party + // deployments of the built `pdf.js`/`pdf.worker.js` files; see + // https://github.com/webpack/webpack/issues/8826 + // + // The following hack is based on the assumption that code running in + // Node.js won't ever be affected by e.g. Content Security Policies that + // prevent the use of `eval`. If that ever occurs, we should revert this + // to a normal `__non_webpack_require__` statement and simply document + // the Webpack warnings instead (telling users to ignore them). + // + // eslint-disable-next-line no-eval + const worker = eval('require')(getWorkerSrc()); return worker.WorkerMessageHandler; } await loadScript(getWorkerSrc());