-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(migrate): commonChunksPlugin to SplitChunksPlugin
ISSUES CLOSED: #393
- Loading branch information
Showing
10 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
packages/migrate/commonsChunkPlugin/__snapshots__/commonsChunkPlugin.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} | ||
" | ||
`; |
3 changes: 3 additions & 0 deletions
3
packages/migrate/commonsChunkPlugin/__testfixtures__/.editorconfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[*] | ||
indent_style = space | ||
indent_size = 4 |
8 changes: 8 additions & 0 deletions
8
packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-0.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.export = { | ||
plugins: [ | ||
new webpack.CommonsChunkPlugin({ | ||
names: ["common", "vendor"], | ||
minChunks: 2 | ||
}) | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-1.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.export = { | ||
plugins: [ | ||
new webpack.CommonsChunkPlugin({ | ||
names: ["common", "vendor"] | ||
}) | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-2.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.export = { | ||
plugins: [ | ||
new webpack.CommonsChunkPlugin({ | ||
names: ["vendor"] | ||
}) | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-3.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.export = { | ||
plugins: [ | ||
new webpack.CommonsChunkPlugin({ | ||
filename: "commons.js", | ||
name: "commons" | ||
}) | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/migrate/commonsChunkPlugin/__testfixtures__/commonsChunkPlugin-4.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.export = { | ||
plugins: [ | ||
new webpack.CommonsChunkPlugin({ | ||
name: "main", | ||
async: true, | ||
minSize: 0, | ||
minChunks: 2 | ||
}) | ||
] | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/migrate/commonsChunkPlugin/commonsChunkPlugin.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); |
147 changes: 147 additions & 0 deletions
147
packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = [ | ||
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) => { | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters