-
-
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
8 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
// 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' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
" | ||
`; | ||
|
||
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' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
" | ||
`; |
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"] | ||
}) | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
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,7 @@ | ||
"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"); |
115 changes: 115 additions & 0 deletions
115
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,115 @@ | ||
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 | ||
findPluginsByName(j, ast, ["webpack.commonsChunkPlugin"]) | ||
.forEach((path: INode): void => { | ||
pluginProps = path.value.arguments[0].properties; // any node | ||
}); | ||
|
||
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": | ||
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 options and map new object | ||
pluginProps.forEach((p: INode) => { | ||
if (p.key.name === "names") { | ||
p.value.elements.forEach((chunkName) => { | ||
cacheGroupsArray.push( | ||
createChunkCache(chunkName), | ||
); | ||
}); | ||
} | ||
}); | ||
|
||
// 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