From 846cc5a7ce2fb4b0b299bec8891ab5a5f17f251e Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Tue, 7 Aug 2018 18:34:21 +0530 Subject: [PATCH] feat(migrate): CCP to SCP - fix tests --- .../commonsChunkPlugin.test.js.snap | 36 +++-- .../commonsChunkPlugin-0.input.js | 10 +- .../commonsChunkPlugin-1.input.js | 10 +- .../commonsChunkPlugin-2.input.js | 10 +- .../commonsChunkPlugin-3.input.js | 6 +- .../commonsChunkPlugin-4.input.js | 12 +- .../commonsChunkPlugin-5.input.js | 6 +- .../commonsChunkPlugin-6.input.js | 6 +- .../commonsChunkPlugin-7.input.js | 11 +- .../commonsChunkPlugin.test.js | 2 +- .../commonsChunkPlugin/commonsChunkPlugin.ts | 126 +++++++++--------- 11 files changed, 117 insertions(+), 118 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index bd55f4cb6da..8d59226331e 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -8,15 +8,15 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da cacheGroups: { common: { + name: 'common', chunks: 'initial', - enforce: true, - name: 'common' + enforce: true }, vendor: { + name: 'vendor', chunks: 'initial', enforce: true, - name: 'vendor', test: '/node_modules/' } } @@ -32,15 +32,15 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da splitChunks: { cacheGroups: { common: { + name: 'common', chunks: 'initial', - enforce: true, - name: 'common' + enforce: true }, vendor: { + name: 'vendor', chunks: 'initial', enforce: true, - name: 'vendor', test: '/node_modules/' } } @@ -56,9 +56,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da splitChunks: { cacheGroups: { vendor: { + name: 'vendor', chunks: 'initial', enforce: true, - name: 'vendor', test: '/node_modules/' } } @@ -74,9 +74,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da splitChunks: { cacheGroups: { commons: { + name: 'commons', chunks: 'initial', - enforce: true, - name: 'commons' + enforce: true } } } @@ -95,8 +95,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da cacheGroups: { main: { - chunks: 'initial', - enforce: true, name: 'main' } } @@ -112,9 +110,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" da splitChunks: { cacheGroups: { main: { + name: 'main', chunks: 'initial', - enforce: true, - name: 'main' + enforce: true } } }, @@ -131,15 +129,15 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6" da splitChunks: { cacheGroups: { main: { + name: 'main', chunks: 'initial', - enforce: true, - name: 'main' + enforce: true }, name: { + name: 'name', chunks: 'initial', enforce: true, - name: 'name', test: ({ resource }) => /node_modules/.test(resource), test: ({ resource }) => { @@ -178,15 +176,15 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da cacheGroups: { main: { + name: 'main', chunks: 'initial', - enforce: true, - name: 'main' + enforce: true }, vendor: { + name: 'vendor', chunks: 'initial', enforce: true, - name: 'vendor', test: '/node_modules/' } } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js index 8c607b8bf07..f66ded553e9 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js @@ -1,8 +1,8 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - names: ["common", "vendor"], + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + names: ["common", "vendor"], minChunks: 2 - }) - ] + }) + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js index deb6b344542..63202321e4f 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js @@ -1,7 +1,7 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - names: ["common", "vendor"] - }) - ] + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + names: ["common", "vendor"] + }) + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js index 110e4232bf0..783778fc416 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js @@ -1,7 +1,7 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - names: ["vendor"] - }) - ] + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + names: ["vendor"] + }) + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js index 49b19719fbe..b6852ef4efc 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js @@ -1,8 +1,8 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ filename: "commons.js", name: "commons" }) - ] + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js index 5f28279038c..48428ca18c4 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js @@ -1,10 +1,10 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ name: "main", - async: true, - minSize: 0, - minChunks: 2 + async: true, + minSize: 0, + minChunks: 2 }) - ] + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js index aa218de9a95..9b1ab0e7592 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js @@ -1,7 +1,7 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ names: ["main", "runtime"], }) - ] + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js index 2a5988ee20f..7554f9eb8ef 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js @@ -1,6 +1,6 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ name: "main", minChunks: ({ resource }) => /node_modules/.test(resource), minChunks: ({ resource }) => { @@ -22,5 +22,5 @@ module.export = { } } }) - ] + ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js index eb7578de976..6ebf70ef678 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js @@ -1,11 +1,8 @@ module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ names: ["main", "vendor"], minChunks: Infinity, - }), - // new webpack.optimize.CommonsChunkPlugin({ - // name: "vendor", - // }), - ] + }) + ] } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js index 9d3e3a96d0a..f1d540e76a1 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -6,7 +6,7 @@ defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-0"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-1"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-2"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-3"); -defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); // todo fix async case +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-5"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-7"); diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index 1d113e1b18a..de87373080c 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -36,13 +36,12 @@ export default function(j: IJSCodeshift, ast: INode): INode { return ast; } - // iterate each CommonsChunkPlugin instance - // cache group options based on keys const cacheGroup: object = {}; - const cacheGroups: INode[] = []; + let isAsyncChunk: boolean = false; + // iterate each CommonsChunkPlugin instance CommonsChunkPlugin.forEach( (path: INode): void => { const CCPProps = path.value.arguments[0].properties; @@ -56,82 +55,87 @@ export default function(j: IJSCodeshift, ast: INode): INode { switch (propKey) { case "names": - p.value.elements.forEach((chunkName) => { - if (chunkName.value === "runtime") { - optimizationProps["runtimeChunk"] = createIdentifierOrLiteral( // tslint:disable-line - j, - true, - ); - } else { - if (!Array.isArray(cacheGroup[chunkName.value])) { - cacheGroup[chunkName.value] = []; - } - - if (chunkName.value === "vendor") { - cacheGroup[chunkName.value].push( - createProperty(j, "test", "/node_modules/"), + p.value.elements.forEach((chunkName) => { + if (chunkName.value === "runtime") { + optimizationProps["runtimeChunk"] = createIdentifierOrLiteral( // tslint:disable-line + j, + true, ); + } else { + if (!Array.isArray(cacheGroup[chunkName.value])) { + cacheGroup[chunkName.value] = []; + } + + if (chunkName.value === "vendor") { + cacheGroup[chunkName.value].push( + createProperty(j, "test", "/node_modules/"), + ); + } } - } - }); - break; + }); + break; + case "name": - chunkKey = propKey; - if (!Array.isArray(cacheGroup[p.value.value])) { - cacheGroup[p.value.value] = []; - } - if (p.value.value === "vendor") { - cacheGroup[p.value.value].push( - createProperty(j, "test", "/node_modules/"), - ); - } - - break; + chunkKey = propKey; + if (!Array.isArray(cacheGroup[p.value.value])) { + cacheGroup[p.value.value] = []; + } + if (p.value.value === "vendor") { + cacheGroup[p.value.value].push( + createProperty(j, "test", "/node_modules/"), + ); + } + + break; + case "async": - splitChunksProps.push(createProperty(j, "chunks", "async")); - break; + splitChunksProps.push(createProperty(j, "chunks", "async")); + isAsyncChunk = true; + break; + case "minSize": case "minChunks": - const { value: pathValue } = p; - - // minChunk is a function - if ( - pathValue.type === "ArrowFunctionExpression" || - pathValue.type === "FunctionExpression" - ) { - if (!Array.isArray(cacheGroup[chunkKey])) { - cacheGroup[chunkKey] = []; + const { value: pathValue } = p; + + // minChunk is a function + if ( + pathValue.type === "ArrowFunctionExpression" || + pathValue.type === "FunctionExpression" + ) { + if (!Array.isArray(cacheGroup[chunkKey])) { + cacheGroup[chunkKey] = []; + } + cacheGroup[chunkKey].push( + j.property( + "init", + createIdentifierOrLiteral(j, "test"), + pathValue, + ), + ); + break; } - cacheGroup[chunkKey].push( - j.property( - "init", - createIdentifierOrLiteral(j, "test"), - pathValue, - ), - ); - break; - } - let propValue; + let propValue; - if (pathValue.name === "Infinity") { - propValue = Infinity; - } else { - propValue = pathValue.value; - } + if (pathValue.name === "Infinity") { + propValue = Infinity; + } else { + propValue = pathValue.value; + } - splitChunksProps.push(createProperty(j, p.key.name, propValue)); - break; + splitChunksProps.push(createProperty(j, p.key.name, propValue)); + break; } }, ); Object.keys(cacheGroup).forEach((chunkName) => { - const chunkProps = [ - ...commonCacheGroupsProps, + const chunkProps: INode[] = [ createProperty(j, "name", chunkName), ]; + if (!isAsyncChunk) { chunkProps.push(...commonCacheGroupsProps); } + if ( cacheGroup[chunkName] && Array.isArray(cacheGroup[chunkName]) &&