{"id":14976335,"url":"https://github.com/va1/string-replace-loader","last_synced_at":"2025-05-16T07:04:14.884Z","repository":{"id":1311822,"uuid":"42241160","full_name":"Va1/string-replace-loader","owner":"Va1","description":"Replace loader for Webpack","archived":false,"fork":false,"pushed_at":"2023-03-14T16:20:29.000Z","size":319,"stargazers_count":252,"open_issues_count":14,"forks_count":57,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-09T15:44:13.850Z","etag":null,"topics":["javascript","regexp","regular-expression","replace-in-files","replace-text","webpack","webpack-loader","webpack2"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Va1.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2015-09-10T11:43:22.000Z","updated_at":"2025-04-03T09:21:12.000Z","dependencies_parsed_at":"2023-07-05T20:31:21.168Z","dependency_job_id":null,"html_url":"https://github.com/Va1/string-replace-loader","commit_stats":{"total_commits":64,"total_committers":14,"mean_commits":4.571428571428571,"dds":0.6875,"last_synced_commit":"64a5e71860f7641a90695373157a884b992df27b"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Va1%2Fstring-replace-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Va1%2Fstring-replace-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Va1%2Fstring-replace-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Va1%2Fstring-replace-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Va1","download_url":"https://codeload.github.com/Va1/string-replace-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254485054,"owners_count":22078767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["javascript","regexp","regular-expression","replace-in-files","replace-text","webpack","webpack-loader","webpack2"],"created_at":"2024-09-24T13:53:44.508Z","updated_at":"2025-05-16T07:04:14.864Z","avatar_url":"https://github.com/Va1.png","language":"JavaScript","readme":"# Replace loader for [Webpack](http://webpack.github.io/)\n\nPerform replacements (plain and regular expression) in the contents loaded by the loader.\n\n## Install:\n\n```bash\n$ yarn add --dev string-replace-loader\n```\n\nWith release of 2.0.0 the loader is expected to be used in Node v4+ environment.\nSupport for Node v3 and lower was dropped, but you can install and use the loader version of 1.3.0 in older environments.\n\nWith release of 3.0.0 the loader is expected to be used with Webpack v5+.\nSupport for Webpack v4 and lower was dropped, but you can install and use the loader version of 2.3.0 in older environments.\n\n## Usage:\n\nLoader allows to perform replacements in a way [String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) does (loader uses it internally).\nIt means that if you want to replace all occurrences, you should use RegExp-like string in `options.search` with `g` flag in `options.flags`, etc.\n\n### Plain replacement:\n\nPlain string replacement, no need to escape RegEx special characters.\n\nIn your `webpack.config.js`:\n\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /fileInWhichJQueryIsUndefined\\.js$/,\n        loader: 'string-replace-loader',\n        options: {\n          search: '$',\n          replace: 'window.jQuery',\n        }\n      }\n    ]\n  }\n}\n```\n\n### RegEx replacement:\n\nTo achieve regular expression replacement you should either specify the `search` option as\n[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance,\neither specify it as string and add the `flags` option (as an empty string if you do not want any flags).\nIn the latter case, `search` and `flags` are being passed to the\n[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor\nand this means that you should escape RegEx special characters in `search` if you want it to be replaced as a string.\n\nIn your `webpack.config.js`:\n\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /fileInWhichJQueryIsUndefined\\.js$/,\n        loader: 'string-replace-loader',\n        options: {\n          search: /\\\\$/i,\n          replace: 'window.jQuery'\n        }\n      }\n    ]\n  }\n}\n```\nor\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /fileInWhichJQueryIsUndefined\\.js$/,\n        loader: 'string-replace-loader',\n        options: {\n          search: '\\\\$',\n          replace: 'window.jQuery',\n          flags: 'i'\n        }\n      }\n    ]\n  }\n}\n```\n\n### Multiple replacement:\n\nAlso, you can pass an array of search-replace pairs this way:\n\nIn your `webpack.config.js`:\n\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /\\.js$/,\n        loader: 'string-replace-loader',\n        options: {\n          multiple: [\n             { search: 'jQuery', replace: 'window.$' },\n             { search: '_', replace: 'window.lodash' }\n          ]\n        }\n      }\n    ]\n  }\n}\n```\n\n### Callback replacement\n\nYou can specify a callback function to have dynamic replacement values.\nThe context of this function will be the context of the loader.\n\nIn your `webpack.config.js`:\n\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /\\.js$/,\n        loader: 'string-replace-loader',\n        options: {\n          search: '^Hello, (.*)!$',\n          replace(match, p1, offset, string) {\n            console.log(`Replace \"${match}\" in file \"${this.resource}\".`)\n            return `Bonjour, ${p1.toUpperCase()}!`\n          },\n          flags: 'g'\n        }\n      }\n    ]\n  }\n}\n```\n\n### Strict mode replacement:\n\nYou can enable strict mode to ensure that the replacement was performed.\nLoader will throw exception if nothing was replaced or if `search` or `replace` options were not specified.\n\nIn your `webpack.config.js`:\n\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /fileInWhichJQueryIsUndefined\\.js$/,\n        loader: 'string-replace-loader',\n        options: {\n          search: 'jQuery',\n          replace: 'window.$',\n          strict: true\n        }\n      }\n    ]\n  }\n}\n```\n\n## Contributing:\n\nFeel free to open issues to propose stuff and participate. Pull requests are also welcome.\n\n## Licence:\n\n[MIT](http://en.wikipedia.org/wiki/MIT_License)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fva1%2Fstring-replace-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fva1%2Fstring-replace-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fva1%2Fstring-replace-loader/lists"}