From a7233db64a4bb2a41261c406229604c0c9329535 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 29 Apr 2021 15:38:09 +0200 Subject: [PATCH 1/3] fixup terser error handling --- 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 | 77 +++++++++++++++++++++++ test/unit/minify-err/output.js | 82 +++++++++++++++++++++++++ 5 files changed, 186 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..8379bea8 --- /dev/null +++ b/test/unit/minify-err/output-coverage.js @@ -0,0 +1,77 @@ +/******/ (() => { // 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 +/******/ if(__webpack_module_cache__[moduleId]) { +/******/ return __webpack_module_cache__[moduleId].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__; +/******/ })() +; diff --git a/test/unit/minify-err/output.js b/test/unit/minify-err/output.js new file mode 100644 index 00000000..b1a48dbe --- /dev/null +++ b/test/unit/minify-err/output.js @@ -0,0 +1,82 @@ +module.exports = +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 212: +/***/ ((__unused_webpack_module, __unused_webpack_exports, __nccwpck_require__) => { + +if (global.GENTLY) __nccwpck_require__(875) = GENTLY.hijack(eval("require")); +console.log(__nccwpck_require__(747)); + + +/***/ }), + +/***/ 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 +/******/ if(__webpack_module_cache__[moduleId]) { +/******/ return __webpack_module_cache__[moduleId].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 */ +/******/ +/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/ +/******/ // module exports must be returned from runtime so entry inlining is disabled +/******/ // startup +/******/ // Load entry module and return exports +/******/ return __nccwpck_require__(212); +/******/ })() +; \ No newline at end of file From 80aa076af8dd0188ad5091ddb3e55cef04345b1c Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 29 Apr 2021 16:07:11 +0200 Subject: [PATCH 2/3] update coverage fixture --- test/unit/minify-err/output-coverage.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/unit/minify-err/output-coverage.js b/test/unit/minify-err/output-coverage.js index 8379bea8..6b2c53c4 100644 --- a/test/unit/minify-err/output-coverage.js +++ b/test/unit/minify-err/output-coverage.js @@ -32,8 +32,9 @@ module.exports = require("fs"); /******/ // The require function /******/ function __nccwpck_require__(moduleId) { /******/ // Check if module is in cache -/******/ if(__webpack_module_cache__[moduleId]) { -/******/ return __webpack_module_cache__[moduleId].exports; +/******/ 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] = { @@ -74,4 +75,4 @@ console.log(__nccwpck_require__(747)); module.exports = __webpack_exports__; /******/ })() -; +; \ No newline at end of file From cee6cad7b173c32f6fa4a99819164c9c8790e730 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 29 Apr 2021 16:30:50 +0200 Subject: [PATCH 3/3] fixup tests --- test/unit/minify-err/output.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/test/unit/minify-err/output.js b/test/unit/minify-err/output.js index b1a48dbe..6b2c53c4 100644 --- a/test/unit/minify-err/output.js +++ b/test/unit/minify-err/output.js @@ -1,16 +1,6 @@ -module.exports = /******/ (() => { // webpackBootstrap /******/ var __webpack_modules__ = ({ -/***/ 212: -/***/ ((__unused_webpack_module, __unused_webpack_exports, __nccwpck_require__) => { - -if (global.GENTLY) __nccwpck_require__(875) = GENTLY.hijack(eval("require")); -console.log(__nccwpck_require__(747)); - - -/***/ }), - /***/ 875: /***/ ((module) => { @@ -19,7 +9,7 @@ function webpackEmptyContext(req) { e.code = 'MODULE_NOT_FOUND'; throw e; } -webpackEmptyContext.keys = () => []; +webpackEmptyContext.keys = () => ([]); webpackEmptyContext.resolve = webpackEmptyContext; webpackEmptyContext.id = 875; module.exports = webpackEmptyContext; @@ -42,8 +32,9 @@ module.exports = require("fs"); /******/ // The require function /******/ function __nccwpck_require__(moduleId) { /******/ // Check if module is in cache -/******/ if(__webpack_module_cache__[moduleId]) { -/******/ return __webpack_module_cache__[moduleId].exports; +/******/ 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] = { @@ -68,15 +59,20 @@ module.exports = require("fs"); /************************************************************************/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { -/******/ __nccwpck_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop) +/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/compat */ /******/ -/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/ -/******/ // module exports must be returned from runtime so entry inlining is disabled -/******/ // startup -/******/ // Load entry module and return exports -/******/ return __nccwpck_require__(212); +/******/ 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