Replace strings on files by using string or regex patterns.
Replace strings on files by using string or regex patterns.
Replaces strings on files by using string or regex patterns. Attempts to be a String.prototype.replace adapter task for your grunt project.
This plugin requires node >= 0.10.0, Grunt >= 0.4.0 and npm >= 1.4.15 (latest stable is recommended).
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-string-replace --save-devOnce the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks("grunt-string-replace");If you're still using grunt v0.3.x it's strongly recommended that you upgrade, but in case you can't please use v0.1.1-1.
Inside your Gruntfile.js file add a section named string-replace. This section specifies the files to edit, destinations, patterns and replacements.
objectDefines what files this task will edit. Grunt itself has very powerful abstractions, so it is highly recommended you understand the different ways to specify them. Learn more at Gruntfile Files mapping, some options incude compact format, files object format and files array format.
objectControls how this task operates and should contain key:value pairs, see options below.
booleanBy default true this flag will instruct grunt-string-replace to copy the files on options.replacements patterns even if there are no replacing matches.
By setting this flag to false files that have not changed (no replacements done) will not be saved on the new location. This will speed up the task if there is a large number of files.
arrayThis option will hold all your pattern/replacement pairs. A pattern/replacement pair should contain key:value pairs containing:
string or regexstringoptions: {
replacements: [
{
pattern: /\/(asdf|qwer)\//gi,
replacement: '"$1"',
},
{
pattern: ",",
replacement: ";",
},
];
}'string-replace': {
dist: {
files: {
'dest/': 'src/**',
'prod/': ['src/*.js', 'src/*.css'],
},
options: {
replacements: [{
pattern: /\/(asdf|qwer)\//ig,
replacement: ''$1''
}, {
pattern: ',',
replacement: ';'
}]
}
}
}'string-replace': {
inline: {
files: {
'dest/': 'src/**',
},
options: {
replacements: [
// place files inline example
{
pattern: '
',
replacement: '
'
}
]
}
}
}For more details, see Grunt's documentation about dynamic files object.
'string-replace': {
dist: {
files: [{
expand: true,
cwd: 'src/',
src: '**/*',
dest: 'dist/'
}],
options: {
replacements: [{
pattern: 'hello',
replacement: 'howdy'
}]
}
}
}Since grunt-string-replace is basically a wrapper of String.prototype.replace you can also provide a function as a replacement pattern instead of a string or a template; as a nice added bonus to using a replacement function, grunt-string-replace will provide 2 extra arguments apart from the ones documented in the link below: src and dest. To get more details about how to use a function as replacement pattern I recommend you to read Specifying a function as a parameter.
We will be reading file names from HTML comments and use the paths later to fetch the content and insert it inside a resulting HTML. Assuming the following setup:
src/index.html
content here
src/partials/header.html
src/partials/footer.html
Gruntfile.js
…After executing grunt we get the following:
dist/index.html
content here
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
saveUnchanged option to control weather unmodified files are saved or not.External libraries are deprecated on Grunt 0.4.2
npm install [email protected].Copyright (c) 2016 Erick Ruiz de Chavez. Licensed under the MIT license.
No open issues yet, or sync has not completed.