-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
loaderOptionsPlugin.ts
61 lines (52 loc) · 1.4 KB
/
loaderOptionsPlugin.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import isEmpty = require("lodash/isEmpty");
import {
createOrUpdatePluginByName,
findPluginsByName,
safeTraverse,
} from "@webpack-cli/utils/ast-utils";
import { IJSCodeshift, INode } from "../types/NodePath";
interface ILoaderOptions {
debug?: boolean;
minimize?: boolean;
}
/**
*
* Transform which adds context information for old loaders by injecting a `LoaderOptionsPlugin`
*
* @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 {
const loaderOptions: ILoaderOptions = {};
// If there is debug: true, set debug: true in the plugin
if (ast.find(j.Identifier, { name: "debug" }).size()) {
loaderOptions.debug = true;
ast
.find(j.Identifier, { name: "debug" })
.forEach((p: INode): void => {
p.parent.prune();
});
}
// If there is UglifyJsPlugin, set minimize: true
if (findPluginsByName(j, ast, ["webpack.optimize.UglifyJsPlugin"]).size()) {
loaderOptions.minimize = true;
}
return ast
.find(j.ArrayExpression)
.filter(
(path: INode): boolean =>
safeTraverse(path, ["parent", "value", "key", "name"]) === "plugins",
)
.forEach((path: INode): void => {
if (!isEmpty(loaderOptions)) {
createOrUpdatePluginByName(
j,
path,
"webpack.LoaderOptionsPlugin",
loaderOptions,
);
}
});
}