From ecf8679fe2284f06ecac10516f992d1c3246dd39 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Wed, 1 Aug 2018 17:27:31 +0530 Subject: [PATCH 01/16] feat(migrate): CCP to SCP - init --- .../commonsChunkPlugin.test.js.snap | 99 ++++++++++++ .../commonsChunkPlugin-0.input.js | 8 + .../commonsChunkPlugin-1.input.js | 7 + .../commonsChunkPlugin-2.input.js | 7 + .../commonsChunkPlugin-3.input.js | 8 + .../commonsChunkPlugin-4.input.js | 10 ++ .../commonsChunkPlugin.test.js | 9 ++ .../commonsChunkPlugin/commonsChunkPlugin.ts | 147 ++++++++++++++++++ packages/migrate/migrate.ts | 3 + 9 files changed, 298 insertions(+) create mode 100644 packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js create mode 100644 packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js create mode 100644 packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap new file mode 100644 index 00000000000..cedf99e5fea --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -0,0 +1,99 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroup: { + common: { + name: 'common', + chunks: 'initial' + }, + + vendor: { + name: 'vendor', + test: '/node_modules/', + chunks: 'initial' + }, + + minChunks: 2 + } + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroup: { + common: { + name: 'common', + chunks: 'initial' + }, + + vendor: { + name: 'vendor', + test: '/node_modules/', + chunks: 'initial' + } + } + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroup: { + vendor: { + name: 'vendor', + test: '/node_modules/', + chunks: 'initial' + } + } + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroup: { + commons: { + name: 'commons', + chunks: 'initial' + } + } + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroup: { + main: { + name: 'main' + }, + + chunks: 'async', + minSize: 0, + minChunks: 2 + } + } + } +} +" +`; diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js new file mode 100644 index 00000000000..6905330e974 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js @@ -0,0 +1,8 @@ +module.export = { + plugins: [ + new webpack.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 new file mode 100644 index 00000000000..ceebadf21fd --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js @@ -0,0 +1,7 @@ +module.export = { + plugins: [ + new webpack.CommonsChunkPlugin({ + names: ["common", "vendor"] + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js new file mode 100644 index 00000000000..679a322ea73 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js @@ -0,0 +1,7 @@ +module.export = { + plugins: [ + new webpack.CommonsChunkPlugin({ + names: ["vendor"] + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js new file mode 100644 index 00000000000..ef43c3ce4a9 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js @@ -0,0 +1,8 @@ +module.export = { + plugins: [ + new webpack.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 new file mode 100644 index 00000000000..f992bd5a870 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js @@ -0,0 +1,10 @@ +module.export = { + plugins: [ + new webpack.CommonsChunkPlugin({ + name: "main", + async: true, + minSize: 0, + minChunks: 2 + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js new file mode 100644 index 00000000000..2107021b083 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -0,0 +1,9 @@ +"use strict"; + +const defineTest = require("@webpack-cli/utils/defineTest").default; + +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"); diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts new file mode 100644 index 00000000000..8fb24c54de0 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -0,0 +1,147 @@ +import { + addOrUpdateConfigObject, + createIdentifierOrLiteral, + createLiteral, + createProperty, + findAndRemovePluginByName, + findPluginsByName, +} from "@webpack-cli/utils/ast-utils"; + +import { IJSCodeshift, INode } from "../types/NodePath"; + +/** + * + * Transform for CommonsChunkPlugin. If found, removes the + * plugin and sets optimizations.namedModules to true + * + * @param {Object} j - jscodeshift top-level import + * @param {Node} ast - jscodeshift ast to transform + * @returns {Node} ast - jscodeshift ast + */ +export default function(j: IJSCodeshift, ast: INode): INode { + + let pluginProps: INode[]; + const cacheGroupsArray: INode[] = []; + + // find old options + const CommonsChunkPlugin = findPluginsByName(j, ast, ["webpack.CommonsChunkPlugin"]); + + if (!CommonsChunkPlugin.size()) { return ast; } + + CommonsChunkPlugin + .forEach((path: INode): void => { + pluginProps = path.value.arguments[0].properties; // any node + }); + + // create chunk cache group option + function createChunkCache(chunkName) { + const commonProperties: INode[] = [ + createProperty( + j, + "name", + chunkName.value, + ), + ]; + switch (chunkName.value) { + case "vendor": + return j.property( + "init", + createIdentifierOrLiteral(j, chunkName.value), + j.objectExpression([ + ...commonProperties, + createProperty( + j, + "test", + "/node_modules/", + ), + createProperty( + j, + "chunks", + "initial", + ), + ]), + ); + case "common": + case "commons": + return j.property( + "init", + createIdentifierOrLiteral(j, chunkName.value), + j.objectExpression([ + ...commonProperties, + createProperty( + j, + "chunks", + "initial", + ), + ]), + ); + default: + return j.property( + "init", + createIdentifierOrLiteral(j, chunkName.value), + j.objectExpression([ + ...commonProperties, + ]), + ); + } + } + + // iterate old props and map new props + pluginProps.forEach((p: INode): void => { + switch (p.key.name) { + case "names": + p.value.elements.forEach((chunkName) => { + cacheGroupsArray.push( + createChunkCache(chunkName), + ); + }); + break; + case "name": + cacheGroupsArray.push( + createChunkCache(p.value), + ); + break; + case "async": + cacheGroupsArray.push( + createProperty( + j, + "chunks", + "async", + ), + ); + break; + case "minSize": + case "minChunks": + cacheGroupsArray.push( + createProperty( + j, + p.key.name, + p.value.value, + ), + ); + break; + } + }); + + // Remove old plugin + const root: INode = findAndRemovePluginByName(j, ast, "webpack.CommonsChunkPlugin"); + + // Add new optimizations splitChunks option + if (root) { + addOrUpdateConfigObject( + j, + root, + "optimizations", + "splitChunks", + j.objectExpression([ + j.property( + "init", + createIdentifierOrLiteral(j, "cacheGroup"), + j.objectExpression(cacheGroupsArray), + ), + ]), + ); + } + + return ast; +} diff --git a/packages/migrate/migrate.ts b/packages/migrate/migrate.ts index f93cded0c94..6d3ad4ec6f3 100644 --- a/packages/migrate/migrate.ts +++ b/packages/migrate/migrate.ts @@ -3,6 +3,7 @@ import pEachSeries = require("p-each-series"); import pLazy = require("p-lazy"); import bannerPluginTransform from "./bannerPlugin/bannerPlugin"; +import commonsChunkPluginTransform from "./commonsChunkPlugin/commonsChunkPlugin"; import extractTextPluginTransform from "./extractTextPlugin/extractTextPlugin"; import loaderOptionsPluginTransform from "./loaderOptionsPlugin/loaderOptionsPlugin"; import loadersTransform from "./loaders/loaders"; @@ -15,6 +16,7 @@ import uglifyJsPluginTransform from "./uglifyJsPlugin/uglifyJsPlugin"; interface ITransformsObject { bannerPluginTransform: object; + commonsChunkPluginTransform?: object; extractTextPluginTransform: object; loaderOptionsPluginTransform: object; loadersTransform: object; @@ -36,6 +38,7 @@ const transformsObject: ITransformsObject = { extractTextPluginTransform, noEmitOnErrorsPluginTransform, removeDeprecatedPluginsTransform, + commonsChunkPluginTransform, }; /* tslint:enable object-literal-sort-keys */ From 3d9d31c70d625a81262466a8e635011a46692514 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Fri, 3 Aug 2018 15:29:16 +0530 Subject: [PATCH 02/16] feat(migrate): CCP to SCP - runtime chunk --- .../commonsChunkPlugin.test.js.snap | 83 ++++++++++--------- .../commonsChunkPlugin-0.input.js | 2 +- .../commonsChunkPlugin-1.input.js | 2 +- .../commonsChunkPlugin-2.input.js | 2 +- .../commonsChunkPlugin-3.input.js | 2 +- .../commonsChunkPlugin-4.input.js | 2 +- .../commonsChunkPlugin-5.input.js | 7 ++ .../commonsChunkPlugin.test.js | 1 + .../commonsChunkPlugin/commonsChunkPlugin.ts | 68 ++++++++++----- 9 files changed, 102 insertions(+), 67 deletions(-) create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index cedf99e5fea..e10def8b3a0 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -4,20 +4,18 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da "module.export = { optimizations: { splitChunks: { - cacheGroup: { - common: { - name: 'common', - chunks: 'initial' - }, + common: { + name: 'common', + chunks: 'initial' + }, - vendor: { - name: 'vendor', - test: '/node_modules/', - chunks: 'initial' - }, + vendor: { + name: 'vendor', + test: '/node_modules/', + chunks: 'initial' + }, - minChunks: 2 - } + minChunks: 2 } } } @@ -28,17 +26,15 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da "module.export = { optimizations: { splitChunks: { - cacheGroup: { - common: { - name: 'common', - chunks: 'initial' - }, + common: { + name: 'common', + chunks: 'initial' + }, - vendor: { - name: 'vendor', - test: '/node_modules/', - chunks: 'initial' - } + vendor: { + name: 'vendor', + test: '/node_modules/', + chunks: 'initial' } } } @@ -50,12 +46,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da "module.export = { optimizations: { splitChunks: { - cacheGroup: { - vendor: { - name: 'vendor', - test: '/node_modules/', - chunks: 'initial' - } + vendor: { + name: 'vendor', + test: '/node_modules/', + chunks: 'initial' } } } @@ -67,11 +61,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da "module.export = { optimizations: { splitChunks: { - cacheGroup: { - commons: { - name: 'commons', - chunks: 'initial' - } + commons: { + name: 'commons', + chunks: 'initial' } } } @@ -83,15 +75,24 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da "module.export = { optimizations: { splitChunks: { - cacheGroup: { - main: { - name: 'main' - }, + main: { + name: 'main' + }, - chunks: 'async', - minSize: 0, - minChunks: 2 - } + chunks: 'async', + minSize: 0, + minChunks: 2 + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + runtimeChunk: true } } } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js index 6905330e974..8c607b8bf07 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js @@ -1,6 +1,6 @@ module.export = { plugins: [ - new webpack.CommonsChunkPlugin({ + 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 ceebadf21fd..deb6b344542 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js @@ -1,6 +1,6 @@ module.export = { plugins: [ - new webpack.CommonsChunkPlugin({ + 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 679a322ea73..110e4232bf0 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js @@ -1,6 +1,6 @@ module.export = { plugins: [ - new webpack.CommonsChunkPlugin({ + 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 ef43c3ce4a9..49b19719fbe 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js @@ -1,6 +1,6 @@ module.export = { plugins: [ - new webpack.CommonsChunkPlugin({ + 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 f992bd5a870..5f28279038c 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js @@ -1,6 +1,6 @@ module.export = { plugins: [ - new webpack.CommonsChunkPlugin({ + new webpack.optimize.CommonsChunkPlugin({ name: "main", async: true, minSize: 0, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js new file mode 100644 index 00000000000..d41a1db3046 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js @@ -0,0 +1,7 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + names: ["runtime"], + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js index 2107021b083..177fc091fbf 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -7,3 +7,4 @@ defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-1"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-2"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-3"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-5"); diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index 8fb24c54de0..59059aa444d 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -21,10 +21,11 @@ import { IJSCodeshift, INode } from "../types/NodePath"; export default function(j: IJSCodeshift, ast: INode): INode { let pluginProps: INode[]; - const cacheGroupsArray: INode[] = []; + const cacheGroupsProps: INode[] = []; + const splitChunksProps: INode[] = []; // find old options - const CommonsChunkPlugin = findPluginsByName(j, ast, ["webpack.CommonsChunkPlugin"]); + const CommonsChunkPlugin = findPluginsByName(j, ast, ["webpack.optimize.CommonsChunkPlugin"]); if (!CommonsChunkPlugin.size()) { return ast; } @@ -91,18 +92,28 @@ export default function(j: IJSCodeshift, ast: INode): INode { switch (p.key.name) { case "names": p.value.elements.forEach((chunkName) => { - cacheGroupsArray.push( - createChunkCache(chunkName), - ); + if (chunkName.value === "runtime") { + splitChunksProps.push( + createProperty( + j, + "runtimeChunk", + true, + ), + ); + } else { + cacheGroupsProps.push( + createChunkCache(chunkName), + ); + } }); break; case "name": - cacheGroupsArray.push( + cacheGroupsProps.push( createChunkCache(p.value), ); break; case "async": - cacheGroupsArray.push( + cacheGroupsProps.push( createProperty( j, "chunks", @@ -112,19 +123,38 @@ export default function(j: IJSCodeshift, ast: INode): INode { break; case "minSize": case "minChunks": - cacheGroupsArray.push( - createProperty( - j, - p.key.name, - p.value.value, - ), - ); - break; + const { value: pathValue } = p; + let propValue; + + if (pathValue.name === "Infinity") { + propValue = Infinity; + } else { + propValue = pathValue.value; + } + + cacheGroupsProps.push( + createProperty( + j, + p.key.name, + propValue, + ), + ); + break; } }); // Remove old plugin - const root: INode = findAndRemovePluginByName(j, ast, "webpack.CommonsChunkPlugin"); + const root: INode = findAndRemovePluginByName(j, ast, "webpack.optimize.CommonsChunkPlugin"); + + const rootProps = [ + ...splitChunksProps, + ]; + + if (cacheGroupsProps.length > 0) { + rootProps.push( + ...cacheGroupsProps, + ); + } // Add new optimizations splitChunks option if (root) { @@ -134,11 +164,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { "optimizations", "splitChunks", j.objectExpression([ - j.property( - "init", - createIdentifierOrLiteral(j, "cacheGroup"), - j.objectExpression(cacheGroupsArray), - ), + ...rootProps, ]), ); } From 303857b9b90fb3f1011cb357e9b78a9e79767f25 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Mon, 6 Aug 2018 19:16:01 +0530 Subject: [PATCH 03/16] feat(migrate): CCP to SCP - add enforce, chunk props --- .../commonsChunkPlugin.test.js.snap | 36 +++++--- .../commonsChunkPlugin/commonsChunkPlugin.ts | 87 ++++++++++--------- 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index e10def8b3a0..7b60c022070 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -5,14 +5,16 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da optimizations: { splitChunks: { common: { - name: 'common', - chunks: 'initial' + chunks: 'initial', + enforce: true, + name: 'common' }, vendor: { + chunks: 'initial', + enforce: true, name: 'vendor', - test: '/node_modules/', - chunks: 'initial' + test: '/node_modules/' }, minChunks: 2 @@ -27,14 +29,16 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da optimizations: { splitChunks: { common: { - name: 'common', - chunks: 'initial' + chunks: 'initial', + enforce: true, + name: 'common' }, vendor: { + chunks: 'initial', + enforce: true, name: 'vendor', - test: '/node_modules/', - chunks: 'initial' + test: '/node_modules/' } } } @@ -47,9 +51,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da optimizations: { splitChunks: { vendor: { + chunks: 'initial', + enforce: true, name: 'vendor', - test: '/node_modules/', - chunks: 'initial' + test: '/node_modules/' } } } @@ -62,8 +67,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da optimizations: { splitChunks: { commons: { - name: 'commons', - chunks: 'initial' + chunks: 'initial', + enforce: true, + name: 'commons' } } } @@ -76,10 +82,14 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da optimizations: { splitChunks: { main: { + chunks: 'initial', + enforce: true, name: 'main' }, - chunks: 'async', + chunks: 'initial', + enforce: true, + name: true, minSize: 0, minChunks: 2 } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index 59059aa444d..9bedb2e03e5 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -24,6 +24,19 @@ export default function(j: IJSCodeshift, ast: INode): INode { const cacheGroupsProps: INode[] = []; const splitChunksProps: INode[] = []; + const commonProperties: INode[] = [ + createProperty( + j, + "chunks", + "initial", + ), + createProperty( + j, + "enforce", + true, + ), + ]; + // find old options const CommonsChunkPlugin = findPluginsByName(j, ast, ["webpack.optimize.CommonsChunkPlugin"]); @@ -36,13 +49,6 @@ export default function(j: IJSCodeshift, ast: INode): INode { // create chunk cache group option function createChunkCache(chunkName) { - const commonProperties: INode[] = [ - createProperty( - j, - "name", - chunkName.value, - ), - ]; switch (chunkName.value) { case "vendor": return j.property( @@ -52,18 +58,20 @@ export default function(j: IJSCodeshift, ast: INode): INode { ...commonProperties, createProperty( j, - "test", - "/node_modules/", + "name", + chunkName.value, ), createProperty( j, - "chunks", - "initial", + "test", + "/node_modules/", ), ]), ); case "common": case "commons": + // TODO works as default for now + default: return j.property( "init", createIdentifierOrLiteral(j, chunkName.value), @@ -71,19 +79,11 @@ export default function(j: IJSCodeshift, ast: INode): INode { ...commonProperties, createProperty( j, - "chunks", - "initial", + "name", + chunkName.value, ), ]), ); - default: - return j.property( - "init", - createIdentifierOrLiteral(j, chunkName.value), - j.objectExpression([ - ...commonProperties, - ]), - ); } } @@ -114,50 +114,57 @@ export default function(j: IJSCodeshift, ast: INode): INode { break; case "async": cacheGroupsProps.push( + ...commonProperties, createProperty( j, - "chunks", - "async", + "name", + p.value.value, ), ); break; case "minSize": case "minChunks": const { value: pathValue } = p; - let propValue; - if (pathValue.name === "Infinity") { - propValue = Infinity; - } else { - propValue = pathValue.value; + // minChunk is a function + if (pathValue.type === "ArrowFunctionExpression" || pathValue.test === "FunctionExpression") { + break; } - cacheGroupsProps.push( - createProperty( - j, - p.key.name, - propValue, - ), - ); - break; + let propValue; + + if (pathValue.name === "Infinity") { + propValue = Infinity; + } else { + propValue = pathValue.value; + } + + cacheGroupsProps.push( + createProperty( + j, + p.key.name, + propValue, + ), + ); + break; } }); // Remove old plugin const root: INode = findAndRemovePluginByName(j, ast, "webpack.optimize.CommonsChunkPlugin"); - const rootProps = [ + const rootProps = [ ...splitChunksProps, ]; - if (cacheGroupsProps.length > 0) { + if (cacheGroupsProps.length > 0) { rootProps.push( ...cacheGroupsProps, ); } // Add new optimizations splitChunks option - if (root) { + if (root) { addOrUpdateConfigObject( j, root, @@ -169,5 +176,5 @@ export default function(j: IJSCodeshift, ast: INode): INode { ); } - return ast; + return ast; } From b4cdd03a81dce817a2e1c3ad0fac7218662d78bf Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Mon, 6 Aug 2018 19:29:28 +0530 Subject: [PATCH 04/16] feat(migrate): CCP to SCP - fix cacheGroups --- .../commonsChunkPlugin.test.js.snap | 91 +++++++++++-------- .../commonsChunkPlugin.test.js | 4 +- .../commonsChunkPlugin/commonsChunkPlugin.ts | 36 +++++--- 3 files changed, 75 insertions(+), 56 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index 7b60c022070..b2902bd8a29 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -4,20 +4,22 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da "module.export = { optimizations: { splitChunks: { - common: { - chunks: 'initial', - enforce: true, - name: 'common' - }, + minChunks: 2, - vendor: { - chunks: 'initial', - enforce: true, - name: 'vendor', - test: '/node_modules/' - }, + cacheGroups: { + common: { + chunks: 'initial', + enforce: true, + name: 'common' + }, - minChunks: 2 + vendor: { + chunks: 'initial', + enforce: true, + name: 'vendor', + test: '/node_modules/' + } + } } } } @@ -28,17 +30,19 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da "module.export = { optimizations: { splitChunks: { - common: { - chunks: 'initial', - enforce: true, - name: 'common' - }, + cacheGroups: { + common: { + chunks: 'initial', + enforce: true, + name: 'common' + }, - vendor: { - chunks: 'initial', - enforce: true, - name: 'vendor', - test: '/node_modules/' + vendor: { + chunks: 'initial', + enforce: true, + name: 'vendor', + test: '/node_modules/' + } } } } @@ -50,11 +54,13 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da "module.export = { optimizations: { splitChunks: { - vendor: { - chunks: 'initial', - enforce: true, - name: 'vendor', - test: '/node_modules/' + cacheGroups: { + vendor: { + chunks: 'initial', + enforce: true, + name: 'vendor', + test: '/node_modules/' + } } } } @@ -66,10 +72,12 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da "module.export = { optimizations: { splitChunks: { - commons: { - chunks: 'initial', - enforce: true, - name: 'commons' + cacheGroups: { + commons: { + chunks: 'initial', + enforce: true, + name: 'commons' + } } } } @@ -81,17 +89,20 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da "module.export = { optimizations: { splitChunks: { - main: { + minSize: 0, + minChunks: 2, + + cacheGroups: { + main: { + chunks: 'initial', + enforce: true, + name: 'main' + }, + chunks: 'initial', enforce: true, - name: 'main' - }, - - chunks: 'initial', - enforce: true, - name: true, - minSize: 0, - minChunks: 2 + name: true + } } } } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js index 177fc091fbf..793dd4bf51a 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -6,5 +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"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); // todo fix async case 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 9bedb2e03e5..5282d0138ff 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -49,18 +49,22 @@ export default function(j: IJSCodeshift, ast: INode): INode { // create chunk cache group option function createChunkCache(chunkName) { + const chunkCacheProps = [ + ...commonProperties, + createProperty( + j, + "name", + chunkName.value, + ), + ]; + switch (chunkName.value) { case "vendor": return j.property( "init", createIdentifierOrLiteral(j, chunkName.value), j.objectExpression([ - ...commonProperties, - createProperty( - j, - "name", - chunkName.value, - ), + ...chunkCacheProps, createProperty( j, "test", @@ -76,12 +80,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { "init", createIdentifierOrLiteral(j, chunkName.value), j.objectExpression([ - ...commonProperties, - createProperty( - j, - "name", - chunkName.value, - ), + ...chunkCacheProps, ]), ); } @@ -117,7 +116,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { ...commonProperties, createProperty( j, - "name", + "name", // todo: fix async case p.value.value, ), ); @@ -139,7 +138,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { propValue = pathValue.value; } - cacheGroupsProps.push( + splitChunksProps.push( createProperty( j, p.key.name, @@ -157,9 +156,16 @@ export default function(j: IJSCodeshift, ast: INode): INode { ...splitChunksProps, ]; + // create root props only if cache groups props are present: 5-input if (cacheGroupsProps.length > 0) { rootProps.push( - ...cacheGroupsProps, + j.property( + "init", + createIdentifierOrLiteral(j, "cacheGroups"), + j.objectExpression([ + ...cacheGroupsProps, + ]), + ), ); } From 6329aaa7ff6e2789e47e388f5a1bcc2cd34b2178 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Tue, 7 Aug 2018 12:45:29 +0530 Subject: [PATCH 05/16] feat(migrate): CCP to SCP - regex support, minChunks, async chunks --- .../commonsChunkPlugin.test.js.snap | 86 ++++- .../commonsChunkPlugin-5.input.js | 2 +- .../commonsChunkPlugin-6.input.js | 26 ++ .../commonsChunkPlugin-7.input.js | 11 + .../commonsChunkPlugin.test.js | 4 +- .../commonsChunkPlugin/commonsChunkPlugin.ts | 294 +++++++++--------- 6 files changed, 275 insertions(+), 148 deletions(-) create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index b2902bd8a29..bd55f4cb6da 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -89,9 +89,46 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da "module.export = { optimizations: { splitChunks: { + chunks: 'async', minSize: 0, minChunks: 2, + cacheGroups: { + main: { + chunks: 'initial', + enforce: true, + name: 'main' + } + } + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroups: { + main: { + chunks: 'initial', + enforce: true, + name: 'main' + } + } + }, + + runtimeChunk: true + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { cacheGroups: { main: { chunks: 'initial', @@ -99,9 +136,33 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da name: 'main' }, - chunks: 'initial', - enforce: true, - name: true + name: { + chunks: 'initial', + enforce: true, + name: 'name', + test: ({ resource }) => /node_modules/.test(resource), + + test: ({ resource }) => { + var foo = \\"bar\\"; + return /node_modules/.test(resource); + }, + + test: function ({ resource }) { + var foo = \\"bar\\"; + return /node_modules/.test(resource); + }, + + test: function ({ resource }) { + var foo = \\"bar\\"; + if (foo) { + return /node_modulesfoo/.test(resource); + } else if (foo === \\"something\\") { + return /node_modulesbar/.test(resource); + } else { + return /node_modulesbaz/.test(resource); + } + } + } } } } @@ -109,11 +170,26 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da " `; -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" data 1`] = ` +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" data 1`] = ` "module.export = { optimizations: { splitChunks: { - runtimeChunk: true + minChunks: Infinity, + + cacheGroups: { + main: { + chunks: 'initial', + enforce: true, + name: 'main' + }, + + vendor: { + chunks: 'initial', + enforce: true, + name: 'vendor', + test: '/node_modules/' + } + } } } } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js index d41a1db3046..aa218de9a95 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({ - names: ["runtime"], + names: ["main", "runtime"], }) ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js new file mode 100644 index 00000000000..2a5988ee20f --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js @@ -0,0 +1,26 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: "main", + minChunks: ({ resource }) => /node_modules/.test(resource), + minChunks: ({ resource }) => { + var foo = "bar"; + return /node_modules/.test(resource); + }, + minChunks: function ({ resource }) { + var foo = "bar"; + return /node_modules/.test(resource); + }, + minChunks: function ({ resource }) { + var foo = "bar"; + if (foo) { + return /node_modulesfoo/.test(resource); + } else if (foo === "something") { + return /node_modulesbar/.test(resource); + } else { + return /node_modulesbaz/.test(resource); + } + } + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js new file mode 100644 index 00000000000..eb7578de976 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js @@ -0,0 +1,11 @@ +module.export = { + 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 793dd4bf51a..9d3e3a96d0a 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -8,5 +8,5 @@ defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-2"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-3"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); // todo fix async case defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-5"); -// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6"); -// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-7"); +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 5282d0138ff..1d113e1b18a 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -1,7 +1,6 @@ import { addOrUpdateConfigObject, createIdentifierOrLiteral, - createLiteral, createProperty, findAndRemovePluginByName, findPluginsByName, @@ -10,177 +9,192 @@ import { import { IJSCodeshift, INode } from "../types/NodePath"; /** - * - * Transform for CommonsChunkPlugin. If found, removes the - * plugin and sets optimizations.namedModules to true - * - * @param {Object} j - jscodeshift top-level import - * @param {Node} ast - jscodeshift ast to transform - * @returns {Node} ast - jscodeshift ast - */ + * + * Transform for CommonsChunkPlugin. If found, removes the + * plugin and sets optimizations.namedModules to true + * + * @param {Object} j - jscodeshift top-level import + * @param {Node} ast - jscodeshift ast to transform + * @returns {Node} ast - jscodeshift ast + */ export default function(j: IJSCodeshift, ast: INode): INode { - - let pluginProps: INode[]; - const cacheGroupsProps: INode[] = []; const splitChunksProps: INode[] = []; + const cacheGroupsProps: INode[] = []; + const optimizationProps: object = {}; - const commonProperties: INode[] = [ - createProperty( - j, - "chunks", - "initial", - ), - createProperty( - j, - "enforce", - true, - ), + const commonCacheGroupsProps: INode[] = [ + createProperty(j, "chunks", "initial"), + createProperty(j, "enforce", true), ]; // find old options - const CommonsChunkPlugin = findPluginsByName(j, ast, ["webpack.optimize.CommonsChunkPlugin"]); - - if (!CommonsChunkPlugin.size()) { return ast; } + const CommonsChunkPlugin = findPluginsByName(j, ast, [ + "webpack.optimize.CommonsChunkPlugin", + ]); - CommonsChunkPlugin - .forEach((path: INode): void => { - pluginProps = path.value.arguments[0].properties; // any node - }); - - // create chunk cache group option - function createChunkCache(chunkName) { - const chunkCacheProps = [ - ...commonProperties, - createProperty( - j, - "name", - chunkName.value, - ), - ]; - - switch (chunkName.value) { - case "vendor": - return j.property( - "init", - createIdentifierOrLiteral(j, chunkName.value), - j.objectExpression([ - ...chunkCacheProps, - createProperty( - j, - "test", - "/node_modules/", - ), - ]), - ); - case "common": - case "commons": - // TODO works as default for now - default: - return j.property( - "init", - createIdentifierOrLiteral(j, chunkName.value), - j.objectExpression([ - ...chunkCacheProps, - ]), - ); - } + if (!CommonsChunkPlugin.size()) { + return ast; } - // iterate old props and map new props - pluginProps.forEach((p: INode): void => { - switch (p.key.name) { - case "names": - p.value.elements.forEach((chunkName) => { - if (chunkName.value === "runtime") { - splitChunksProps.push( - createProperty( - j, - "runtimeChunk", - true, - ), - ); - } else { - cacheGroupsProps.push( - createChunkCache(chunkName), - ); + // iterate each CommonsChunkPlugin instance + + // cache group options based on keys + const cacheGroup: object = {}; + + const cacheGroups: INode[] = []; + + CommonsChunkPlugin.forEach( + (path: INode): void => { + const CCPProps = path.value.arguments[0].properties; + + let chunkKey: string; + + // iterate CCP props and map SCP props + CCPProps.forEach( + (p: INode): void => { + const propKey = p.key.name; + + 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/"), + ); + } + } + }); + 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; + case "async": + splitChunksProps.push(createProperty(j, "chunks", "async")); + 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] = []; + } + cacheGroup[chunkKey].push( + j.property( + "init", + createIdentifierOrLiteral(j, "test"), + pathValue, + ), + ); + break; + } + + let propValue; + + if (pathValue.name === "Infinity") { + propValue = Infinity; + } else { + propValue = pathValue.value; + } + + splitChunksProps.push(createProperty(j, p.key.name, propValue)); + break; } - }); - break; - case "name": - cacheGroupsProps.push( - createChunkCache(p.value), - ); - break; - case "async": - cacheGroupsProps.push( - ...commonProperties, - createProperty( - j, - "name", // todo: fix async case - p.value.value, + }, + ); + + Object.keys(cacheGroup).forEach((chunkName) => { + const chunkProps = [ + ...commonCacheGroupsProps, + createProperty(j, "name", chunkName), + ]; + + if ( + cacheGroup[chunkName] && + Array.isArray(cacheGroup[chunkName]) && + cacheGroup[chunkName].length > 0 + ) { + chunkProps.push(...cacheGroup[chunkName]); + } + + cacheGroups.push( + j.property( + "init", + createIdentifierOrLiteral(j, chunkName), + j.objectExpression([...chunkProps]), ), ); - break; - case "minSize": - case "minChunks": - const { value: pathValue } = p; - - // minChunk is a function - if (pathValue.type === "ArrowFunctionExpression" || pathValue.test === "FunctionExpression") { - break; - } - - let propValue; + }); - if (pathValue.name === "Infinity") { - propValue = Infinity; - } else { - propValue = pathValue.value; - } - - splitChunksProps.push( - createProperty( - j, - p.key.name, - propValue, - ), - ); - break; - } - }); + if (cacheGroups.length > 0) { + cacheGroupsProps.push(...cacheGroups); + } + }, + ); // Remove old plugin - const root: INode = findAndRemovePluginByName(j, ast, "webpack.optimize.CommonsChunkPlugin"); + const root: INode = findAndRemovePluginByName( + j, + ast, + "webpack.optimize.CommonsChunkPlugin", + ); - const rootProps = [ - ...splitChunksProps, - ]; + const rootProps = [...splitChunksProps]; // create root props only if cache groups props are present: 5-input - if (cacheGroupsProps.length > 0) { + if (cacheGroupsProps.length > 0) { rootProps.push( j.property( "init", - createIdentifierOrLiteral(j, "cacheGroups"), - j.objectExpression([ - ...cacheGroupsProps, - ]), + createIdentifierOrLiteral(j, "cacheGroups"), + j.objectExpression([...cacheGroupsProps]), ), ); } // Add new optimizations splitChunks option - if (root) { + if (root) { addOrUpdateConfigObject( j, root, "optimizations", "splitChunks", - j.objectExpression([ - ...rootProps, - ]), + j.objectExpression([...rootProps]), ); + + Object.keys(optimizationProps).forEach((key) => { + addOrUpdateConfigObject( + j, + root, + "optimizations", + key, + optimizationProps[key], + ); + }); } - return ast; + return ast; } From bde0049d28ab972e0545d2dabdff84e429aae661 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Tue, 7 Aug 2018 18:34:21 +0530 Subject: [PATCH 06/16] 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 | 134 +++++++++--------- 11 files changed, 121 insertions(+), 122 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..f2af16baaa4 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -28,7 +28,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { ]; // find old options - const CommonsChunkPlugin = findPluginsByName(j, ast, [ + const CommonsChunkPlugin: INode = findPluginsByName(j, ast, [ "webpack.optimize.CommonsChunkPlugin", ]); @@ -36,102 +36,106 @@ 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; + const CCPProps: INode[] = path.value.arguments[0].properties; let chunkKey: string; // iterate CCP props and map SCP props CCPProps.forEach( (p: INode): void => { - const propKey = p.key.name; + const propKey: string = p.key.name; 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 }: INode = 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: INode; - 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]) && @@ -162,7 +166,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { "webpack.optimize.CommonsChunkPlugin", ); - const rootProps = [...splitChunksProps]; + const rootProps: INode[] = [...splitChunksProps]; // create root props only if cache groups props are present: 5-input if (cacheGroupsProps.length > 0) { From 41534710987d51431570970bd59f65331aea261c Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Wed, 8 Aug 2018 13:45:56 +0530 Subject: [PATCH 07/16] feat(migrate): CCP to SCP - multiple instances --- .../commonsChunkPlugin.test.js.snap | 78 +++++++++++++++++++ .../commonsChunkPlugin-10.input.js | 10 +++ .../commonsChunkPlugin-8.input.js | 10 +++ .../commonsChunkPlugin-9.input.js | 10 +++ .../commonsChunkPlugin.test.js | 3 + .../commonsChunkPlugin/commonsChunkPlugin.ts | 9 ++- 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index 8d59226331e..94d32a78629 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -193,3 +193,81 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da } " `; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-8" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroups: { + main: { + name: 'main', + chunks: 'initial', + enforce: true + }, + + vendor: { + name: 'vendor', + chunks: 'initial', + enforce: true, + test: '/node_modules/' + }, + + app: { + name: 'app', + chunks: 'initial', + enforce: true + } + } + } + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-9" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroups: { + main: { + name: 'main', + chunks: 'initial', + enforce: true + }, + + app: { + name: 'app', + chunks: 'initial', + enforce: true + } + } + }, + + runtimeChunk: true + } +} +" +`; + +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-10" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroups: { + app: { + name: 'app', + chunks: 'initial', + enforce: true + }, + + runtime: { + name: 'runtime', + chunks: 'initial', + enforce: true + } + } + } + } +} +" +`; diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js new file mode 100644 index 00000000000..9d828c541da --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js @@ -0,0 +1,10 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: "app" + }), + new webpack.optimize.CommonsChunkPlugin({ + name: "runtime" + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js new file mode 100644 index 00000000000..15c359a3808 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js @@ -0,0 +1,10 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + names: ["main", "vendor"] + }), + new webpack.optimize.CommonsChunkPlugin({ + name: "app" + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js new file mode 100644 index 00000000000..44242ddaf74 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js @@ -0,0 +1,10 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + names: ["main", "runtime"] + }), + new webpack.optimize.CommonsChunkPlugin({ + name: "app" + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js index f1d540e76a1..285c672844a 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -10,3 +10,6 @@ defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-5"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-7"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-8"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-9"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-10"); diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index f2af16baaa4..16ab0d13214 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -37,8 +37,8 @@ export default function(j: IJSCodeshift, ast: INode): INode { } // cache group options based on keys - const cacheGroup: object = {}; - const cacheGroups: INode[] = []; + let cacheGroup: object = {}; + let cacheGroups: INode[] = []; let isAsyncChunk: boolean = false; // iterate each CommonsChunkPlugin instance @@ -46,6 +46,11 @@ export default function(j: IJSCodeshift, ast: INode): INode { (path: INode): void => { const CCPProps: INode[] = path.value.arguments[0].properties; + // reset chunks from old props + cacheGroup = {}; + cacheGroups = []; + isAsyncChunk = false; + let chunkKey: string; // iterate CCP props and map SCP props From 53fcea1d16dcdc4dbfbed985d48b956b5fab4b54 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Wed, 8 Aug 2018 14:29:16 +0530 Subject: [PATCH 08/16] feat(migrate): CCP to SCP - fix runtime bug --- .../commonsChunkPlugin.test.js.snap | 25 +++++++------------ .../commonsChunkPlugin-10.input.js | 10 +++++--- .../commonsChunkPlugin/commonsChunkPlugin.ts | 24 ++++++++++++------ 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index 94d32a78629..9f9bdb85c13 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -131,12 +131,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6" da main: { name: 'main', chunks: 'initial', - enforce: true - }, - - name: { - name: 'name', - chunks: 'initial', enforce: true, test: ({ resource }) => /node_modules/.test(resource), @@ -253,20 +247,19 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-10" d "module.export = { optimizations: { splitChunks: { - cacheGroups: { - app: { - name: 'app', - chunks: 'initial', - enforce: true - }, + minChunks: Infinity, - runtime: { - name: 'runtime', + cacheGroups: { + modules: { + name: 'modules', chunks: 'initial', - enforce: true + enforce: true, + test: ({ resource }) => /node_modules/.test(resource) } } - } + }, + + runtimeChunk: true } } " diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js index 9d828c541da..8a7d7366b04 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js @@ -1,10 +1,12 @@ module.export = { plugins: [ new webpack.optimize.CommonsChunkPlugin({ - name: "app" - }), + name: "runtime", + minChunks: Infinity, + }), new webpack.optimize.CommonsChunkPlugin({ - name: "runtime" - }) + name: "modules", + minChunks: ({ resource }) => /node_modules/.test(resource), + }), ] } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index 16ab0d13214..8ccf1fa32b6 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -81,16 +81,23 @@ export default function(j: IJSCodeshift, ast: INode): INode { 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/"), + const nameKey = p.value.value; + if (nameKey === "runtime") { + optimizationProps["runtimeChunk"] = createIdentifierOrLiteral( // tslint:disable-line + j, + true, ); + } else { + chunkKey = nameKey; + if (!Array.isArray(cacheGroup[nameKey])) { + cacheGroup[nameKey] = []; + } + if (nameKey === "vendor") { + cacheGroup[nameKey].push( + createProperty(j, "test", "/node_modules/"), + ); + } } - break; case "async": @@ -107,6 +114,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { pathValue.type === "ArrowFunctionExpression" || pathValue.type === "FunctionExpression" ) { + if (!Array.isArray(cacheGroup[chunkKey])) { cacheGroup[chunkKey] = []; } From 4fe0db3924d518dd30d92186e79d428ba560ca9d Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Thu, 9 Aug 2018 13:04:41 +0530 Subject: [PATCH 09/16] feat(migrate): CCP to SCP - fix minChunks async, bug --- .../commonsChunkPlugin.test.js.snap | 142 +++++++++--------- .../commonsChunkPlugin-10.input.js | 2 +- .../commonsChunkPlugin-11.input.js | 8 + .../commonsChunkPlugin-3.input.js | 2 +- .../commonsChunkPlugin-4.input.js | 3 +- .../commonsChunkPlugin-6a.input.js | 8 + .../commonsChunkPlugin-6b.input.js | 11 ++ .../commonsChunkPlugin-6c.input.js | 11 ++ ...nput.js => commonsChunkPlugin-6d.input.js} | 9 -- .../commonsChunkPlugin.test.js | 12 +- .../commonsChunkPlugin/commonsChunkPlugin.ts | 98 ++++++------ 11 files changed, 175 insertions(+), 131 deletions(-) create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js create mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js rename packages/migrate/commonsChunkPlugin/__testfixtures__/{commonsChunkPlugin-6.input.js => commonsChunkPlugin-6d.input.js} (57%) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index 9f9bdb85c13..aaeec7e9b43 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -4,20 +4,19 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da "module.export = { optimizations: { splitChunks: { - minChunks: 2, - cacheGroups: { common: { name: 'common', chunks: 'initial', - enforce: true + enforce: true, + minChunks: 2 }, vendor: { name: 'vendor', chunks: 'initial', enforce: true, - test: '/node_modules/' + minChunks: 2 } } } @@ -34,14 +33,15 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da common: { name: 'common', chunks: 'initial', - enforce: true + enforce: true, + minChunks: 2 }, vendor: { name: 'vendor', chunks: 'initial', enforce: true, - test: '/node_modules/' + minChunks: 2 } } } @@ -59,7 +59,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da name: 'vendor', chunks: 'initial', enforce: true, - test: '/node_modules/' + minChunks: 1 } } } @@ -76,7 +76,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da commons: { name: 'commons', chunks: 'initial', - enforce: true + enforce: true, + minChunks: 1, + filename: 'commons.js' } } } @@ -90,12 +92,12 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da optimizations: { splitChunks: { chunks: 'async', - minSize: 0, - minChunks: 2, + minSize: 2000, cacheGroups: { main: { - name: 'main' + name: 'main', + minChunks: 1 } } } @@ -112,18 +114,21 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" da main: { name: 'main', chunks: 'initial', - enforce: true + enforce: true, + minChunks: 1 } } }, - runtimeChunk: true + runtimeChunk: { + name: 'runtime' + } } } " `; -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6" data 1`] = ` +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" data 1`] = ` "module.export = { optimizations: { splitChunks: { @@ -132,27 +137,30 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6" da name: 'main', chunks: 'initial', enforce: true, - test: ({ resource }) => /node_modules/.test(resource), + minChunks: 1, + test: ({ resource }) => /node_modules/.test(resource) + } + } + } + } +} +" +`; - test: ({ resource }) => { - var foo = \\"bar\\"; - return /node_modules/.test(resource); - }, +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" data 1`] = ` +"module.export = { + optimizations: { + splitChunks: { + cacheGroups: { + main: { + name: 'main', + chunks: 'initial', + enforce: true, + minChunks: 1, - test: function ({ resource }) { + test: ({ resource }) => { var foo = \\"bar\\"; return /node_modules/.test(resource); - }, - - test: function ({ resource }) { - var foo = \\"bar\\"; - if (foo) { - return /node_modulesfoo/.test(resource); - } else if (foo === \\"something\\") { - return /node_modulesbar/.test(resource); - } else { - return /node_modulesbaz/.test(resource); - } } } } @@ -162,24 +170,21 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6" da " `; -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" data 1`] = ` +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" data 1`] = ` "module.export = { optimizations: { splitChunks: { - minChunks: Infinity, - cacheGroups: { main: { name: 'main', chunks: 'initial', - enforce: true - }, - - vendor: { - name: 'vendor', - chunks: 'initial', enforce: true, - test: '/node_modules/' + minChunks: 1, + + test: function ({ resource }) { + var foo = \\"bar\\"; + return /node_modules/.test(resource); + } } } } @@ -188,7 +193,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da " `; -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-8" data 1`] = ` +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" data 1`] = ` "module.export = { optimizations: { splitChunks: { @@ -196,20 +201,19 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-8" da main: { name: 'main', chunks: 'initial', - enforce: true - }, - - vendor: { - name: 'vendor', - chunks: 'initial', enforce: true, - test: '/node_modules/' - }, + minChunks: 1, - app: { - name: 'app', - chunks: 'initial', - enforce: true + test: function ({ resource }) { + var foo = \\"bar\\"; + if (foo) { + return /node_modulesfoo/.test(resource); + } else if (foo === \\"something\\") { + return /node_modulesbar/.test(resource); + } else { + return /node_modulesbaz/.test(resource); + } + } } } } @@ -218,7 +222,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-8" da " `; -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-9" data 1`] = ` +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" data 1`] = ` "module.export = { optimizations: { splitChunks: { @@ -226,40 +230,38 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-9" da main: { name: 'main', chunks: 'initial', - enforce: true + enforce: true, + minChunks: 2 }, - app: { - name: 'app', + vendor: { + name: 'vendor', chunks: 'initial', - enforce: true + enforce: true, + minChunks: 2 } } - }, - - runtimeChunk: true + } } } " `; -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-10" data 1`] = ` +exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-11" data 1`] = ` "module.export = { optimizations: { splitChunks: { - minChunks: Infinity, + minSize: 3000, cacheGroups: { - modules: { - name: 'modules', + main: { + name: 'main', chunks: 'initial', enforce: true, - test: ({ resource }) => /node_modules/.test(resource) + minChunks: 1 } } - }, - - runtimeChunk: true + } } } " diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js index 8a7d7366b04..9ac01eb3af7 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js @@ -3,7 +3,7 @@ module.export = { new webpack.optimize.CommonsChunkPlugin({ name: "runtime", minChunks: Infinity, - }), + }), new webpack.optimize.CommonsChunkPlugin({ name: "modules", minChunks: ({ resource }) => /node_modules/.test(resource), diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js new file mode 100644 index 00000000000..d3de6736899 --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js @@ -0,0 +1,8 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: "main", + minSize: 3000, + }), + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js index b6852ef4efc..c296d27a85b 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({ + name: "commons", 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 48428ca18c4..6ab495725d1 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js @@ -3,8 +3,7 @@ module.export = { new webpack.optimize.CommonsChunkPlugin({ name: "main", async: true, - minSize: 0, - minChunks: 2 + minSize: 2000, }) ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js new file mode 100644 index 00000000000..c8224f1788a --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js @@ -0,0 +1,8 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: "main", + minChunks: ({ resource }) => /node_modules/.test(resource), + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js new file mode 100644 index 00000000000..61c5e8ee22a --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js @@ -0,0 +1,11 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: "main", + minChunks: ({ resource }) => { + var foo = "bar"; + return /node_modules/.test(resource); + }, + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js new file mode 100644 index 00000000000..d1108b7948c --- /dev/null +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js @@ -0,0 +1,11 @@ +module.export = { + plugins: [ + new webpack.optimize.CommonsChunkPlugin({ + name: "main", + minChunks: function ({ resource }) { + var foo = "bar"; + return /node_modules/.test(resource); + }, + }) + ] +} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js similarity index 57% rename from packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js rename to packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js index 7554f9eb8ef..228744c2698 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js @@ -2,15 +2,6 @@ module.export = { plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "main", - minChunks: ({ resource }) => /node_modules/.test(resource), - minChunks: ({ resource }) => { - var foo = "bar"; - return /node_modules/.test(resource); - }, - minChunks: function ({ resource }) { - var foo = "bar"; - return /node_modules/.test(resource); - }, minChunks: function ({ resource }) { var foo = "bar"; if (foo) { diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js index 285c672844a..fa2ad1a3df3 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -8,8 +8,12 @@ defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-2"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-3"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-4"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-5"); -defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6a"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6b"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6c"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6d"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-7"); -defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-8"); -defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-9"); -defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-10"); +// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-8"); +// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-9"); +// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-10"); +defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-11"); diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index 8ccf1fa32b6..04615e6d2b5 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -9,20 +9,20 @@ import { import { IJSCodeshift, INode } from "../types/NodePath"; /** - * - * Transform for CommonsChunkPlugin. If found, removes the - * plugin and sets optimizations.namedModules to true - * - * @param {Object} j - jscodeshift top-level import - * @param {Node} ast - jscodeshift ast to transform - * @returns {Node} ast - jscodeshift ast - */ + * + * Transform for CommonsChunkPlugin. If found, removes the + * plugin and sets optimizations.namedModules to true + * + * @param {Object} j - jscodeshift top-level import + * @param {Node} ast - jscodeshift ast to transform + * @returns {Node} ast - jscodeshift ast + */ export default function(j: IJSCodeshift, ast: INode): INode { const splitChunksProps: INode[] = []; const cacheGroupsProps: INode[] = []; const optimizationProps: object = {}; - const commonCacheGroupsProps: INode[] = [ + let commonCacheGroupsProps: INode[] = [ createProperty(j, "chunks", "initial"), createProperty(j, "enforce", true), ]; @@ -50,8 +50,13 @@ export default function(j: IJSCodeshift, ast: INode): INode { cacheGroup = {}; cacheGroups = []; isAsyncChunk = false; + commonCacheGroupsProps = [ + createProperty(j, "chunks", "initial"), + createProperty(j, "enforce", true), + ]; let chunkKey: string; + let chunkCount: number = 0; // iterate CCP props and map SCP props CCPProps.forEach( @@ -62,50 +67,55 @@ export default function(j: IJSCodeshift, ast: INode): INode { case "names": p.value.elements.forEach((chunkName) => { if (chunkName.value === "runtime") { - optimizationProps["runtimeChunk"] = createIdentifierOrLiteral( // tslint:disable-line - j, - true, - ); + optimizationProps["runtimeChunk"] = j.objectExpression([ // tslint:disable-line + createProperty(j, "name", chunkName.value), + ]); } else { + chunkCount++; if (!Array.isArray(cacheGroup[chunkName.value])) { cacheGroup[chunkName.value] = []; } - - if (chunkName.value === "vendor") { - cacheGroup[chunkName.value].push( - createProperty(j, "test", "/node_modules/"), - ); - } } }); break; case "name": + chunkCount++; const nameKey = p.value.value; if (nameKey === "runtime") { - optimizationProps["runtimeChunk"] = createIdentifierOrLiteral( // tslint:disable-line - j, - true, - ); + optimizationProps["runtimeChunk"] = j.objectExpression([ // tslint:disable-line + createProperty(j, "name", nameKey), + ]); } else { chunkKey = nameKey; + if (!Array.isArray(cacheGroup[nameKey])) { cacheGroup[nameKey] = []; } - if (nameKey === "vendor") { - cacheGroup[nameKey].push( - createProperty(j, "test", "/node_modules/"), - ); + } + break; + + case "filename": + if (chunkKey) { + if (!Array.isArray(cacheGroup[chunkKey])) { + cacheGroup[chunkKey] = []; } + cacheGroup[chunkKey].push( + createProperty(j, propKey, p.value.value), + ); } - break; + break; case "async": splitChunksProps.push(createProperty(j, "chunks", "async")); isAsyncChunk = true; - break; + break; case "minSize": + splitChunksProps.push( + j.property("init", createIdentifierOrLiteral(j, propKey), p.value), + ); + break; case "minChunks": const { value: pathValue }: INode = p; @@ -114,7 +124,6 @@ export default function(j: IJSCodeshift, ast: INode): INode { pathValue.type === "ArrowFunctionExpression" || pathValue.type === "FunctionExpression" ) { - if (!Array.isArray(cacheGroup[chunkKey])) { cacheGroup[chunkKey] = []; } @@ -125,19 +134,8 @@ export default function(j: IJSCodeshift, ast: INode): INode { pathValue, ), ); - break; - } - - let propValue: INode; - - if (pathValue.name === "Infinity") { - propValue = Infinity; - } else { - propValue = pathValue.value; } - - splitChunksProps.push(createProperty(j, p.key.name, propValue)); - break; + break; } }, ); @@ -147,7 +145,20 @@ export default function(j: IJSCodeshift, ast: INode): INode { createProperty(j, "name", chunkName), ]; - if (!isAsyncChunk) { chunkProps.push(...commonCacheGroupsProps); } + if (isAsyncChunk) { + chunkProps.push(j.property( + "init", + createIdentifierOrLiteral(j, "minChunks"), + createIdentifierOrLiteral(j, chunkCount), + )); + } else { + chunkProps.push(...commonCacheGroupsProps, + j.property( + "init", + createIdentifierOrLiteral(j, "minChunks"), + createIdentifierOrLiteral(j, chunkCount), + )); + } if ( cacheGroup[chunkName] && @@ -181,7 +192,6 @@ export default function(j: IJSCodeshift, ast: INode): INode { const rootProps: INode[] = [...splitChunksProps]; - // create root props only if cache groups props are present: 5-input if (cacheGroupsProps.length > 0) { rootProps.push( j.property( From db22fcce90f74ea1a350811ed5a7c92a24822897 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Thu, 9 Aug 2018 15:41:43 +0530 Subject: [PATCH 10/16] feat(migrate): CCP to SCP - fix test prop bug --- .../commonsChunkPlugin.test.js.snap | 93 ++++++++++++------- .../commonsChunkPlugin-0.input.js | 6 +- .../commonsChunkPlugin-1.input.js | 4 + .../commonsChunkPlugin-10.input.js | 12 --- .../commonsChunkPlugin-11.input.js | 8 -- .../commonsChunkPlugin-2.input.js | 4 + .../commonsChunkPlugin-4.input.js | 4 + .../commonsChunkPlugin-5.input.js | 4 + .../commonsChunkPlugin-6a.input.js | 4 + .../commonsChunkPlugin-6b.input.js | 4 + .../commonsChunkPlugin-6c.input.js | 4 + .../commonsChunkPlugin-6d.input.js | 4 + .../commonsChunkPlugin-7.input.js | 10 +- .../commonsChunkPlugin-8.input.js | 10 -- .../commonsChunkPlugin-9.input.js | 10 -- .../commonsChunkPlugin.test.js | 4 - .../commonsChunkPlugin/commonsChunkPlugin.ts | 37 ++++++-- 17 files changed, 135 insertions(+), 87 deletions(-) delete mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js delete mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js delete mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js delete mode 100644 packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index aaeec7e9b43..aa27ed1365c 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -2,21 +2,28 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" data 1`] = ` "module.export = { + entry: { + app: './src/app.js', + vendor: './src/vendors.js', + }, + optimizations: { splitChunks: { cacheGroups: { - common: { - name: 'common', + app: { + name: 'app', chunks: 'initial', enforce: true, - minChunks: 2 + minChunks: 2, + test: 'app' }, vendor: { name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 2 + minChunks: 2, + test: 'vendor' } } } @@ -27,6 +34,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" data 1`] = ` "module.export = { + entry: { + vendor: './src/vendors.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -41,7 +52,8 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 2 + minChunks: 2, + test: 'vendor' } } } @@ -52,6 +64,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" data 1`] = ` "module.export = { + entry: { + vendor: './src/vendors.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -59,7 +75,8 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 1 + minChunks: 1, + test: 'vendor' } } } @@ -89,6 +106,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" data 1`] = ` "module.export = { + entry: { + main: './src/index.js', + }, + optimizations: { splitChunks: { chunks: 'async', @@ -97,7 +118,8 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da cacheGroups: { main: { name: 'main', - minChunks: 1 + minChunks: 1, + test: 'main' } } } @@ -108,6 +130,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" data 1`] = ` "module.export = { + entry: { + main: './src/index.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -115,7 +141,8 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" da name: 'main', chunks: 'initial', enforce: true, - minChunks: 1 + minChunks: 1, + test: 'main' } } }, @@ -130,6 +157,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" da exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" data 1`] = ` "module.export = { + entry: { + main: './src/index.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -138,6 +169,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" d chunks: 'initial', enforce: true, minChunks: 1, + test: 'main', test: ({ resource }) => /node_modules/.test(resource) } } @@ -149,6 +181,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" d exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" data 1`] = ` "module.export = { + entry: { + main: './src/index.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -157,6 +193,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" d chunks: 'initial', enforce: true, minChunks: 1, + test: 'main', test: ({ resource }) => { var foo = \\"bar\\"; @@ -172,6 +209,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" d exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" data 1`] = ` "module.export = { + entry: { + main: './src/index.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -180,6 +221,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" d chunks: 'initial', enforce: true, minChunks: 1, + test: 'main', test: function ({ resource }) { var foo = \\"bar\\"; @@ -195,6 +237,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" d exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" data 1`] = ` "module.export = { + entry: { + main: './src/index.js', + }, + optimizations: { splitChunks: { cacheGroups: { @@ -203,6 +249,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" d chunks: 'initial', enforce: true, minChunks: 1, + test: 'main', test: function ({ resource }) { var foo = \\"bar\\"; @@ -224,31 +271,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" d exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" data 1`] = ` "module.export = { - optimizations: { - splitChunks: { - cacheGroups: { - main: { - name: 'main', - chunks: 'initial', - enforce: true, - minChunks: 2 - }, - - vendor: { - name: 'vendor', - chunks: 'initial', - enforce: true, - minChunks: 2 - } - } - } - } -} -" -`; + entry: { + main: './src/index.js', + }, -exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-11" data 1`] = ` -"module.export = { optimizations: { splitChunks: { minSize: 3000, @@ -258,7 +284,8 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-11" d name: 'main', chunks: 'initial', enforce: true, - minChunks: 1 + minChunks: 1, + test: 'main' } } } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js index f66ded553e9..5687b165008 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js @@ -1,7 +1,11 @@ module.export = { + entry: { + app: './src/app.js', + vendor: './src/vendors.js', + }, plugins: [ new webpack.optimize.CommonsChunkPlugin({ - names: ["common", "vendor"], + names: ["app", "vendor"], minChunks: 2 }) ] diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js index 63202321e4f..6d950c1401c 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + vendor: './src/vendors.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ names: ["common", "vendor"] diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js deleted file mode 100644 index 9ac01eb3af7..00000000000 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-10.input.js +++ /dev/null @@ -1,12 +0,0 @@ -module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - name: "runtime", - minChunks: Infinity, - }), - new webpack.optimize.CommonsChunkPlugin({ - name: "modules", - minChunks: ({ resource }) => /node_modules/.test(resource), - }), - ] -} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js deleted file mode 100644 index d3de6736899..00000000000 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-11.input.js +++ /dev/null @@ -1,8 +0,0 @@ -module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - name: "main", - minSize: 3000, - }), - ] -} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js index 783778fc416..44482258b11 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + vendor: './src/vendors.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ names: ["vendor"] diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js index 6ab495725d1..0c350285425 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "main", diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js index 9b1ab0e7592..d008206d846 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ names: ["main", "runtime"], diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js index c8224f1788a..46f3be8ea3b 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "main", diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js index 61c5e8ee22a..16d4f7b7420 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "main", diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js index d1108b7948c..6c4cfad7bdf 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "main", diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js index 228744c2698..62817af40f4 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js @@ -1,4 +1,8 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "main", diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js index 6ebf70ef678..3db72ac2f1f 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js @@ -1,8 +1,12 @@ module.export = { + entry: { + main: './src/index.js', + }, + plugins: [ new webpack.optimize.CommonsChunkPlugin({ - names: ["main", "vendor"], - minChunks: Infinity, - }) + name: "main", + minSize: 3000, + }), ] } diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js deleted file mode 100644 index 15c359a3808..00000000000 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-8.input.js +++ /dev/null @@ -1,10 +0,0 @@ -module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - names: ["main", "vendor"] - }), - new webpack.optimize.CommonsChunkPlugin({ - name: "app" - }) - ] -} diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js deleted file mode 100644 index 44242ddaf74..00000000000 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-9.input.js +++ /dev/null @@ -1,10 +0,0 @@ -module.export = { - plugins: [ - new webpack.optimize.CommonsChunkPlugin({ - names: ["main", "runtime"] - }), - new webpack.optimize.CommonsChunkPlugin({ - name: "app" - }) - ] -} diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js index fa2ad1a3df3..123b3ba81d8 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js @@ -13,7 +13,3 @@ defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6b"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6c"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-6d"); defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-7"); -// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-8"); -// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-9"); -// defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-10"); -defineTest(__dirname, "commonsChunkPlugin", "commonsChunkPlugin-11"); diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index 04615e6d2b5..a842e5939e6 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -4,6 +4,7 @@ import { createProperty, findAndRemovePluginByName, findPluginsByName, + findRootNodesByName, } from "@webpack-cli/utils/ast-utils"; import { IJSCodeshift, INode } from "../types/NodePath"; @@ -65,23 +66,36 @@ export default function(j: IJSCodeshift, ast: INode): INode { switch (propKey) { case "names": - p.value.elements.forEach((chunkName) => { - if (chunkName.value === "runtime") { + p.value.elements.forEach(({ value: chunkValue }) => { + if (chunkValue === "runtime") { optimizationProps["runtimeChunk"] = j.objectExpression([ // tslint:disable-line - createProperty(j, "name", chunkName.value), + createProperty(j, "name", chunkValue), ]); } else { chunkCount++; - if (!Array.isArray(cacheGroup[chunkName.value])) { - cacheGroup[chunkName.value] = []; + + if (!Array.isArray(cacheGroup[chunkValue])) { + cacheGroup[chunkValue] = []; } + + findRootNodesByName(j, ast, "entry").forEach( + ({ value: { value: { properties: entries }} }, + ) => { + entries.forEach(({ key: { name: entryName }}) => + entryName === chunkValue ? cacheGroup[chunkValue].push( + createProperty(j, "test", entryName), + ) : null, + ); + }); } }); break; case "name": chunkCount++; + const nameKey = p.value.value; + if (nameKey === "runtime") { optimizationProps["runtimeChunk"] = j.objectExpression([ // tslint:disable-line createProperty(j, "name", nameKey), @@ -92,6 +106,16 @@ export default function(j: IJSCodeshift, ast: INode): INode { if (!Array.isArray(cacheGroup[nameKey])) { cacheGroup[nameKey] = []; } + + findRootNodesByName(j, ast, "entry").forEach( + ({ value: { value: { properties: entries }} }, + ) => { + entries.forEach(({ key: { name: entryName }}) => + entryName === nameKey ? cacheGroup[nameKey].push( + createProperty(j, "test", entryName), + ) : null, + ); + }); } break; @@ -116,7 +140,8 @@ export default function(j: IJSCodeshift, ast: INode): INode { j.property("init", createIdentifierOrLiteral(j, propKey), p.value), ); break; - case "minChunks": + + case "minChunks" : const { value: pathValue }: INode = p; // minChunk is a function From 08eff347593336b496879d1854cb62fb33482abd Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Mon, 13 Aug 2018 14:49:13 +0530 Subject: [PATCH 11/16] feat(migrate): CCP to SCP - fix PR comments --- .../__testfixtures__/commonsChunkPlugin-0.input.js | 2 +- .../__testfixtures__/commonsChunkPlugin-1.input.js | 2 +- .../__testfixtures__/commonsChunkPlugin-2.input.js | 2 +- .../__testfixtures__/commonsChunkPlugin-3.input.js | 2 +- .../__testfixtures__/commonsChunkPlugin-4.input.js | 2 +- .../__testfixtures__/commonsChunkPlugin-5.input.js | 2 +- .../commonsChunkPlugin-6a.input.js | 2 +- .../commonsChunkPlugin-6b.input.js | 2 +- .../commonsChunkPlugin-6c.input.js | 2 +- .../commonsChunkPlugin-6d.input.js | 2 +- .../__testfixtures__/commonsChunkPlugin-7.input.js | 2 +- .../commonsChunkPlugin/commonsChunkPlugin.ts | 14 +++++++------- 12 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js index 5687b165008..5174a051ee1 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { app: './src/app.js', vendor: './src/vendors.js', diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js index 6d950c1401c..278b2581639 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { vendor: './src/vendors.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js index 44482258b11..83a4fb6d3c5 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { vendor: './src/vendors.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js index c296d27a85b..0c4289d6290 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: "commons", diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js index 0c350285425..973867a9866 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js index d008206d846..c7b2dc1c53d 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-5.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js index 46f3be8ea3b..641965277fa 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6a.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js index 16d4f7b7420..b51ee8a7483 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js index 6c4cfad7bdf..1a1b0042b7e 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js index 62817af40f4..4d7763906a7 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js index 3db72ac2f1f..9999d11bd64 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-7.input.js @@ -1,4 +1,4 @@ -module.export = { +module.exports = { entry: { main: './src/index.js', }, diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index a842e5939e6..a657bfbb0f8 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -66,7 +66,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { switch (propKey) { case "names": - p.value.elements.forEach(({ value: chunkValue }) => { + p.value.elements.forEach(({ value: chunkValue }): void => { if (chunkValue === "runtime") { optimizationProps["runtimeChunk"] = j.objectExpression([ // tslint:disable-line createProperty(j, "name", chunkValue), @@ -80,8 +80,8 @@ export default function(j: IJSCodeshift, ast: INode): INode { findRootNodesByName(j, ast, "entry").forEach( ({ value: { value: { properties: entries }} }, - ) => { - entries.forEach(({ key: { name: entryName }}) => + ): void => { + entries.forEach(({ key: { name: entryName }}): void => entryName === chunkValue ? cacheGroup[chunkValue].push( createProperty(j, "test", entryName), ) : null, @@ -109,8 +109,8 @@ export default function(j: IJSCodeshift, ast: INode): INode { findRootNodesByName(j, ast, "entry").forEach( ({ value: { value: { properties: entries }} }, - ) => { - entries.forEach(({ key: { name: entryName }}) => + ): void => { + entries.forEach(({ key: { name: entryName }}): void => entryName === nameKey ? cacheGroup[nameKey].push( createProperty(j, "test", entryName), ) : null, @@ -165,7 +165,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { }, ); - Object.keys(cacheGroup).forEach((chunkName) => { + Object.keys(cacheGroup).forEach((chunkName: string): void => { const chunkProps: INode[] = [ createProperty(j, "name", chunkName), ]; @@ -237,7 +237,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { j.objectExpression([...rootProps]), ); - Object.keys(optimizationProps).forEach((key) => { + Object.keys(optimizationProps).forEach((key: string): void => { addOrUpdateConfigObject( j, root, From b671af532f61dd82742a6e06f15cf21be3d8d022 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Mon, 13 Aug 2018 14:50:34 +0530 Subject: [PATCH 12/16] feat(migrate): CCP to SCP - update snapshots --- .../commonsChunkPlugin.test.js.snap | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index aa27ed1365c..4d689718daa 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" data 1`] = ` -"module.export = { +"module.exports = { entry: { app: './src/app.js', vendor: './src/vendors.js', @@ -33,7 +33,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" data 1`] = ` -"module.export = { +"module.exports = { entry: { vendor: './src/vendors.js', }, @@ -63,7 +63,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" data 1`] = ` -"module.export = { +"module.exports = { entry: { vendor: './src/vendors.js', }, @@ -86,7 +86,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" data 1`] = ` -"module.export = { +"module.exports = { optimizations: { splitChunks: { cacheGroups: { @@ -105,7 +105,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, @@ -129,7 +129,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, @@ -156,7 +156,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" da `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, @@ -180,7 +180,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" d `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, @@ -208,7 +208,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" d `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, @@ -236,7 +236,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" d `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, @@ -270,7 +270,7 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" d `; exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" data 1`] = ` -"module.export = { +"module.exports = { entry: { main: './src/index.js', }, From 1e441e3b418863b45588585df4f8c43fad90630b Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Thu, 16 Aug 2018 23:50:10 +0530 Subject: [PATCH 13/16] feat(migrate): CCP to SCP - merge test entry prop and function exp --- .../commonsChunkPlugin.test.js.snap | 75 +++++++---- .../commonsChunkPlugin-6b.input.js | 2 +- .../commonsChunkPlugin-6c.input.js | 2 +- .../commonsChunkPlugin-6d.input.js | 3 - .../commonsChunkPlugin/commonsChunkPlugin.ts | 118 +++++++++++++----- packages/migrate/types/NodePath.ts | 7 ++ 6 files changed, 143 insertions(+), 64 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index 4d689718daa..8278a1ea76e 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -45,14 +45,14 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da name: 'common', chunks: 'initial', enforce: true, - minChunks: 2 + minChunks: 1 }, vendor: { name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 2, + minChunks: 1, test: 'vendor' } } @@ -94,7 +94,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-3" da name: 'commons', chunks: 'initial', enforce: true, - minChunks: 1, filename: 'commons.js' } } @@ -113,9 +112,10 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da optimizations: { splitChunks: { chunks: 'async', - minSize: 2000, cacheGroups: { + minSize: 2000, + main: { name: 'main', minChunks: 1, @@ -169,8 +169,14 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" d chunks: 'initial', enforce: true, minChunks: 1, - test: 'main', - test: ({ resource }) => /node_modules/.test(resource) + + test: module => { + if (module.getChunks().some(chunk => chunk.name === 'main')) + return true; + + const fn = ({ resource }) => /node_modules/.test(resource); + return fn(module); + } } } } @@ -193,11 +199,17 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" d chunks: 'initial', enforce: true, minChunks: 1, - test: 'main', - test: ({ resource }) => { - var foo = \\"bar\\"; - return /node_modules/.test(resource); + test: module => { + if (module.getChunks().some(chunk => chunk.name === 'main')) + return true; + + const fn = ({ resource }) => { + // some code + return /node_modules/.test(resource); + }; + + return fn(module); } } } @@ -221,11 +233,17 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" d chunks: 'initial', enforce: true, minChunks: 1, - test: 'main', - test: function ({ resource }) { - var foo = \\"bar\\"; - return /node_modules/.test(resource); + test: module => { + if (module.getChunks().some(chunk => chunk.name === 'main')) + return true; + + const fn = function ({ resource }) { + // some code + return /node_modules/.test(resource); + }; + + return fn(module); } } } @@ -249,17 +267,20 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" d chunks: 'initial', enforce: true, minChunks: 1, - test: 'main', - - test: function ({ resource }) { - var foo = \\"bar\\"; - if (foo) { - return /node_modulesfoo/.test(resource); - } else if (foo === \\"something\\") { - return /node_modulesbar/.test(resource); - } else { - return /node_modulesbaz/.test(resource); - } + + test: module => { + if (module.getChunks().some(chunk => chunk.name === 'main')) + return true; + + const fn = function ({ resource }) { + if (foo) { + return /node_modulesfoo/.test(resource); + } else { + return /node_modulesbaz/.test(resource); + } + }; + + return fn(module); } } } @@ -277,9 +298,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da optimizations: { splitChunks: { - minSize: 3000, - cacheGroups: { + minSize: 3000, + main: { name: 'main', chunks: 'initial', diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js index b51ee8a7483..fd48b62e34c 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6b.input.js @@ -7,7 +7,7 @@ module.exports = { new webpack.optimize.CommonsChunkPlugin({ name: "main", minChunks: ({ resource }) => { - var foo = "bar"; + // some code return /node_modules/.test(resource); }, }) diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js index 1a1b0042b7e..9fe78cbc0fa 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6c.input.js @@ -7,7 +7,7 @@ module.exports = { new webpack.optimize.CommonsChunkPlugin({ name: "main", minChunks: function ({ resource }) { - var foo = "bar"; + // some code return /node_modules/.test(resource); }, }) diff --git a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js index 4d7763906a7..bab003418a4 100644 --- a/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js +++ b/packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-6d.input.js @@ -7,11 +7,8 @@ module.exports = { new webpack.optimize.CommonsChunkPlugin({ name: "main", minChunks: function ({ resource }) { - var foo = "bar"; if (foo) { return /node_modulesfoo/.test(resource); - } else if (foo === "something") { - return /node_modulesbar/.test(resource); } else { return /node_modulesbaz/.test(resource); } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index a657bfbb0f8..a64f420fc0c 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -72,8 +72,6 @@ export default function(j: IJSCodeshift, ast: INode): INode { createProperty(j, "name", chunkValue), ]); } else { - chunkCount++; - if (!Array.isArray(cacheGroup[chunkValue])) { cacheGroup[chunkValue] = []; } @@ -81,19 +79,20 @@ export default function(j: IJSCodeshift, ast: INode): INode { findRootNodesByName(j, ast, "entry").forEach( ({ value: { value: { properties: entries }} }, ): void => { - entries.forEach(({ key: { name: entryName }}): void => - entryName === chunkValue ? cacheGroup[chunkValue].push( + chunkCount = entries.length; + entries.forEach(({ key: { name: entryName }}): void => { + if (entryName === chunkValue) { + cacheGroup[chunkValue].push( createProperty(j, "test", entryName), - ) : null, - ); + ); + } + }); }); } }); break; case "name": - chunkCount++; - const nameKey = p.value.value; if (nameKey === "runtime") { @@ -110,11 +109,14 @@ export default function(j: IJSCodeshift, ast: INode): INode { findRootNodesByName(j, ast, "entry").forEach( ({ value: { value: { properties: entries }} }, ): void => { - entries.forEach(({ key: { name: entryName }}): void => - entryName === nameKey ? cacheGroup[nameKey].push( + chunkCount = entries.length; + entries.forEach(({ key: { name: entryName }}): void => { + if (entryName === nameKey) { + cacheGroup[nameKey].push( createProperty(j, "test", entryName), - ) : null, - ); + ); + } + }); }); } break; @@ -136,7 +138,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { break; case "minSize": - splitChunksProps.push( + cacheGroupsProps.push( j.property("init", createIdentifierOrLiteral(j, propKey), p.value), ); break; @@ -152,13 +154,9 @@ export default function(j: IJSCodeshift, ast: INode): INode { if (!Array.isArray(cacheGroup[chunkKey])) { cacheGroup[chunkKey] = []; } - cacheGroup[chunkKey].push( - j.property( - "init", - createIdentifierOrLiteral(j, "test"), - pathValue, - ), - ); + + cacheGroup[chunkKey] = cacheGroup[chunkKey].map((prop) => + prop.key.name === "test" ? mergeTestPropArrowFunction(j, chunkKey, pathValue) : prop); } break; } @@ -170,19 +168,20 @@ export default function(j: IJSCodeshift, ast: INode): INode { createProperty(j, "name", chunkName), ]; - if (isAsyncChunk) { - chunkProps.push(j.property( - "init", - createIdentifierOrLiteral(j, "minChunks"), - createIdentifierOrLiteral(j, chunkCount), - )); - } else { - chunkProps.push(...commonCacheGroupsProps, + const chunkPropsToAdd = []; + + if (!isAsyncChunk) { + chunkProps.push(...commonCacheGroupsProps); + } + + if (chunkCount > 0) { + chunkProps.push( j.property( - "init", - createIdentifierOrLiteral(j, "minChunks"), - createIdentifierOrLiteral(j, chunkCount), - )); + "init", + createIdentifierOrLiteral(j, "minChunks"), + createIdentifierOrLiteral(j, chunkCount), + ), + ); } if ( @@ -250,3 +249,58 @@ export default function(j: IJSCodeshift, ast: INode): INode { return ast; } + +// merge test entry prop and function expression. case 6[x] +const mergeTestPropArrowFunction = (j, chunkKey, testFunc) => { + return j.property( + "init", + createIdentifierOrLiteral(j, "test"), + j.arrowFunctionExpression( + [j.identifier("module")], + j.blockStatement([ + j.ifStatement( + j.callExpression( + j.memberExpression( + j.callExpression( + j.memberExpression( + j.identifier("module"), + j.identifier("getChunks"), + ), + [], + ), + j.identifier("some"), + false, + ), + [j.arrowFunctionExpression( + [j.identifier("chunk")], + j.binaryExpression( + "===", + j.memberExpression( + j.identifier("chunk"), + j.identifier("name"), + ), + j.literal(chunkKey), + ), + )], + ), + j.returnStatement( + j.literal(true), + ), + ), + j.variableDeclaration( + "const", + [j.variableDeclarator( + j.identifier("fn"), + testFunc, + )], + ), + j.returnStatement( + j.callExpression( + j.identifier("fn"), + [j.identifier("module")], + ), + ), + ]), + ), + ); +}; diff --git a/packages/migrate/types/NodePath.ts b/packages/migrate/types/NodePath.ts index d082ddc6587..e40bdf3cba8 100644 --- a/packages/migrate/types/NodePath.ts +++ b/packages/migrate/types/NodePath.ts @@ -71,6 +71,12 @@ export interface IJSCodeshift extends Object { property?: (type: string, key: INode, value: INode) => INode; program?: (nodes: INode[]) => INode; booleanLiteral?: (bool: boolean) => INode; + arrowFunctionExpression?: (params: INode[], body: INode, exp: INode) => INode; + blockStatement?: (body: INode[]) => INode; + ifStatement?: (test: INode, consequent: INode, alternate?: INode) => INode; + returnStatement?: (arg: INode) => INode; + binaryExpression?: (operator: string, left: INode, right: INode) => INode; + Property?: IExpressionObject; NewExpression?: IExpressionObject; CallExpression?: IExpressionObject; @@ -83,6 +89,7 @@ export interface IJSCodeshift extends Object { ObjectExpression?: IExpressionObject; BlockStatement?: IExpressionObject; Program?: IExpressionObject; + ArrowFunctionExpression?: IExpressionObject; filters?: { VariableDeclarator: { requiresModule: Function, From b68a7d06a1d16057228ecbc03902dc13383f3570 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Mon, 20 Aug 2018 14:40:23 +0530 Subject: [PATCH 14/16] feat(migrate): CCP to SCP - fix minSize issue --- .../__snapshots__/commonsChunkPlugin.test.js.snap | 7 +++---- packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index 8278a1ea76e..cccd24645f5 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -112,10 +112,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da optimizations: { splitChunks: { chunks: 'async', + minSize: 2000, cacheGroups: { - minSize: 2000, - main: { name: 'main', minChunks: 1, @@ -298,9 +297,9 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da optimizations: { splitChunks: { - cacheGroups: { - minSize: 3000, + minSize: 3000, + cacheGroups: { main: { name: 'main', chunks: 'initial', diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index a64f420fc0c..bf8950fe45b 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -138,7 +138,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { break; case "minSize": - cacheGroupsProps.push( + splitChunksProps.push( j.property("init", createIdentifierOrLiteral(j, propKey), p.value), ); break; From e404f3b6d91e3f55cb9f88d80971c0a1237d8dbf Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Mon, 20 Aug 2018 17:23:34 +0530 Subject: [PATCH 15/16] feat(migrate): CCP to SCP - fix minChunks, test issue --- .../commonsChunkPlugin.test.js.snap | 14 +------------ .../commonsChunkPlugin/commonsChunkPlugin.ts | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index cccd24645f5..c9b1ec5c332 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -14,7 +14,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da name: 'app', chunks: 'initial', enforce: true, - minChunks: 2, test: 'app' }, @@ -22,7 +21,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-0" da name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 2, test: 'vendor' } } @@ -44,15 +42,13 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-1" da common: { name: 'common', chunks: 'initial', - enforce: true, - minChunks: 1 + enforce: true }, vendor: { name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 1, test: 'vendor' } } @@ -75,7 +71,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-2" da name: 'vendor', chunks: 'initial', enforce: true, - minChunks: 1, test: 'vendor' } } @@ -117,7 +112,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da cacheGroups: { main: { name: 'main', - minChunks: 1, test: 'main' } } @@ -140,7 +134,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-5" da name: 'main', chunks: 'initial', enforce: true, - minChunks: 1, test: 'main' } } @@ -167,7 +160,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6a" d name: 'main', chunks: 'initial', enforce: true, - minChunks: 1, test: module => { if (module.getChunks().some(chunk => chunk.name === 'main')) @@ -197,7 +189,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6b" d name: 'main', chunks: 'initial', enforce: true, - minChunks: 1, test: module => { if (module.getChunks().some(chunk => chunk.name === 'main')) @@ -231,7 +222,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6c" d name: 'main', chunks: 'initial', enforce: true, - minChunks: 1, test: module => { if (module.getChunks().some(chunk => chunk.name === 'main')) @@ -265,7 +255,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-6d" d name: 'main', chunks: 'initial', enforce: true, - minChunks: 1, test: module => { if (module.getChunks().some(chunk => chunk.name === 'main')) @@ -304,7 +293,6 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da name: 'main', chunks: 'initial', enforce: true, - minChunks: 1, test: 'main' } } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index bf8950fe45b..c91e51fe305 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -164,17 +164,17 @@ export default function(j: IJSCodeshift, ast: INode): INode { ); Object.keys(cacheGroup).forEach((chunkName: string): void => { - const chunkProps: INode[] = [ + let chunkProps: INode[] = [ createProperty(j, "name", chunkName), ]; - const chunkPropsToAdd = []; + const chunkPropsToAdd = cacheGroup[chunkName]; if (!isAsyncChunk) { chunkProps.push(...commonCacheGroupsProps); } - if (chunkCount > 0) { + if (chunkCount > 1) { chunkProps.push( j.property( "init", @@ -184,12 +184,18 @@ export default function(j: IJSCodeshift, ast: INode): INode { ); } + const chunkPropsKeys = chunkPropsToAdd.some((prop) => prop.key.name === "test" && prop.value.type === "Literal"); + + if (chunkPropsKeys) { + chunkProps = chunkProps.filter((prop) => prop.key.name !== "minChunks"); + } + if ( - cacheGroup[chunkName] && - Array.isArray(cacheGroup[chunkName]) && - cacheGroup[chunkName].length > 0 + chunkPropsToAdd && + Array.isArray(chunkPropsToAdd) && + chunkPropsToAdd.length > 0 ) { - chunkProps.push(...cacheGroup[chunkName]); + chunkProps.push(...chunkPropsToAdd); } cacheGroups.push( From 2d63478f8e1d9fa8e084258cbaca20c68920fc91 Mon Sep 17 00:00:00 2001 From: dhruvdutt Date: Tue, 21 Aug 2018 01:55:19 +0530 Subject: [PATCH 16/16] feat(migrate): CCP to SCP - fix minSize, async chunk --- .../commonsChunkPlugin.test.js.snap | 13 ++++----- .../commonsChunkPlugin/commonsChunkPlugin.ts | 29 ++++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap index c9b1ec5c332..edc282c874b 100644 --- a/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap +++ b/packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap @@ -106,13 +106,13 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-4" da optimizations: { splitChunks: { - chunks: 'async', - minSize: 2000, - cacheGroups: { main: { name: 'main', - test: 'main' + enforce: true, + test: 'main', + chunks: 'async', + minSize: 2000 } } } @@ -286,14 +286,13 @@ exports[`commonsChunkPlugin transforms correctly using "commonsChunkPlugin-7" da optimizations: { splitChunks: { - minSize: 3000, - cacheGroups: { main: { name: 'main', chunks: 'initial', enforce: true, - test: 'main' + test: 'main', + minSize: 3000 } } } diff --git a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts index c91e51fe305..ea86fb371ba 100644 --- a/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts +++ b/packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts @@ -40,7 +40,6 @@ export default function(j: IJSCodeshift, ast: INode): INode { // cache group options based on keys let cacheGroup: object = {}; let cacheGroups: INode[] = []; - let isAsyncChunk: boolean = false; // iterate each CommonsChunkPlugin instance CommonsChunkPlugin.forEach( @@ -50,7 +49,7 @@ export default function(j: IJSCodeshift, ast: INode): INode { // reset chunks from old props cacheGroup = {}; cacheGroups = []; - isAsyncChunk = false; + commonCacheGroupsProps = [ createProperty(j, "chunks", "initial"), createProperty(j, "enforce", true), @@ -133,12 +132,19 @@ export default function(j: IJSCodeshift, ast: INode): INode { break; case "async": - splitChunksProps.push(createProperty(j, "chunks", "async")); - isAsyncChunk = true; + if (!Array.isArray(cacheGroup[chunkKey])) { + cacheGroup[chunkKey] = []; + } + cacheGroup[chunkKey].push( + createProperty(j, "chunks", "async"), + ); break; case "minSize": - splitChunksProps.push( + if (!Array.isArray(cacheGroup[chunkKey])) { + cacheGroup[chunkKey] = []; + } + cacheGroup[chunkKey].push( j.property("init", createIdentifierOrLiteral(j, propKey), p.value), ); break; @@ -169,10 +175,12 @@ export default function(j: IJSCodeshift, ast: INode): INode { ]; const chunkPropsToAdd = cacheGroup[chunkName]; + const chunkPropsKeys = chunkPropsToAdd.map((prop) => prop.key.name); - if (!isAsyncChunk) { - chunkProps.push(...commonCacheGroupsProps); - } + commonCacheGroupsProps = + commonCacheGroupsProps.filter((commonProp) => !chunkPropsKeys.includes(commonProp.key.name)); + + chunkProps.push(...commonCacheGroupsProps); if (chunkCount > 1) { chunkProps.push( @@ -184,9 +192,10 @@ export default function(j: IJSCodeshift, ast: INode): INode { ); } - const chunkPropsKeys = chunkPropsToAdd.some((prop) => prop.key.name === "test" && prop.value.type === "Literal"); + const chunkPropsContainTest = + chunkPropsToAdd.some((prop) => prop.key.name === "test" && prop.value.type === "Literal"); - if (chunkPropsKeys) { + if (chunkPropsContainTest) { chunkProps = chunkProps.filter((prop) => prop.key.name !== "minChunks"); }