{"id":19555481,"url":"https://github.com/maxzz/rollup-plugin-replace-regex","last_synced_at":"2026-06-15T15:32:02.710Z","repository":{"id":184330294,"uuid":"671281707","full_name":"maxzz/rollup-plugin-replace-regex","owner":"maxzz","description":"This is a modified copy of @rollup/plugin-replace with the ability to match regular expression strings and process conditional comment blocks.","archived":false,"fork":false,"pushed_at":"2023-08-19T21:47:14.000Z","size":145,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-04T13:54:06.075Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/rollup-plugin-replace-regex","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxzz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-27T01:10:25.000Z","updated_at":"2023-08-01T02:59:41.000Z","dependencies_parsed_at":"2025-09-26T23:31:23.311Z","dependency_job_id":null,"html_url":"https://github.com/maxzz/rollup-plugin-replace-regex","commit_stats":null,"previous_names":["maxzz/rollup-plugin-replace-regex"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/maxzz/rollup-plugin-replace-regex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxzz%2Frollup-plugin-replace-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxzz%2Frollup-plugin-replace-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxzz%2Frollup-plugin-replace-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxzz%2Frollup-plugin-replace-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxzz","download_url":"https://codeload.github.com/maxzz/rollup-plugin-replace-regex/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxzz%2Frollup-plugin-replace-regex/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34369840,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-15T02:00:07.085Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T04:34:13.825Z","updated_at":"2026-06-15T15:32:02.622Z","avatar_url":"https://github.com/maxzz.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rollup-plugin-replace-regex\n\nThis is a modified copy of @rollup/plugin-replace with the ability to match regular expression strings and process conditional comment blocks.\n\n## Changes and additions to RollupReplaceOptions\n\nThe RollupReplaceOptions type is defined [here](./types/index.d.ts).\n\n### Regex values\n\nNew regexValues key to specify replacement pairs where the key can be a regular expression.\n\n  * *key* may contain regex expression (except regex group definitions)\n    * regex keys ignore the boundaries option, \n      but boundaries can be added as part of the regex manually\n  * *value* is a string or function that gets\n    * file id\n    * matched expression by regex key\n    * original matched regex key from regexValues\n\n### Conditional comments\n\n* comments\n\n   Special source code comment marks are converted to:\n    | source | result |\n    | ---    |---     |\n    | /*{}*/            | // |\n    | /*[condition]{}*/ | //[condition]{}*/|\n    | /*[condition]{*/  | /*[condition]{*/ and line comments \n    | |until the next closing mark|\n    | /*[condition]}*/  | /*[condition]}*/|\n    | /*[condition]}*/  | /*[condition]}*/|\n\n    It is possible to specify multiple conditions in a single line and the line won't be commneted only if all \n    of them are defined:\n    ```js \n    /* [aa]\u003c\u003e */ // This line adds 'aa' to allowed definitions\n    /* [bb]\u003c\u003e */ // This line adds 'bb' to allowed definitions\n    /*[aa]{}*/ /*[bb]{}*/ 'aa and bb are defined' \n    ```\n\n  If commentsForRelease option is true then all conditional blocks will be commented \n  regardless defined conditions\n\n* conditions\n  * additional conditions for comments\n\n* commentsForRelease\n\n* verbose\n  * print matching and comments process results.\n\n### Execution order\n\n  rollup-plugin-replace-regex supports three diffent modes: \n   * values replace\n   * values with regular expressions\n   * comments\n\n  comments operation is executed before values replace. If you need opposite then \n  call rollup-plugin-replace-regex plugin twice:\n\n  ```js\n    plugins: [\n        rollup-plugin-replace-regex({values: ...}),\n        rollup-plugin-replace-regex({cooments: true}),\n    ]\n  ```\n\n## TODO \n    Add an option to specify when to run actions. Now we run all jobs on transform hook vs. orignal plugin does both transform and renderChunk hooks.\n\n# @rollup/plugin-replace\n\n🍣 A Rollup plugin which replaces targeted strings in files while bundling.\n\n[npm]: https://img.shields.io/npm/v/@rollup/plugin-replace\n[npm-url]: https://www.npmjs.com/package/@rollup/plugin-replace\n[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-replace\n[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-replace\n\n[![npm][npm]][npm-url]\n[![size][size]][size-url]\n[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)\n\n## Requirements\n\nThis plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.\n\n## Install\n\nUsing npm:\n\n```console\nnpm install @rollup/plugin-replace --save-dev\n```\n\n## Usage\n\nCreate a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:\n\n```js\nimport replace from '@rollup/plugin-replace';\n\nexport default {\n  input: 'src/index.js',\n  output: {\n    dir: 'output',\n    format: 'cjs'\n  },\n  plugins: [\n    replace({\n      'process.env.NODE_ENV': JSON.stringify('production'),\n      __buildDate__: () =\u003e JSON.stringify(new Date()),\n      __buildVersion: 15\n    })\n  ]\n};\n```\n\nThen call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).\n\nThe configuration above will replace every instance of `process.env.NODE_ENV` with `\"production\"` and `__buildDate__` with the result of the given function in any file included in the build.\n\n_Note: Values must be either primitives (e.g. string, number) or `function` that returns a string. For complex values, use `JSON.stringify`. To replace a target with a value that will be evaluated as a string, set the value to a quoted string (e.g. `\"test\"`) or use `JSON.stringify` to preprocess the target string safely._\n\nTypically, `@rollup/plugin-replace` should be placed in `plugins` _before_ other plugins so that they may apply optimizations, such as dead code removal.\n\n## Options\n\nIn addition to the properties and values specified for replacement, users may also specify the options below.\n\n### `delimiters`\n\nType: `Array[String, String]`\u003cbr\u003e\nDefault: `['\\\\b', '\\\\b(?!\\\\.)']`\n\nSpecifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html) and also prevent replacements of instances with nested access. See [Word Boundaries](#word-boundaries) below for more information.\nFor example, if you pass `typeof window` in `values` to-be-replaced, then you could expect the following scenarios:\n\n- `typeof window` **will** be replaced\n- `typeof window.document` **will not** be replaced due to `(?!\\.)` boundary\n- `typeof windowSmth` **will not** be replaced due to a `\\b` boundary\n\nDelimiters will be used to build a `Regexp`. To match special characters (any of `.*+?^${}()|[]\\`), be sure to [escape](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping) them.\n\n### `objectGuards`\n\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nWhen replacing dot-separated object properties like `process.env.NODE_ENV`, will also replace `typeof process` object guard\nchecks against the objects with the string `\"object\"`.\n\nFor example:\n\n```js\nreplace({\n  values: {\n    'process.env.NODE_ENV': '\"production\"'\n  }\n});\n```\n\n```js\n// Input\nif (typeof process !== 'undefined' \u0026\u0026 process.env.NODE_ENV === 'production') {\n  console.log('production');\n}\n// Without `objectGuards`\nif (typeof process !== 'undefined' \u0026\u0026 'production' === 'production') {\n  console.log('production');\n}\n// With `objectGuards`\nif ('object' !== 'undefined' \u0026\u0026 'production' === 'production') {\n  console.log('production');\n}\n```\n\n### `preventAssignment`\n\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nPrevents replacing strings where they are followed by a single equals sign. For example, where the plugin is called as follows:\n\n```js\nreplace({\n  values: {\n    'process.env.DEBUG': 'false'\n  }\n});\n```\n\nObserve the following code:\n\n```js\n// Input\nprocess.env.DEBUG = false;\nif (process.env.DEBUG == true) {\n  //\n}\n// Without `preventAssignment`\nfalse = false; // this throws an error because false cannot be assigned to\nif (false == true) {\n  //\n}\n// With `preventAssignment`\nprocess.env.DEBUG = false;\nif (false == true) {\n  //\n}\n```\n\n### `exclude`\n\nType: `String` | `Array[...String]`\u003cbr\u003e\nDefault: `null`\n\nA [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.\n\n### `include`\n\nType: `String` | `Array[...String]`\u003cbr\u003e\nDefault: `null`\n\nA [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.\n\n### `values`\n\nType: `{ [key: String]: Replacement }`, where `Replacement` is either a string or a `function` that returns a string.\nDefault: `{}`\n\nTo avoid mixing replacement strings with the other options, you can specify replacements in the `values` option. For example, the following signature:\n\n```js\nreplace({\n  include: ['src/**/*.js'],\n  changed: 'replaced'\n});\n```\n\nCan be replaced with:\n\n```js\nreplace({\n  include: ['src/**/*.js'],\n  values: {\n    changed: 'replaced'\n  }\n});\n```\n\n## Word Boundaries\n\nBy default, values will only match if they are surrounded by _word boundaries_.\n\nConsider the following options and build file:\n\n```js\nmodule.exports = {\n  ...\n  plugins: [replace({ changed: 'replaced' })]\n};\n```\n\n```js\n// file.js\nconsole.log('changed');\nconsole.log('unchanged');\n```\n\nThe result would be:\n\n```js\n// file.js\nconsole.log('replaced');\nconsole.log('unchanged');\n```\n\nTo ignore word boundaries and replace every instance of the string, wherever it may be, specify empty strings as delimiters:\n\n```js\nexport default {\n  ...\n  plugins: [\n    replace({\n      changed: 'replaced',\n      delimiters: ['', '']\n    })\n  ]\n};\n```\n\n## Meta\n\n[CONTRIBUTING](/.github/CONTRIBUTING.md)\n\n[LICENSE (MIT)](/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxzz%2Frollup-plugin-replace-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxzz%2Frollup-plugin-replace-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxzz%2Frollup-plugin-replace-regex/lists"}