Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Oct 4, 2021
1 parent e1ab8d0 commit c0f1417
Show file tree
Hide file tree
Showing 9 changed files with 2,684 additions and 6,314 deletions.
8,804 changes: 2,597 additions & 6,207 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"cross-env": "^7.0.3",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"esbuild": "^0.12.23",
"esbuild": "^0.13.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
Expand Down
51 changes: 16 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class TerserPlugin {
* @private
* @param {Error | string} warning
* @param {string} file
* @returns {WebpackError}
* @returns {Error}
*/
static buildWarning(warning, file) {
/**
Expand All @@ -219,31 +219,28 @@ class TerserPlugin {
builtWarning.hideStack = true;
builtWarning.file = file;

// @ts-ignore
return builtWarning;
}

/**
* @private
* @param {any} error
* @param {string} file
* @param {Compilation["requestShortener"]} [requestShortener]
* @param {SourceMapConsumer} [sourceMap]
* @returns {WebpackError}
* @param {Compilation["requestShortener"]} [requestShortener]
* @returns {Error}
*/
static buildError(error, file, requestShortener, sourceMap) {
static buildError(error, file, sourceMap, requestShortener) {
/**
* @type {Error & { file: string }}
*/
// @ts-ignore
let builtError;

if (typeof error === "string") {
// @ts-ignore
builtError = new Error(`${file} from Terser plugin\n${error}`);
builtError.file = file;

// @ts-ignore
return builtError;
}

Expand All @@ -270,7 +267,6 @@ class TerserPlugin {
);
builtError.file = file;

// @ts-ignore
return builtError;
}

Expand All @@ -284,7 +280,6 @@ class TerserPlugin {
);
builtError.file = file;

// @ts-ignore
return builtError;
}

Expand All @@ -297,15 +292,13 @@ class TerserPlugin {
);
builtError.file = file;

// @ts-ignore
return builtError;
}

// @ts-ignore
builtError = new Error(`${file} from Terser plugin\n${error.message}`);
builtError.file = file;

// @ts-ignore
return builtError;
}

Expand Down Expand Up @@ -511,14 +504,14 @@ class TerserPlugin {
TerserPlugin.buildError(
error,
name,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined,
hasSourceMap
? new SourceMapConsumer(
/** @type {RawSourceMap} */ (inputSourceMap)
)
: // eslint-disable-next-line no-undefined
undefined
undefined,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined
)
)
);
Expand Down Expand Up @@ -560,14 +553,14 @@ class TerserPlugin {
TerserPlugin.buildError(
item,
name,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined,
hasSourceMap
? new SourceMapConsumer(
/** @type {RawSourceMap} */ (inputSourceMap)
)
: // eslint-disable-next-line no-undefined
undefined
undefined,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined
)
);
}
Expand Down Expand Up @@ -677,27 +670,15 @@ class TerserPlugin {
}

if (output.warnings && output.warnings.length > 0) {
output.warnings.forEach(
/**
* @param {Error} warning
*/
(warning) => {
compilation.warnings.push(
/** @type {WebpackError} */ (warning)
);
}
);
for (const warning of output.warnings) {
compilation.warnings.push(/** @type {WebpackError} */ (warning));
}
}

if (output.errors && output.errors.length > 0) {
output.errors.forEach(
/**
* @param {Error & { filename?: string }} error
*/
(error) => {
compilation.errors.push(/** @type {WebpackError} */ (error));
}
);
for (const error of output.errors) {
compilation.errors.push(/** @type {WebpackError} */ (error));
}
}

/** @type {Record<string, any>} */
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,10 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
const esbuild = require("esbuild");

// Copy `swc` options
// Copy `esbuild` options
const esbuildOptions = buildEsbuildOptions(minimizerOptions);

// Let `swc` generate a SourceMap
// Let `esbuild` generate a SourceMap
if (sourceMap) {
esbuildOptions.sourcemap = true;
esbuildOptions.sourcesContent = false;
Expand Down
8 changes: 4 additions & 4 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ describe("TerserPlugin", () => {
TerserPlugin.buildError(
errorWithLineAndCol,
"test.js",
new SourceMapConsumer(rawSourceMap),
// eslint-disable-next-line no-undefined
undefined,
new SourceMapConsumer(rawSourceMap)
undefined
)
).toMatchSnapshot();

Expand All @@ -570,8 +570,8 @@ describe("TerserPlugin", () => {
TerserPlugin.buildError(
otherErrorWithLineAndCol,
"test.js",
new RequestShortener("/example.com/www/js/"),
new SourceMapConsumer(rawSourceMap)
new SourceMapConsumer(rawSourceMap),
new RequestShortener("/example.com/www/js/")
)
).toMatchSnapshot();

Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/TerserPlugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ exports[`TerserPlugin should work and do not use memory cache when the "cache" o

exports[`TerserPlugin should work and generate real content hash: assets 1`] = `
Object {
"598.fcfa82506be0724e60bc.f6be1154613dd88c2562.dd68119a1a0ad8266e25.js": "\\"use strict\\";(self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[]).push([[598],{598:(e,s,p)=>{p.r(s),p.d(s,{default:()=>c});const c=\\"async-dep\\"}}]);",
"app.6d205c9362364a8593a3.fbed6865b6f887a0cdf2.dd68119a1a0ad8266e25.js": "(()=>{\\"use strict\\";var e,r,t={},o={};function n(e){var r=o[e];if(void 0!==r)return r.exports;var a=o[e]={exports:{}};return t[e](a,a.exports,n),a.exports}n.m=t,n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.f={},n.e=e=>Promise.all(Object.keys(n.f).reduce(((r,t)=>(n.f[t](e,r),r)),[])),n.u=e=>e+\\".fcfa82506be0724e60bc.f6be1154613dd88c2562.\\"+n.h()+\\".js\\",n.h=()=>\\"dd68119a1a0ad8266e25\\",n.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(e){if(\\"object\\"==typeof window)return window}}(),n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r=\\"terser-webpack-plugin:\\",n.l=(t,o,a,i)=>{if(e[t])e[t].push(o);else{var l,c;if(void 0!==a)for(var u=document.getElementsByTagName(\\"script\\"),s=0;s<u.length;s++){var d=u[s];if(d.getAttribute(\\"src\\")==t||d.getAttribute(\\"data-webpack\\")==r+a){l=d;break}}l||(c=!0,(l=document.createElement(\\"script\\")).charset=\\"utf-8\\",l.timeout=120,n.nc&&l.setAttribute(\\"nonce\\",n.nc),l.setAttribute(\\"data-webpack\\",r+a),l.src=t),e[t]=[o];var p=(r,o)=>{l.onerror=l.onload=null,clearTimeout(f);var n=e[t];if(delete e[t],l.parentNode&&l.parentNode.removeChild(l),n&&n.forEach((e=>e(o))),r)return r(o)},f=setTimeout(p.bind(null,void 0,{type:\\"timeout\\",target:l}),12e4);l.onerror=p.bind(null,l.onerror),l.onload=p.bind(null,l.onload),c&&document.head.appendChild(l)}},n.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},(()=>{var e;n.g.importScripts&&(e=n.g.location+\\"\\");var r=n.g.document;if(!e&&r&&(r.currentScript&&(e=r.currentScript.src),!e)){var t=r.getElementsByTagName(\\"script\\");t.length&&(e=t[t.length-1].src)}if(!e)throw new Error(\\"Automatic publicPath is not supported in this browser\\");e=e.replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),n.p=e})(),(()=>{var e={143:0};n.f.j=(r,t)=>{var o=n.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var a=new Promise(((t,n)=>o=e[r]=[t,n]));t.push(o[2]=a);var i=n.p+n.u(r),l=new Error;n.l(i,(t=>{if(n.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var a=t&&(\\"load\\"===t.type?\\"missing\\":t.type),i=t&&t.target&&t.target.src;l.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+a+\\": \\"+i+\\")\\",l.name=\\"ChunkLoadError\\",l.type=a,l.request=i,o[1](l)}}),\\"chunk-\\"+r,r)}};var r=(r,t)=>{var o,a,[i,l,c]=t,u=0;if(i.some((r=>0!==e[r]))){for(o in l)n.o(l,o)&&(n.m[o]=l[o]);if(c)c(n)}for(r&&r(t);u<i.length;u++)a=i[u],n.o(e,a)&&e[a]&&e[a][0](),e[i[u]]=0},t=self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[];t.forEach(r.bind(null,0)),t.push=r.bind(null,t.push.bind(t))})(),n.e(598).then(n.bind(n,598)).then((()=>{console.log(\\"Good\\")}))})();",
"598.fcfa82506be0724e60bc.f13c7f3e6c32f16994e6.c3b6af56e534fdf71cbe.js": "\\"use strict\\";(self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[]).push([[598],{598:(e,s,p)=>{p.r(s),p.d(s,{default:()=>c});const c=\\"async-dep\\"}}]);",
"app.dfb0a582489b50c67225.be47805a15bc806a2c40.c3b6af56e534fdf71cbe.js": "(()=>{\\"use strict\\";var e,r,t={},o={};function n(e){var r=o[e];if(void 0!==r)return r.exports;var i=o[e]={exports:{}};return t[e](i,i.exports,n),i.exports}n.m=t,n.d=(e,r)=>{for(var t in r)n.o(r,t)&&!n.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},n.f={},n.e=e=>Promise.all(Object.keys(n.f).reduce(((r,t)=>(n.f[t](e,r),r)),[])),n.u=e=>e+\\".fcfa82506be0724e60bc.f13c7f3e6c32f16994e6.\\"+n.h()+\\".js\\",n.h=()=>\\"c3b6af56e534fdf71cbe\\",n.g=function(){if(\\"object\\"==typeof globalThis)return globalThis;try{return this||new Function(\\"return this\\")()}catch(e){if(\\"object\\"==typeof window)return window}}(),n.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),e={},r=\\"terser-webpack-plugin:\\",n.l=(t,o,i,a)=>{if(e[t])e[t].push(o);else{var c,l;if(void 0!==i)for(var u=document.getElementsByTagName(\\"script\\"),s=0;s<u.length;s++){var p=u[s];if(p.getAttribute(\\"src\\")==t||p.getAttribute(\\"data-webpack\\")==r+i){c=p;break}}c||(l=!0,(c=document.createElement(\\"script\\")).charset=\\"utf-8\\",c.timeout=120,n.nc&&c.setAttribute(\\"nonce\\",n.nc),c.setAttribute(\\"data-webpack\\",r+i),c.src=t),e[t]=[o];var d=(r,o)=>{c.onerror=c.onload=null,clearTimeout(f);var n=e[t];if(delete e[t],c.parentNode&&c.parentNode.removeChild(c),n&&n.forEach((e=>e(o))),r)return r(o)},f=setTimeout(d.bind(null,void 0,{type:\\"timeout\\",target:c}),12e4);c.onerror=d.bind(null,c.onerror),c.onload=d.bind(null,c.onload),l&&document.head.appendChild(c)}},n.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},(()=>{var e;n.g.importScripts&&(e=n.g.location+\\"\\");var r=n.g.document;if(!e&&r&&(r.currentScript&&(e=r.currentScript.src),!e)){var t=r.getElementsByTagName(\\"script\\");t.length&&(e=t[t.length-1].src)}if(!e)throw new Error(\\"Automatic publicPath is not supported in this browser\\");e=e.replace(/#.*$/,\\"\\").replace(/\\\\?.*$/,\\"\\").replace(/\\\\/[^\\\\/]+$/,\\"/\\"),n.p=e})(),(()=>{var e={143:0};n.f.j=(r,t)=>{var o=n.o(e,r)?e[r]:void 0;if(0!==o)if(o)t.push(o[2]);else{var i=new Promise(((t,n)=>o=e[r]=[t,n]));t.push(o[2]=i);var a=n.p+n.u(r),c=new Error;n.l(a,(t=>{if(n.o(e,r)&&(0!==(o=e[r])&&(e[r]=void 0),o)){var i=t&&(\\"load\\"===t.type?\\"missing\\":t.type),a=t&&t.target&&t.target.src;c.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+i+\\": \\"+a+\\")\\",c.name=\\"ChunkLoadError\\",c.type=i,c.request=a,o[1](c)}}),\\"chunk-\\"+r,r)}};var r=(r,t)=>{var o,i,[a,c,l]=t,u=0;if(a.some((r=>0!==e[r]))){for(o in c)n.o(c,o)&&(n.m[o]=c[o]);if(l)l(n)}for(r&&r(t);u<a.length;u++)i=a[u],n.o(e,i)&&e[i]&&e[i][0](),e[a[u]]=0},t=self.webpackChunkterser_webpack_plugin=self.webpackChunkterser_webpack_plugin||[];t.forEach(r.bind(null,0)),t.push=r.bind(null,t.push.bind(t))})(),n.e(598).then(n.bind(n,598)).then((()=>{console.log(\\"Good\\")}))})();",
}
`;

Expand Down
Loading

0 comments on commit c0f1417

Please sign in to comment.