From 058b9b79719e1b75fa69e0d75cadddf832394d0c Mon Sep 17 00:00:00 2001 From: Amir Musaev Date: Thu, 29 Apr 2021 22:47:13 +0200 Subject: [PATCH] fix: handle terser error (#703) With the Terser upgrade in https://github.com/vercel/ncc/pull/669 the error handling needed to be updated to handle the new async form of the function. As a result Terser errors were becoming build errors, which is fixed here. This resolves the Terser error regression from https://github.com/vercel/ncc/issues/697. --- src/index.js | 39 +++++++------ test/unit/minify-err/input.js | 2 + test/unit/minify-err/opt.json | 3 + test/unit/minify-err/output-coverage.js | 78 +++++++++++++++++++++++++ test/unit/minify-err/output.js | 78 +++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 17 deletions(-) create mode 100644 test/unit/minify-err/input.js create mode 100644 test/unit/minify-err/opt.json create mode 100644 test/unit/minify-err/output-coverage.js create mode 100644 test/unit/minify-err/output.js diff --git a/src/index.js b/src/index.js index f689a6ae..85419a74 100644 --- a/src/index.js +++ b/src/index.js @@ -462,27 +462,32 @@ function ncc ( } if (minify) { - const result = await terser.minify(code, { - compress: false, - mangle: { - keep_classnames: true, - keep_fnames: true - }, - sourceMap: sourceMap ? { - content: map, - filename, - url: `${filename}.map` - } : false - }); - // For some reason, auth0 returns "undefined"! - // custom terser phase used over Webpack integration for this reason - if (result.code !== undefined) { + let result; + try { + result = await terser.minify(code, { + compress: false, + mangle: { + keep_classnames: true, + keep_fnames: true + }, + sourceMap: sourceMap ? { + content: map, + filename, + url: `${filename}.map` + } : false + }); + // For some reason, auth0 returns "undefined"! + // custom terser phase used over Webpack integration for this reason + if (!result || result.code === undefined) + throw null; + ({ code, map } = { code: result.code, map: sourceMap ? JSON.parse(result.map) : undefined }); - } else { - console.log('An error occurred while minifying. The result will not be minified.') + } + catch { + console.log('An error occurred while minifying. The result will not be minified.'); } } diff --git a/test/unit/minify-err/input.js b/test/unit/minify-err/input.js new file mode 100644 index 00000000..5bfa69fe --- /dev/null +++ b/test/unit/minify-err/input.js @@ -0,0 +1,2 @@ +if (global.GENTLY) require = GENTLY.hijack(__non_webpack_require__); +console.log(require('fs')); diff --git a/test/unit/minify-err/opt.json b/test/unit/minify-err/opt.json new file mode 100644 index 00000000..3dc1021a --- /dev/null +++ b/test/unit/minify-err/opt.json @@ -0,0 +1,3 @@ +{ + "minify": true +} diff --git a/test/unit/minify-err/output-coverage.js b/test/unit/minify-err/output-coverage.js new file mode 100644 index 00000000..6b2c53c4 --- /dev/null +++ b/test/unit/minify-err/output-coverage.js @@ -0,0 +1,78 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 875: +/***/ ((module) => { + +function webpackEmptyContext(req) { + var e = new Error("Cannot find module '" + req + "'"); + e.code = 'MODULE_NOT_FOUND'; + throw e; +} +webpackEmptyContext.keys = () => ([]); +webpackEmptyContext.resolve = webpackEmptyContext; +webpackEmptyContext.id = 875; +module.exports = webpackEmptyContext; + +/***/ }), + +/***/ 747: +/***/ ((module) => { + +"use strict"; +module.exports = require("fs"); + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __nccwpck_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ var threw = true; +/******/ try { +/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/ +var __webpack_exports__ = {}; +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +if (global.GENTLY) __nccwpck_require__(875) = GENTLY.hijack(eval("require")); +console.log(__nccwpck_require__(747)); + +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file diff --git a/test/unit/minify-err/output.js b/test/unit/minify-err/output.js new file mode 100644 index 00000000..6b2c53c4 --- /dev/null +++ b/test/unit/minify-err/output.js @@ -0,0 +1,78 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 875: +/***/ ((module) => { + +function webpackEmptyContext(req) { + var e = new Error("Cannot find module '" + req + "'"); + e.code = 'MODULE_NOT_FOUND'; + throw e; +} +webpackEmptyContext.keys = () => ([]); +webpackEmptyContext.resolve = webpackEmptyContext; +webpackEmptyContext.id = 875; +module.exports = webpackEmptyContext; + +/***/ }), + +/***/ 747: +/***/ ((module) => { + +"use strict"; +module.exports = require("fs"); + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __nccwpck_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ var threw = true; +/******/ try { +/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__); +/******/ threw = false; +/******/ } finally { +/******/ if(threw) delete __webpack_module_cache__[moduleId]; +/******/ } +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* webpack/runtime/compat */ +/******/ +/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/ +var __webpack_exports__ = {}; +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +if (global.GENTLY) __nccwpck_require__(875) = GENTLY.hijack(eval("require")); +console.log(__nccwpck_require__(747)); + +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file