package/package.json000644 0000001451 3560116604 011547 0ustar00000000 000000 { "name": "string-replace-loader", "version": "2.3.0", "description": "Replace loader for Webpack", "keywords": [ "webpack", "loader", "webpack-loader", "replace", "string-replace", "regex-replace" ], "scripts": { "test": "mocha" }, "main": "index.js", "dependencies": { "loader-utils": "^1.2.3", "schema-utils": "^2.6.5" }, "devDependencies": { "chai": "^4.1.2", "mocha": "^5.0.1", "webpack": "^4.34.0" }, "peerDependencies": { "webpack": "1 || 2 || 3 || 4" }, "repository": { "type": "git", "url": "https://github.com/Va1/string-replace-loader.git" }, "homepage": "https://github.com/Va1/string-replace-loader", "author": "Valentyn Barmashyn ", "license": "MIT", "private": false } package/.editorconfig000644 0000000300 3560116604 011726 0ustar00000000 000000 # http://EditorConfig.org root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true indent_style = space [*.{js,json,md}] indent_size = 2 package/index.js000644 0000000122 3560116604 010720 0ustar00000000 000000 const processChuck = require('./lib/processChunk') module.exports = processChuck package/LICENCE000644 0000002074 3560116604 010250 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2015 Valentyn Barmashyn Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package/README.md000644 0000007750 3560116604 010550 0ustar00000000 000000 # Replace loader for [Webpack](http://webpack.github.io/) Perform replacements (plain and regular expression) in the contents loaded by the loader. ## Install: ```bash $ npm install --save-dev string-replace-loader ``` With release of 2.0.0 the loader is expected to be used in Node v4+ environment. Support for Node v3 and lower was dropped, but you can install and use the loader version of 1.3.0 in older environments. ## Usage: Loader 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). It 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. ### Plain replacement: Plain string replacement, no need to escape RegEx special characters. In your `webpack.config.js`: ```javascript module.exports = { // ... module: { rules: [ { test: /fileInWhichJQueryIsUndefined\.js$/, loader: 'string-replace-loader', options: { search: '$', replace: 'window.jQuery', } } ] } } ``` ### RegEx replacement: To achieve regular expression replacement you should either specify the `search` option as [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) instance, either specify it as string and add the `flags` option (as an empty string if you do not want any flags). In the latter case, `search` and `flags` are being passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor and this means that you should escape RegEx special characters in `search` if you want it to be replaced as a string. In your `webpack.config.js`: ```javascript module.exports = { // ... module: { rules: [ { test: /fileInWhichJQueryIsUndefined\.js$/, loader: 'string-replace-loader', options: { search: /\$/i, replace: 'window.jQuery' } } ] } } ``` or ```javascript module.exports = { // ... module: { rules: [ { test: /fileInWhichJQueryIsUndefined\.js$/, loader: 'string-replace-loader', options: { search: '\$', replace: 'window.jQuery', flags: 'i' } } ] } } ``` ### Multiple replacement: Also, you can pass an array of search-replace pairs this way: In your `webpack.config.js`: ```javascript module.exports = { // ... module: { rules: [ { test: /\.js$/, loader: 'string-replace-loader', options: { multiple: [ { search: 'jQuery', replace: 'window.$' }, { search: '_', replace: 'window.lodash' } ] } } ] } } ``` ### Callback replacement You can specify a callback function to have dynamic replacement values. In your `webpack.config.js`: ```javascript module.exports = { // ... module: { rules: [ { test: /\.js$/, loader: 'string-replace-loader', options: { search: '^Hello, (.*)!$', replace: (match, p1, offset, string) => `Bonjour, ${p1.toUpperCase()}!`, flags: 'g' } } ] } } ``` ### Strict mode replacement: You can enable strict mode to ensure that the replacement was performed. Loader will throw exception if nothing was replaced or if `search` or `replace` options were not specified. In your `webpack.config.js`: ```javascript module.exports = { // ... module: { rules: [ { test: /fileInWhichJQueryIsUndefined\.js$/, loader: 'string-replace-loader', options: { search: 'jQuery', replace: 'window.$', strict: true } } ] } } ``` ## Contributing: Feel free to open issues to propose stuff and participate. Pull requests are also welcome. ## Licence: [MIT](http://en.wikipedia.org/wiki/MIT_License) package/lib/getOptionsArray.js000644 0000002300 3560116604 013511 0ustar00000000 000000 const { getOptions } = require('loader-utils') const validateOptions = require('schema-utils') const loaderName = 'string-replace-loader' const optionsSchema = { type: 'object', properties: { search: { anyOf: [ { instanceof: 'RegExp' }, { type: 'string' } ] }, replace: { anyOf: [ { instanceof: 'Function' }, { type: 'string' } ] }, flags: { type: 'string', }, strict: { type: 'boolean' } }, additionalProperties: false } const defaultOptions = { search: null, replace: null, flags: null, strict: false } function getOptionsArray (config) { const rawOptions = getOptions(config) const rawOptionsArray = ( typeof rawOptions.multiple !== 'undefined' ? rawOptions.multiple : [rawOptions] ) const optionsArray = [] for (const optionsIndex in rawOptionsArray) { validateOptions(optionsSchema, rawOptionsArray[optionsIndex], loaderName) optionsArray[optionsIndex] = Object.assign({}, defaultOptions, rawOptionsArray[optionsIndex]) } return optionsArray } module.exports = getOptionsArray package/lib/processChunk.js000644 0000000574 3560116604 013041 0ustar00000000 000000 const getOptionsArray = require('./getOptionsArray') const replace = require('./replace') function processChunk (source, map) { this.cacheable() const optionsArray = getOptionsArray(this) let newSource = source for (const options of optionsArray) { newSource = replace(newSource, options) } this.callback(null, newSource, map) } module.exports = processChunk package/lib/replace.js000644 0000001467 3560116604 012007 0ustar00000000 000000 function replace (source, options) { const { replace, flags, strict } = options let search if (options.search instanceof RegExp) { // if the `search` type is RegExp, we ignore `flags` search = options.search } else if (flags !== null) { search = new RegExp(options.search, flags) } else { search = options.search } if (strict && (typeof search === 'undefined' || search === null || typeof replace === 'undefined' || replace === null)) { throw new Error('Replace failed (strict mode) : options.search and options.replace are required') } const newSource = source.replace(search, replace) if (strict && (newSource === source)) { throw new Error('Replace failed (strict mode) : ' + options.search + ' → ' + options.replace) } return newSource } module.exports = replace