Skip to content

Commit

Permalink
chore(migrate): add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvdutt committed Jul 13, 2018
1 parent 90909e4 commit 82a7dec
Show file tree
Hide file tree
Showing 31 changed files with 1,499 additions and 1,188 deletions.
1,063 changes: 482 additions & 581 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/add/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions packages/generators/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/info/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions packages/init/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/migrate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*.js
/**/*.js
!*.test.js
!/**/*.test.js
!/**/__testfixtures__/*.js
!/**/__snapshots__/*.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const utils = require("@webpack-cli/utils/ast-utils");
import * as utils from "@webpack-cli/utils/ast-utils";

import { IJSCodeshift, INode } from "../types/NodePath";

/**
*
Expand All @@ -10,11 +12,11 @@ const utils = require("@webpack-cli/utils/ast-utils");
* @returns {Node} ast - jscodeshift ast
*/

module.exports = function(j, ast) {
export default function(j: IJSCodeshift, ast: INode): INode {
return utils
.findPluginsByName(j, ast, ["webpack.BannerPlugin"])
.forEach(path => {
const args = path.value.arguments; // any node
.forEach((path: INode): void => {
const args: INode[] = path.value.arguments; // any node
// If the first argument is a literal replace it with object notation
// See https://webpack.js.org/guides/migrating/#bannerplugin-breaking-change
if (args && args.length > 1 && args[0].type === j.Literal.name) {
Expand All @@ -25,9 +27,9 @@ module.exports = function(j, ast) {
path.parent,
"webpack.BannerPlugin",
{
banner: args[0].value
}
banner: args[0].value,
},
);
}
});
};
}
58 changes: 0 additions & 58 deletions packages/migrate/extractTextPlugin/extractTextPlugin.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";

const defineTest = require("@webpack-cli/utils//defineTest");
const defineTest = require("@webpack-cli/utils/defineTest");

defineTest(__dirname, "extractTextPlugin");
60 changes: 60 additions & 0 deletions packages/migrate/extractTextPlugin/extractTextPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as utils from "@webpack-cli/utils/ast-utils";

import { IJSCodeshift, INode } from "../types/NodePath";

/**
*
* Check whether `node` is the invocation of the plugin denoted by `pluginName`
*
* @param {Object} j - jscodeshift top-level import
* @param {Path} node - ast node to check
* @param {String} pluginName - name of the plugin
* @returns {Boolean} isPluginInvocation - whether `node` is the invocation of the plugin denoted by `pluginName`
*/

function findInvocation(j: IJSCodeshift, path: INode, pluginName: string): boolean {
let found: boolean = false;
found =
j(path)
.find(j.MemberExpression)
.filter((p: INode): boolean => p.get("object").value.name === pluginName)
.size() > 0;
return found;
}

/**
*
* Transform for ExtractTextPlugin arguments. Consolidates arguments into single options object.
*
* @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): void | INode {
const changeArguments = (path: INode): INode => {
const args: INode[] = path.value.arguments;

const literalArgs: INode[] = args.filter((p: INode): boolean => utils.isType(p, "Literal"));
if (literalArgs && literalArgs.length > 1) {
const newArgs: object = j.objectExpression(
literalArgs.map((p: INode, index: number): INode =>
utils.createProperty(j, index === 0 ? "fallback" : "use", p.value),
),
);
path.value.arguments = [newArgs];
}
return path;
};
const name: string = utils.findVariableToPlugin(
j,
ast,
"extract-text-webpack-plugin",
);
if (!name) { return ast; }

return ast
.find(j.CallExpression)
.filter((p: INode): boolean => findInvocation(j, p, name))
.forEach(changeArguments);
}
Loading

0 comments on commit 82a7dec

Please sign in to comment.