|
| 1 | +import { |
| 2 | + createProperty, |
| 3 | + findPluginsArrayAndRemoveIfEmpty, |
| 4 | + findPluginsByName, |
| 5 | + getRequire, |
| 6 | + safeTraverse, |
| 7 | +} from "@webpack-cli/utils/ast-utils"; |
| 8 | + |
| 9 | +import { IJSCodeshift, INode } from "../types/NodePath"; |
| 10 | + |
| 11 | +/** |
| 12 | + * |
| 13 | + * Transform which: |
| 14 | + * Removes UglifyWebpackPlugin from plugins array, if no options is passed to the plugin. |
| 15 | + * and adds `optimization.minimize: true` to config |
| 16 | + * |
| 17 | + * If any configuration is passed to UglifyWebpackPlugin |
| 18 | + * plugin instantiation is moved to `optimization.minimizer`. |
| 19 | + * |
| 20 | + * @param {Object} j - jscodeshift top-level import |
| 21 | + * @param {Node} ast - jscodeshift ast to transform |
| 22 | + * @returns {Node} ast - jscodeshift ast |
| 23 | + */ |
| 24 | + |
| 25 | +export default function(j: IJSCodeshift, ast: INode): INode { |
| 26 | + |
| 27 | + let pluginVariableAssignment: string = null; |
| 28 | + |
| 29 | + const searchForRequirePlugin: INode = ast |
| 30 | + .find(j.VariableDeclarator) |
| 31 | + .filter( |
| 32 | + j.filters.VariableDeclarator.requiresModule("uglifyjs-webpack-plugin"), |
| 33 | + ); |
| 34 | + |
| 35 | + /** |
| 36 | + * Look for a variable declaration which requires uglifyjs-webpack-plugin |
| 37 | + * saves the name of this variable. |
| 38 | + */ |
| 39 | + searchForRequirePlugin.forEach((node: INode): void => { |
| 40 | + pluginVariableAssignment = node.value.id.name; |
| 41 | + }); |
| 42 | + |
| 43 | + pluginVariableAssignment = !pluginVariableAssignment |
| 44 | + ? "webpack.optimize.UglifyJsPlugin" |
| 45 | + : pluginVariableAssignment; |
| 46 | + |
| 47 | + findPluginsByName(j, ast, [pluginVariableAssignment]) |
| 48 | + .forEach((node: INode): void => { |
| 49 | + let expressionContent: object = null; |
| 50 | + |
| 51 | + const configBody: INode = safeTraverse(node, ["parent", "parent", "parent"]); |
| 52 | + |
| 53 | + // options passed to plugin |
| 54 | + const pluginOptions: INode[] = node.value.arguments; |
| 55 | + |
| 56 | + /** |
| 57 | + * check if there are any options passed to UglifyWebpackPlugin |
| 58 | + * If so, they are moved to optimization.minimizer. |
| 59 | + * Otherwise, rely on default options |
| 60 | + */ |
| 61 | + if (pluginOptions.length) { |
| 62 | + /* |
| 63 | + * If user is using UglifyJsPlugin directly from webpack |
| 64 | + * transformation must: |
| 65 | + * - remove it |
| 66 | + * - add require for uglify-webpack-plugin |
| 67 | + * - add to minimizer |
| 68 | + */ |
| 69 | + if (pluginVariableAssignment && pluginVariableAssignment.includes("webpack")) { |
| 70 | + // create require for uglify-webpack-plugin |
| 71 | + const pathRequire: INode = getRequire( |
| 72 | + j, |
| 73 | + "UglifyJsPlugin", |
| 74 | + "uglifyjs-webpack-plugin", |
| 75 | + ); |
| 76 | + // append to source code. |
| 77 | + ast |
| 78 | + .find(j.Program) |
| 79 | + .replaceWith((p: INode): INode => |
| 80 | + j.program([].concat(pathRequire).concat(p.value.body)), |
| 81 | + ); |
| 82 | + |
| 83 | + expressionContent = j.property( |
| 84 | + "init", |
| 85 | + j.identifier("minimizer"), |
| 86 | + j.arrayExpression([ |
| 87 | + j.newExpression(j.identifier("UglifyJsPlugin"), [pluginOptions[0]]), |
| 88 | + ]), |
| 89 | + ); |
| 90 | + } else { |
| 91 | + expressionContent = j.property( |
| 92 | + "init", |
| 93 | + j.identifier("minimizer"), |
| 94 | + j.arrayExpression([node.value]), |
| 95 | + ); |
| 96 | + } |
| 97 | + } else { |
| 98 | + searchForRequirePlugin.forEach((n: INode): void => j(n).remove()); |
| 99 | + } |
| 100 | + |
| 101 | + const minimizeProperty = createProperty(j, "minimize", "true"); |
| 102 | + // creates optimization property at the body of the config. |
| 103 | + if (expressionContent) { |
| 104 | + configBody.value.properties.push( |
| 105 | + j.property( |
| 106 | + "init", |
| 107 | + j.identifier("optimization"), |
| 108 | + j.objectExpression([minimizeProperty, expressionContent]), |
| 109 | + ), |
| 110 | + ); |
| 111 | + } else { |
| 112 | + configBody.value.properties.push( |
| 113 | + j.property( |
| 114 | + "init", |
| 115 | + j.identifier("optimization"), |
| 116 | + j.objectExpression([minimizeProperty]), |
| 117 | + ), |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + // remove the old Uglify plugin from Plugins array. |
| 122 | + j(node).remove(); |
| 123 | + }); |
| 124 | + |
| 125 | + findPluginsArrayAndRemoveIfEmpty(j, ast); |
| 126 | + |
| 127 | + return ast; |
| 128 | +} |
0 commit comments