Skip to content

Commit

Permalink
feat(migrate): commonChunksPlugin to SplitChunksPlugin
Browse files Browse the repository at this point in the history
ISSUES CLOSED: webpack#393
  • Loading branch information
dhruvdutt committed Jul 31, 2018
1 parent 72c70f2 commit e46a6b5
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 0 deletions.
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'
}
}
}
}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style = space
indent_size = 4
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
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.export = {
plugins: [
new webpack.commonsChunkPlugin({
names: ["common", "vendor"]
})
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.export = {
plugins: [
new webpack.commonsChunkPlugin({
names: ["vendor"]
})
]
}
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");
119 changes: 119 additions & 0 deletions packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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
});

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;
}
3 changes: 3 additions & 0 deletions packages/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -15,6 +16,7 @@ import uglifyJsPluginTransform from "./uglifyJsPlugin/uglifyJsPlugin";

interface ITransformsObject {
bannerPluginTransform: object;
commonsChunkPluginTransform?: object;
extractTextPluginTransform: object;
loaderOptionsPluginTransform: object;
loadersTransform: object;
Expand All @@ -36,6 +38,7 @@ const transformsObject: ITransformsObject = {
extractTextPluginTransform,
noEmitOnErrorsPluginTransform,
removeDeprecatedPluginsTransform,
commonsChunkPluginTransform,
};
/* tslint:enable object-literal-sort-keys */

Expand Down

0 comments on commit e46a6b5

Please sign in to comment.