Skip to content

Commit

Permalink
fix: handle terser error (#703)
Browse files Browse the repository at this point in the history
With the Terser upgrade in #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 #697.
  • Loading branch information
guybedford authored Apr 29, 2021
1 parent 6bdb3e2 commit 571eced
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/unit/minify-err/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (global.GENTLY) require = GENTLY.hijack(__non_webpack_require__);
console.log(require('fs'));
3 changes: 3 additions & 0 deletions test/unit/minify-err/opt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minify": true
}
78 changes: 78 additions & 0 deletions test/unit/minify-err/output-coverage.js
Original file line number Diff line number Diff line change
@@ -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__;
/******/ })()
;
78 changes: 78 additions & 0 deletions test/unit/minify-err/output.js
Original file line number Diff line number Diff line change
@@ -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__;
/******/ })()
;

0 comments on commit 571eced

Please sign in to comment.