> For the complete documentation index, see [llms.txt](https://mhy.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mhy.js.org/recipes-and-examples/package.json-overrides.md).

# package.json overrides

Using `json-merger`'s syntax you have powerful ways to customize configs without *JavaScript* directly in your `package.json` files. There can be some really exotic cases tho. This is a place to collect some of these solutions.

All samples should be placed inside `{ "mhy": { HERE } }` in your `package.json` file.

## Extend `.npmignore` with custom file names

```json
"npmignore": {
  "root": {
    "$concat": [
      "/test",
      "/ci.sh",
      "/.gitbook"
    ]
  }
}
```

## `babel`: Deep customization

> Note: Babel support was replaced by SWC, but the example is still helpful.

1. Change `preset-env` to produce `commonjs` modules
2. Add `module.exports` statements to each module for default exports (from Babel v7 it is not being added anymore by default)
3. Remove `regenerator-plugin`, it doesn't needed in `node`.

```json
"babel": {
  "production": {
    "presets": [
      {
        "$match": {
          "query": "$[?(@[0].includes('preset-env'))]",
          "value": [
            {
              "$match": {
                "index": 1,
                "value": {
                  "modules": "commonjs",
                  "targets": {
                    "node": true,
                    "esmodules": true
                  }
                }
              }
            }
          ]
        }
      }
    ],
    "plugins": [
      {
        "$prepend": {
          "$expression": "$params.require.resolve('babel-plugin-add-module-exports')"
        }
      },
      {
        "$match": {
          "query": "$[?(@.includes('regenerator'))]",
          "value": {
            "$remove": true
          }
        }
      }
    ]
  }
}
```

## `Webpack`: Some customization

1. Generate `index.php` file instead of `index.html` (in this case `html-webpack-plugin` is the first in the plugins array).
2. Change regex for ignored modules from watch. This exact example is useful when working with locally linked npm modules and you want to recompile on it's changes also.

```json
"webpack": {
    "production": {
        "plugins": [{
            "$expression": "Object.assign($targetProperty.options, {filename : 'index.php'})"
        }]
    },
    "development": {
        "watchOptions": {
            "ignored": [
                "node_modules.(?!(my-module-.*)).*"
            ]
        }
    }
}
```

## `Jest`: Load extra environment setup file

```json
"jest": {
  "development": {
    "setupFilesAfterEnv": {
      "$concat": [
        "./test-setup.js"
      ]
    }
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mhy.js.org/recipes-and-examples/package.json-overrides.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
