Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fine-tools-repair.md
Original file line number Diff line number Diff line change
@@ -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`
54 changes: 54 additions & 0 deletions codemods/set-target-to-false-and-update-plugins/README.md
Original file line number Diff line number Diff line change
@@ -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)],
};
```
21 changes: 21 additions & 0 deletions codemods/set-target-to-false-and-update-plugins/codemod.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions codemods/set-target-to-false-and-update-plugins/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
42 changes: 42 additions & 0 deletions codemods/set-target-to-false-and-update-plugins/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -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<Js> = 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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
target: false,
plugins: [WebExtensionTarget(nodeConfig)],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
target: WebExtensionTarget(nodeConfig),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const WebExtensionTarget = require("webpack-extension-target");

module.exports = {
target: false,
plugins: [WebExtensionTarget(nodeConfig)],
mode: "development",
output: {
filename: "bundle.js",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const WebExtensionTarget = require("webpack-extension-target");

module.exports = {
target: WebExtensionTarget(nodeConfig),
mode: "development",
output: {
filename: "bundle.js",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
target: false,
plugins: [WebExtensionTarget(nodeConfig)],
optimization: {
splitChunks: {
chunks: "all",
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
target: WebExtensionTarget(nodeConfig),
optimization: {
splitChunks: {
chunks: "all",
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
target: false,
plugins: [CustomTargetFunction(config)],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
target: CustomTargetFunction(config),
};
23 changes: 23 additions & 0 deletions codemods/set-target-to-false-and-update-plugins/workflow.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.