jamesandersen / string-replace-webpack-plugin

Replace string tokens in a bundle.
93 stars 27 forks source link

Only replaces first match in file #10

Closed anthonator closed 9 years ago

anthonator commented 9 years ago

Only replaces the first instance of the matched item in a file.

{
  replacements: [
    {
      pattern: /process\.env\.[A-Z\d_]+/,
      replacement: function(match, p1, offset, string) {
        return process.env[match.replace(/process\.env\./, '')];
      }
    }
  ]
}
anthonator commented 9 years ago

Example output to replace process.env.. Only the instance in del get replaced.

'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true
});

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _superagent = __webpack_require__(413);

var _superagent2 = _interopRequireDefault(_superagent);

/**
 * Superagent wrapper for easily making AJAX requests. Provides a layer of
 * convenience by adding a base URL to each request. The base URL is derived
 * from the ```API_URL``` environment variable.
 */
exports['default'] = {
  /**
   * DELETE relative `path` with optional callback `fn(res)`.
   *
   * @method del
   * @param {String} path
   * @param {Function} fn
   * @return {Request}
   * @public
   */
  del: function del(path, fn) {
    return _superagent2['default'].del('http://localhost:5000/api' + path, fn);
  },

  /**
   * GET relative `path` with optional `data` and callback `fn(res)`.
   *
   * @method get
   * @param {String} path
   * @param {Mixed} data
   * @param {Function} fn
   * @return {Request}
   * @public
   */
  get: function get(path, data, fn) {
    return _superagent2['default'].get('process.env.API_URL' + path, data, fn);
  },

  /**
   * PATCH relative `path` with optional `data` and callback `fn(res)`.
   *
   * @method patch
   * @param {String} path
   * @param {Mixed} data
   * @param {Function} fn
   * @return {Request}
   * @public
   */
  patch: function patch(path, data, fn) {
    return _superagent2['default'].patch('process.env.API_URL' + path, data, fn);
  },

  /**
   * POST relative `path` with optional `data` and callback `fn(res)`.
   *
   * @method post
   * @param {String} path
   * @param {Mixed} data
   * @param {Function} fn
   * @return {Request}
   * @public
   */
  post: function post(path, data, fn) {
    return _superagent2['default'].post('process.env.API_URL' + path, data, fn);
  },

  /**
   * PUT relative `path` with optional `data` and callback `fn(res)`.
   *
   * @method put
   * @param {String} path
   * @param {Mixed} data
   * @param {Function} fn
   * @return {Request}
   * @public
   */
  put: function put(path, data, fn) {
    return _superagent2['default'].put('process.env.API_URL' + path, data, fn);
  }
};
module.exports = exports['default'];

/*****************
 ** WEBPACK FOOTER
 ** ./app/sources/http/client.js
 ** module id = 539
 ** module chunks = 0
 **/
anthonator commented 9 years ago

Needed to add a \g to the end of the pattern.

{
  replacements: [
    {
      pattern: /process\.env\.[A-Z\d_]+/g,
      replacement: function(match, p1, offset, string) {
        return process.env[match.replace(/process\.env\./, '')];
      }
    }
  ]
}
jamesandersen commented 9 years ago

@anthonator glad you got it worked out ;-)