From 7e8a07ddc22da58c7dec8649606089295433b934 Mon Sep 17 00:00:00 2001 From: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Date: Thu, 13 Aug 2026 12:01:55 +0200 Subject: [PATCH] feat(`set-target-to-false-and-update-plugins`): introduce --- .changeset/fine-tools-repair.md | 5 ++ .../README.md | 54 +++++++++++++++++++ .../codemod.yaml | 21 ++++++++ .../package.json | 12 +++++ .../src/workflow.ts | 42 +++++++++++++++ .../tests/test-1/expected.ts | 4 ++ .../tests/test-1/input.ts | 3 ++ .../tests/test-2/expected.ts | 10 ++++ .../tests/test-2/input.ts | 9 ++++ .../tests/test-3/expected.ts | 9 ++++ .../tests/test-3/input.ts | 8 +++ .../tests/test-4/expected.ts | 4 ++ .../tests/test-4/input.ts | 3 ++ .../workflow.yaml | 23 ++++++++ package-lock.json | 13 ++++- 15 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 .changeset/fine-tools-repair.md create mode 100644 codemods/set-target-to-false-and-update-plugins/README.md create mode 100644 codemods/set-target-to-false-and-update-plugins/codemod.yaml create mode 100644 codemods/set-target-to-false-and-update-plugins/package.json create mode 100644 codemods/set-target-to-false-and-update-plugins/src/workflow.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-1/expected.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-1/input.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-2/expected.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-2/input.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-3/expected.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-3/input.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-4/expected.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/tests/test-4/input.ts create mode 100644 codemods/set-target-to-false-and-update-plugins/workflow.yaml diff --git a/.changeset/fine-tools-repair.md b/.changeset/fine-tools-repair.md new file mode 100644 index 0000000..7d684d2 --- /dev/null +++ b/.changeset/fine-tools-repair.md @@ -0,0 +1,5 @@ +--- +"@webpack/set-target-to-false-and-update-plugins": major +--- + +Introduce the codemod, by copy pasting it here from `codemod/webpack-codemods` diff --git a/codemods/set-target-to-false-and-update-plugins/README.md b/codemods/set-target-to-false-and-update-plugins/README.md new file mode 100644 index 0000000..c341d21 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/README.md @@ -0,0 +1,54 @@ +# set-target-to-false-and-update-plugins + +This codemod migrates the `target` property in Webpack configurations from a function to `false` and moves the function to the `plugins` array. + +In Webpack 4, it was possible to set the `target` property to a function. However, in Webpack 5, this approach is no longer supported. Instead, the `target` should be set to `false`, and the function should be included in the `plugins` array. This codemod automates the transformation of Webpack configurations to adhere to the new specification. + +## Examples + +```diff +- module.exports = { +- target: WebExtensionTarget(nodeConfig), +- }; ++ module.exports = { ++ target: false, ++ plugins: [WebExtensionTarget(nodeConfig)], ++ }; +``` + +```diff + const WebExtensionTarget = require("webpack-extension-target"); + + module.exports = { +- target: WebExtensionTarget(nodeConfig), ++ target: false, ++ plugins: [WebExtensionTarget(nodeConfig)], + mode: "development", ++ + output: { + filename: "bundle.js", + }, + }; +``` + +```diff + module.exports = { +- target: WebExtensionTarget(nodeConfig), ++ target: false, ++ plugins: [WebExtensionTarget(nodeConfig)], ++ + optimization: { + splitChunks: { + chunks: "all", + }, + }, + }; +``` + +```diff + module.exports = { +- target: CustomTargetFunction(config), ++ target: false, ++ plugins: [CustomTargetFunction(config)], + }; +``` diff --git a/codemods/set-target-to-false-and-update-plugins/codemod.yaml b/codemods/set-target-to-false-and-update-plugins/codemod.yaml new file mode 100644 index 0000000..8759c08 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/codemod.yaml @@ -0,0 +1,21 @@ +schema_version: "1.0" +name: "@webpack/set-target-to-false-and-update-plugins" +version: 1.0.0 +description: Transform Webpack 'target' function to 'false' and move to 'plugins'. +author: akash-kumar-dev +license: MIT +workflow: workflow.yaml +repository: "https://github.com/webpack/codemods/tree/HEAD/codemods/set-target-to-false-and-update-plugins" +category: migration + +targets: + languages: + - javascript + - typescript + +keywords: + - webpack + +registry: + access: public + visibility: public diff --git a/codemods/set-target-to-false-and-update-plugins/package.json b/codemods/set-target-to-false-and-update-plugins/package.json new file mode 100644 index 0000000..fd12657 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/package.json @@ -0,0 +1,12 @@ +{ + "name": "@webpack/set-target-to-false-and-update-plugins", + "version": "1.0.0", + "description": "Transform Webpack 'target' function to 'false' and move to 'plugins'.", + "type": "module", + "scripts": { + "test": "npx codemod jssg test -l typescript ./src/workflow.ts" + }, + "devDependencies": { + "@codemod.com/jssg-types": "^1.6.3" + } +} diff --git a/codemods/set-target-to-false-and-update-plugins/src/workflow.ts b/codemods/set-target-to-false-and-update-plugins/src/workflow.ts new file mode 100644 index 0000000..2626218 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/src/workflow.ts @@ -0,0 +1,42 @@ +import type { Codemod, Edit } from "codemod:ast-grep"; +import type Js from "@codemod.com/jssg-types/langs/javascript"; + + +const transform: Codemod = async (root) => { + const rootNode = root.root(); + const edits: Edit[] = []; + + // Find all pairs in the code + const pairs = rootNode.findAll({ + rule: { + kind: "pair", + }, + }); + + for (const pair of pairs) { + const pairText = pair.text(); + + // Check if this is a target property with a call expression + if (pairText.startsWith("target:")) { + // Find call expressions in this pair + const callExpressions = pair.findAll({ + rule: { + kind: "call_expression", + }, + }); + + if (callExpressions.length > 0) { + const callExpressionText = callExpressions[0].text(); + + // Replace this pair with both target: false and plugins array + edits.push(pair.replace(`target: false,\n\tplugins: [${callExpressionText}]`)); + } + } + } + + if (!edits.length) return null; + + return rootNode.commitEdits(edits); +} + +export default transform; \ No newline at end of file diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-1/expected.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-1/expected.ts new file mode 100644 index 0000000..0e2a9ea --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-1/expected.ts @@ -0,0 +1,4 @@ +module.exports = { + target: false, + plugins: [WebExtensionTarget(nodeConfig)], +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-1/input.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-1/input.ts new file mode 100644 index 0000000..dd4386f --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-1/input.ts @@ -0,0 +1,3 @@ +module.exports = { + target: WebExtensionTarget(nodeConfig), +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-2/expected.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-2/expected.ts new file mode 100644 index 0000000..ac5f45f --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-2/expected.ts @@ -0,0 +1,10 @@ +const WebExtensionTarget = require("webpack-extension-target"); + +module.exports = { + target: false, + plugins: [WebExtensionTarget(nodeConfig)], + mode: "development", + output: { + filename: "bundle.js", + }, +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-2/input.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-2/input.ts new file mode 100644 index 0000000..10b3aee --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-2/input.ts @@ -0,0 +1,9 @@ +const WebExtensionTarget = require("webpack-extension-target"); + +module.exports = { + target: WebExtensionTarget(nodeConfig), + mode: "development", + output: { + filename: "bundle.js", + }, +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-3/expected.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-3/expected.ts new file mode 100644 index 0000000..10c13a3 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-3/expected.ts @@ -0,0 +1,9 @@ +module.exports = { + target: false, + plugins: [WebExtensionTarget(nodeConfig)], + optimization: { + splitChunks: { + chunks: "all", + }, + }, +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-3/input.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-3/input.ts new file mode 100644 index 0000000..d7a416c --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-3/input.ts @@ -0,0 +1,8 @@ +module.exports = { + target: WebExtensionTarget(nodeConfig), + optimization: { + splitChunks: { + chunks: "all", + }, + }, +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-4/expected.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-4/expected.ts new file mode 100644 index 0000000..54fd176 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-4/expected.ts @@ -0,0 +1,4 @@ +module.exports = { + target: false, + plugins: [CustomTargetFunction(config)], +}; diff --git a/codemods/set-target-to-false-and-update-plugins/tests/test-4/input.ts b/codemods/set-target-to-false-and-update-plugins/tests/test-4/input.ts new file mode 100644 index 0000000..5801287 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/tests/test-4/input.ts @@ -0,0 +1,3 @@ +module.exports = { + target: CustomTargetFunction(config), +}; diff --git a/codemods/set-target-to-false-and-update-plugins/workflow.yaml b/codemods/set-target-to-false-and-update-plugins/workflow.yaml new file mode 100644 index 0000000..44293b1 --- /dev/null +++ b/codemods/set-target-to-false-and-update-plugins/workflow.yaml @@ -0,0 +1,23 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json + +version: "1" + +nodes: + - id: apply-transforms + name: Apply AST Transformations + type: automatic + steps: + - name: Transform Webpack 'target' function to 'false' and move to 'plugins'. + js-ast-grep: + js_file: src/workflow.ts + base_path: . + include: + - "**/*.tsx" + - "**/*.ts" + - "**/*.jsx" + - "**/*.js" + - "**/*.cts" + - "**/*.mts" + exclude: + - "**/node_modules/**" + language: typescript diff --git a/package-lock.json b/package-lock.json index 3f0ce0f..5b563d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ }, "codemods/css-plugins-to-native-css": { "name": "@webpack/css-plugins-to-native-css", - "version": "0.0.0", + "version": "1.0.0", "license": "MIT", "dependencies": { "@webpack/codemod-utils": "*" @@ -32,6 +32,13 @@ "@codemod.com/jssg-types": "^1.6.2" } }, + "codemods/set-target-to-false-and-update-plugins": { + "name": "@webpack/set-target-to-false-and-update-plugins", + "version": "1.0.0", + "devDependencies": { + "@codemod.com/jssg-types": "^1.6.3" + } + }, "node_modules/@babel/runtime": { "version": "7.29.7", "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.7.tgz", @@ -1029,6 +1036,10 @@ "resolved": "codemods/css-plugins-to-native-css", "link": true }, + "node_modules/@webpack/set-target-to-false-and-update-plugins": { + "resolved": "codemods/set-target-to-false-and-update-plugins", + "link": true + }, "node_modules/acorn": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.18.0.tgz",