Skip to content

Commit 82a7dec

Browse files
committed
chore(migrate): add typescript support
1 parent 90909e4 commit 82a7dec

File tree

31 files changed

+1499
-1188
lines changed

31 files changed

+1499
-1188
lines changed

package-lock.json

Lines changed: 482 additions & 581 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/add/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/generators/package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/info/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/init/package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/migrate/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*.js
2+
/**/*.js
3+
!*.test.js
4+
!/**/*.test.js
5+
!/**/__testfixtures__/*.js
6+
!/**/__snapshots__/*.js
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const utils = require("@webpack-cli/utils/ast-utils");
1+
import * as utils from "@webpack-cli/utils/ast-utils";
2+
3+
import { IJSCodeshift, INode } from "../types/NodePath";
24

35
/**
46
*
@@ -10,11 +12,11 @@ const utils = require("@webpack-cli/utils/ast-utils");
1012
* @returns {Node} ast - jscodeshift ast
1113
*/
1214

13-
module.exports = function(j, ast) {
15+
export default function(j: IJSCodeshift, ast: INode): INode {
1416
return utils
1517
.findPluginsByName(j, ast, ["webpack.BannerPlugin"])
16-
.forEach(path => {
17-
const args = path.value.arguments; // any node
18+
.forEach((path: INode): void => {
19+
const args: INode[] = path.value.arguments; // any node
1820
// If the first argument is a literal replace it with object notation
1921
// See https://webpack.js.org/guides/migrating/#bannerplugin-breaking-change
2022
if (args && args.length > 1 && args[0].type === j.Literal.name) {
@@ -25,9 +27,9 @@ module.exports = function(j, ast) {
2527
path.parent,
2628
"webpack.BannerPlugin",
2729
{
28-
banner: args[0].value
29-
}
30+
banner: args[0].value,
31+
},
3032
);
3133
}
3234
});
33-
};
35+
}

packages/migrate/extractTextPlugin/extractTextPlugin.js

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
22

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

55
defineTest(__dirname, "extractTextPlugin");
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as utils from "@webpack-cli/utils/ast-utils";
2+
3+
import { IJSCodeshift, INode } from "../types/NodePath";
4+
5+
/**
6+
*
7+
* Check whether `node` is the invocation of the plugin denoted by `pluginName`
8+
*
9+
* @param {Object} j - jscodeshift top-level import
10+
* @param {Path} node - ast node to check
11+
* @param {String} pluginName - name of the plugin
12+
* @returns {Boolean} isPluginInvocation - whether `node` is the invocation of the plugin denoted by `pluginName`
13+
*/
14+
15+
function findInvocation(j: IJSCodeshift, path: INode, pluginName: string): boolean {
16+
let found: boolean = false;
17+
found =
18+
j(path)
19+
.find(j.MemberExpression)
20+
.filter((p: INode): boolean => p.get("object").value.name === pluginName)
21+
.size() > 0;
22+
return found;
23+
}
24+
25+
/**
26+
*
27+
* Transform for ExtractTextPlugin arguments. Consolidates arguments into single options object.
28+
*
29+
* @param {Object} j - jscodeshift top-level import
30+
* @param {Node} ast - jscodeshift ast to transform
31+
* @returns {Node} ast - jscodeshift ast
32+
*/
33+
34+
export default function(j: IJSCodeshift, ast: INode): void | INode {
35+
const changeArguments = (path: INode): INode => {
36+
const args: INode[] = path.value.arguments;
37+
38+
const literalArgs: INode[] = args.filter((p: INode): boolean => utils.isType(p, "Literal"));
39+
if (literalArgs && literalArgs.length > 1) {
40+
const newArgs: object = j.objectExpression(
41+
literalArgs.map((p: INode, index: number): INode =>
42+
utils.createProperty(j, index === 0 ? "fallback" : "use", p.value),
43+
),
44+
);
45+
path.value.arguments = [newArgs];
46+
}
47+
return path;
48+
};
49+
const name: string = utils.findVariableToPlugin(
50+
j,
51+
ast,
52+
"extract-text-webpack-plugin",
53+
);
54+
if (!name) { return ast; }
55+
56+
return ast
57+
.find(j.CallExpression)
58+
.filter((p: INode): boolean => findInvocation(j, p, name))
59+
.forEach(changeArguments);
60+
}

0 commit comments

Comments
 (0)