-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
bannerPlugin.ts
35 lines (32 loc) · 1.08 KB
/
bannerPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import * as utils from "@webpack-cli/utils/ast-utils";
import { IJSCodeshift, INode } from "../types/NodePath";
/**
*
* Transform for BannerPlugin arguments. Consolidates first and second argument (if
* both are present) 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): INode {
return utils
.findPluginsByName(j, ast, ["webpack.BannerPlugin"])
.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) {
// and remove the first argument
path.value.arguments = [path.value.arguments[1]];
utils.createOrUpdatePluginByName(
j,
path.parent,
"webpack.BannerPlugin",
{
banner: args[0].value,
},
);
}
});
}