Skip to content

Commit

Permalink
feat(migrate): commonChunksPlugin to SplitChunksPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvdutt committed Aug 1, 2018
1 parent 72c70f2 commit 0b8e51e
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 0 deletions.
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
}
}
}
}
"
`;
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,8 @@
module.export = {
plugins: [
new webpack.CommonsChunkPlugin({
filename: "commons.js",
name: "commons"
})
]
}
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
})
]
}
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 packages/migrate/commonsChunkPlugin/commonsChunkPlugin.ts
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: 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;
}
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 0b8e51e

Please sign in to comment.