JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 47688
  • Score
    100M100P100Q146167F

General purpose text-replacement for grunt

Package Exports

  • grunt-text-replace

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (grunt-text-replace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

grunt-text-replace

General purpose text-replacement for grunt.

This plugin allows you to replace text in files with regex or string replacement rules.

Installation

Installation follows the same pattern for any grunt plugin:

  • In your project's grunt.js directory, run: npm install grunt-text-replace
  • Alternatively list it as a dependancy in your package.json and run: npm install
  • Then add this line to your project's grunt.js:
grunt.loadNpmTasks('grunt-text-replace');

Usage

Below we're using:

  • a string to change Hello to Goodbye
  • a regex to change Foooo to Moooo
grunt.initConfig({
  ...
  replace: {
    example: {
      src: ['text/*.txt'],
      dest: 'build/text/',
      replacements: [
        { 
          from: 'Hello', 
          to: 'Good bye' 
        }, 
        { 
          from: /(f|F)(o{2,100})/g, 
          to: 'M$2' 
        }
      ]
    }
  }
  ...
});

In the next example we're:

grunt.initConfig({
  ...
  replace: {
    another_example: {
      src: ['build/*.html'],
      overwrite: true,
      replacements: [
        { 
          from: '<p>Version:</p>', 
          to: '<p>Version: <%= grunt.template.today("yyyy-mm-dd") %></p>'
        }
      ]
    }
  }
  ...
});

API reference

replace

Object. The top level grunt task.

replace is a multi-task, meaning that it must contain targets, which you can name anything you like.

src

Array. The source of the files that require text replacement.

src must be defined within each target. src supports minimatch paths.

Examples of valid src values:

src: ['test.txt']             // matches the files 'test.txt' only
src: ['test/*.html']          // matches all html files inside the folder 'test'
src: ['**/*.js']              // matches all .js files inside all subdirctories 
src: ['test.txt', '**/*.js']  // a combination of two of the above

dest

String. The destination for those files that have are matched by the src.

dest can refer to either:

  • a single file
  • a single directory

grunt-text-replace will throw an error if multiple source files are mapped to a single file.

Examples of valid desc values:

dest: 'output.txt'    // sends to 'output.txt' in the grunt.js directory
dest: 'output/'       // sends the replace files/s to the directory 'output'

overwrite

Boolean. A switch to allow grunt-replace-text to rewrite original files.

overwrite can only be used when a dest is not defined, otherwise grunt-text-replace will throw an error.

replacements

Array. The set of text replacements for a given task.

replacements is an array that can contain any number of replacements.

Examples of valid replacements values:

replacements: [{ from: "Red", to: "Blue" }]
replacements: [
  { 
    from: /colou?r/g, 
    to: "shade" 
  }, 
  {
    from: /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/g,
    to: "<%= grunt.template.today('dd/mm/yyyy') %>"
  }
]

from

String or RegEx. The old text that you'd like replace.

from can be:

  • a plain string
  • a regular expression object

For examples, see replacements above.

to

String. The new text that you'd like to change to.

to can be:

  • a plain string
  • a string containing a grunt.template
  • a string containing regex variables $1, $2, etc

For examples, see replacements above.

Release History

Current version: 0.1.7

License

Copyright (c) 2012 Jonathan Holmes
Licensed under the MIT license.