From af13334f6379ac53c4bb608c8e5d302fd5ad4e31 Mon Sep 17 00:00:00 2001 From: Kyle Truong Date: Mon, 19 Jun 2017 21:18:17 -0400 Subject: [PATCH 1/4] Issue #4099: ES6 Refactor for depdendencies/DepBlockHelpers.js --- lib/dependencies/DepBlockHelpers.js | 32 +++++++++++------------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/dependencies/DepBlockHelpers.js b/lib/dependencies/DepBlockHelpers.js index 9a89c6780a5..b6228bfc3d6 100644 --- a/lib/dependencies/DepBlockHelpers.js +++ b/lib/dependencies/DepBlockHelpers.js @@ -2,35 +2,27 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -var DepBlockHelpers = exports; +"use strict"; -DepBlockHelpers.getLoadDepBlockWrapper = function(depBlock, outputOptions, requestShortener, name) { - var promiseCode = DepBlockHelpers.getDepBlockPromise(depBlock, outputOptions, requestShortener, name); +const DepBlockHelpers = exports; + +DepBlockHelpers.getLoadDepBlockWrapper = (depBlock, outputOptions, requestShortener, name) => { + const promiseCode = DepBlockHelpers.getDepBlockPromise(depBlock, outputOptions, requestShortener, name); return [ - promiseCode + ".then(", + `${promiseCode}.then(`, ").catch(", ")" ]; }; -DepBlockHelpers.getDepBlockPromise = function(depBlock, outputOptions, requestShortener, name) { +DepBlockHelpers.getDepBlockPromise = (depBlock, outputOptions, requestShortener, name) => { if(depBlock.chunks) { - var chunks = depBlock.chunks.filter(function(chunk) { - return !chunk.hasRuntime() && chunk.id !== null; - }); + const chunks = depBlock.chunks.filter(chunk => !chunk.hasRuntime() && chunk.id !== null); if(chunks.length === 1) { - var chunk = chunks[0]; - return "__webpack_require__.e" + asComment(name) + "(" + JSON.stringify(chunk.id) + "" + - (outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : "") + - asComment(depBlock.chunkReason) + ")"; + const chunk = chunks[0]; + return `__webpack_require__.e${asComment(name)}(${JSON.stringify(chunk.id)}${outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : ""}${asComment(depBlock.chunkReason)})`; } else if(chunks.length > 0) { - return "Promise.all" + asComment(name) + "(" + - (outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : "") + - "[" + - chunks.map(function(chunk) { - return "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")"; - }).join(", ") + - "])"; + return `Promise.all${asComment(name)}(${outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : ""}[${chunks.map(chunk => "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")").join(", ")}])`; } } return "new Promise(function(resolve) { resolve(); })"; @@ -38,5 +30,5 @@ DepBlockHelpers.getDepBlockPromise = function(depBlock, outputOptions, requestSh function asComment(str) { if(!str) return ""; - return "/* " + str + " */"; + return `/* ${str} */`; } From be6a710a2b94ebd6a0bc3e800dc7a4f6f55d6d2e Mon Sep 17 00:00:00 2001 From: Kyle Truong Date: Mon, 19 Jun 2017 21:45:00 -0400 Subject: [PATCH 2/4] no message --- lib/dependencies/DepBlockHelpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dependencies/DepBlockHelpers.js b/lib/dependencies/DepBlockHelpers.js index b6228bfc3d6..57739851075 100644 --- a/lib/dependencies/DepBlockHelpers.js +++ b/lib/dependencies/DepBlockHelpers.js @@ -9,7 +9,7 @@ const DepBlockHelpers = exports; DepBlockHelpers.getLoadDepBlockWrapper = (depBlock, outputOptions, requestShortener, name) => { const promiseCode = DepBlockHelpers.getDepBlockPromise(depBlock, outputOptions, requestShortener, name); return [ - `${promiseCode}.then(`, + promiseCode + ".then(", ").catch(", ")" ]; From 6549f15f3e23b058d2e6f77739200a55109a1273 Mon Sep 17 00:00:00 2001 From: Kyle Truong Date: Sat, 1 Jul 2017 09:53:16 -0400 Subject: [PATCH 3/4] - Move some expressions out from string template into separate variables to improve readability --- lib/dependencies/DepBlockHelpers.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/dependencies/DepBlockHelpers.js b/lib/dependencies/DepBlockHelpers.js index 57739851075..ebf977fe94f 100644 --- a/lib/dependencies/DepBlockHelpers.js +++ b/lib/dependencies/DepBlockHelpers.js @@ -18,11 +18,14 @@ DepBlockHelpers.getLoadDepBlockWrapper = (depBlock, outputOptions, requestShorte DepBlockHelpers.getDepBlockPromise = (depBlock, outputOptions, requestShortener, name) => { if(depBlock.chunks) { const chunks = depBlock.chunks.filter(chunk => !chunk.hasRuntime() && chunk.id !== null); + const pathAndChunkCheck = outputOptions.pathinfo && depBlock.chunkName; + const shortenedChunkName = requestShortener.shorten(depBlock.chunkName); + const chunkReason = asComment(depBlock.chunkReason); if(chunks.length === 1) { - const chunk = chunks[0]; - return `__webpack_require__.e${asComment(name)}(${JSON.stringify(chunk.id)}${outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : ""}${asComment(depBlock.chunkReason)})`; + const chunkId = JSON.stringify(chunks[0].id); + return `__webpack_require__.e${asComment(name)}(${chunkId}${pathAndChunkCheck ? "/*! " + shortenedChunkName + " */" : ""}${chunkReason})`; } else if(chunks.length > 0) { - return `Promise.all${asComment(name)}(${outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : ""}[${chunks.map(chunk => "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")").join(", ")}])`; + return `Promise.all${asComment(name)}(${pathAndChunkCheck ? "/*! " + shortenedChunkName + " */" : ""}[${chunks.map(chunk => "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")").join(", ")}])`; } } return "new Promise(function(resolve) { resolve(); })"; From 98951d17829cbc5dfd15a7a1aa5136532f7b1c4d Mon Sep 17 00:00:00 2001 From: Kyle Truong Date: Sat, 1 Jul 2017 10:12:11 -0400 Subject: [PATCH 4/4] - Move more expressions out to improve readability as discussed in PR review --- lib/dependencies/DepBlockHelpers.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/dependencies/DepBlockHelpers.js b/lib/dependencies/DepBlockHelpers.js index ebf977fe94f..9f099b16aec 100644 --- a/lib/dependencies/DepBlockHelpers.js +++ b/lib/dependencies/DepBlockHelpers.js @@ -18,14 +18,16 @@ DepBlockHelpers.getLoadDepBlockWrapper = (depBlock, outputOptions, requestShorte DepBlockHelpers.getDepBlockPromise = (depBlock, outputOptions, requestShortener, name) => { if(depBlock.chunks) { const chunks = depBlock.chunks.filter(chunk => !chunk.hasRuntime() && chunk.id !== null); - const pathAndChunkCheck = outputOptions.pathinfo && depBlock.chunkName; - const shortenedChunkName = requestShortener.shorten(depBlock.chunkName); + const pathChunkCheck = outputOptions.pathinfo && depBlock.chunkName; + const shortChunkName = requestShortener.shorten(depBlock.chunkName); const chunkReason = asComment(depBlock.chunkReason); + const requireChunkId = chunk => "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")"; + name = asComment(name); if(chunks.length === 1) { const chunkId = JSON.stringify(chunks[0].id); - return `__webpack_require__.e${asComment(name)}(${chunkId}${pathAndChunkCheck ? "/*! " + shortenedChunkName + " */" : ""}${chunkReason})`; + return `__webpack_require__.e${name}(${chunkId}${pathChunkCheck ? "/*! " + shortChunkName + " */" : ""}${chunkReason})`; } else if(chunks.length > 0) { - return `Promise.all${asComment(name)}(${pathAndChunkCheck ? "/*! " + shortenedChunkName + " */" : ""}[${chunks.map(chunk => "__webpack_require__.e(" + JSON.stringify(chunk.id) + ")").join(", ")}])`; + return `Promise.all${name}(${pathChunkCheck ? "/*! " + shortChunkName + " */" : ""}[${chunks.map(requireChunkId).join(", ")}])`; } } return "new Promise(function(resolve) { resolve(); })";