jack e1a2621c78 update | 2 년 전 | |
---|---|---|
.. | ||
tasks | 2 년 전 | |
LICENSE-MIT | 2 년 전 | |
README.md | 2 년 전 | |
package.json | 2 년 전 |
Replace text patterns with applause.
From NPM:
npm install grunt-replace --save-dev
Assuming installation via NPM, you can use grunt-replace
in your gruntfile like this:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-replace');
grunt.initConfig({
replace: {
dist: {
options: {
patterns: [
{
match: 'foo',
replacement: 'bar'
}
]
},
files: [
{expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'}
]
}
}
});
grunt.registerTask('default', 'replace');
};
Type: Array
Define patterns that will be used to replace the contents of source files.
Type: String|RegExp
Indicates the matching expression.
If matching type is String
we use a simple variable lookup mechanism @@string
(in any other case we use the default regexp replace logic):
{
patterns: [
{
match: 'foo',
replacement: 'bar' // replaces "@@foo" to "bar"
}
]
}
Type: String|Function|Object
Indicates the replacement for match, for more information about replacement check out the String.replace.
You can specify a function as replacement. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string.
{
patterns: [
{
match: /foo/g,
replacement: function () {
return 'bar'; // replaces "foo" to "bar"
}
}
]
}
Also supports object as replacement (we create string representation of object using JSON.stringify):
{
patterns: [
{
match: /foo/g,
replacement: [1, 2, 3] // replaces "foo" with string representation of "array" object
}
]
}
Type: Object
If an attribute json
found in pattern definition we flatten the object using delimiter
concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support).
{
patterns: [
{
json: {
"key": "value" // replaces "@@key" to "value"
}
}
]
}
Also supports nested objects:
{
patterns: [
{
json: {
"key": "value", // replaces "@@key" to "value"
"inner": { // replaces "@@inner" with string representation of "inner" object
"key": "value" // replaces "@@inner.key" to "value"
}
}
}
]
}
For deferred invocations is possible to define functions:
{
patterns: [
{
json: function (done) {
done({
key: 'value'
});
}
}
]
}
Type: String
If an attribute yaml
found in pattern definition will be converted and then processed like json attribute.
{
patterns: [
{
yaml: 'key: value' // replaces "@@key" to "value"
}
]
}
For deferred invocations is possible to define functions:
{
patterns: [
{
yaml: function (done) {
done('key: value');
}
}
]
}
Type: String
If an attribute cson
found in pattern definition will be converted and then processed like json attribute.
{
patterns: [
{
cson: 'key: \'value\''
}
]
}
For deferred invocations is possible to define functions:
{
patterns: [
{
cson: function (done) {
done('key: \'value\'');
}
}
]
}
Type: Object
This is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support). You can still use this but for more control you should use the new patterns
way.
{
variables: {
'key': 'value' // replaces "@@key" to "value"
}
}
Type: String
Default: @@
The prefix added for matching (prevent bad replacements / easy way).
This only applies for simple variable lookup mechanism.
Type: Boolean
Default: true
If set to false
, we match the pattern without prefix
concatenation (useful when you want to lookup an simple string).
This only applies for simple variable lookup mechanism.
Type: Boolean
Default: false
If set to true
, we preserve the prefix
in target.
This only applies for simple variable lookup mechanism and
patterns.replacement
is an string.
Type: String
Default: .
The delimiter used to flatten when using object as replacement.
Type: Boolean
Default: false
If set to true
, we preserve the patterns definition order, otherwise these will be sorted (in ascending order) to prevent replacement issues like head
/ header
(typo regexps will be resolved at last).
Type: Boolean
Default: false
If set to true
, we exclude built-in matching rules.
Type: Boolean
Default: true
Force the copy of files even when those files don't have any match found for replacement.
Type: String
This option is an advanced way to control which file contents are processed.
processContentExclude
has been renamed tonoProcess
and the option name will be removed in the future.
Type: String
Default: grunt.file.defaultEncoding
The file encoding to copy files with.
Type: Boolean
or Number
Default: false
Whether to copy or set the existing file permissions. Set to true
to copy the existing file permissions. Or set to the mode, i.e.: 0644
, that copied files will be set to.
Few matching rules are provided by default and can be used anytime (these will be affected by the options
given):
__SOURCE_FILE__
:
Replace match with the source file.
__SOURCE_PATH__
:
Replace match with the path of source file.
__SOURCE_FILENAME__
:
Replace match with the filename of source file.
__TARGET_FILE__
:
Replace match with the target file.
__TARGET_PATH__
:
Replace match with the path of target file.
__TARGET_FILENAME__
:
Replace match with the filename of target file.
If you are looking how to use an
built-in
replacements, check out the How to insert filename in target usage.
File src/manifest.appcache
:
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*
Gruntfile, define pattern (for timestamp) and the source files for lookup:
replace: {
dist: {
options: {
patterns: [
{
match: 'timestamp',
replacement: '<%= grunt.template.today() %>'
}
]
},
files: [
{expand: true, flatten: true, src: ['src/manifest.appcache'], dest: 'build/'}
]
}
}
File src/manifest.appcache
:
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*
File src/humans.txt
:
__ _
_ _/__ /./|,//_`
/_//_// /_|/// //_, outaTiME v.@@version
/* TEAM */
Web Developer / Graphic Designer: Ariel Oscar Falduto
Site: http://www.outa.im
Twitter: @outa7iME
Contact: afalduto at gmail dot com
From: Buenos Aires, Argentina
/* SITE */
Last update: @@timestamp
Standards: HTML5, CSS3, robotstxt.org, humanstxt.org
Components: H5BP, Modernizr, jQuery, Twitter Bootstrap, LESS, Jade, Grunt
Software: Sublime Text 2, Photoshop, LiveReload
Gruntfile:
replace: {
dist: {
options: {
patterns: [
{
match: 'version',
replacement: '<%= pkg.version %>'
},
{
match: 'timestamp',
replacement: '<%= grunt.template.today() %>'
}
]
},
files: [
{expand: true, flatten: true, src: ['src/manifest.appcache', 'src/humans.txt'], dest: 'build/'}
]
}
}
File src/assets/index.html
:
<head>
<link rel="stylesheet" href="/css/style.css?rel=@@timestamp">
<script src="/js/app.js?rel=@@timestamp"></script>
</head>
Gruntfile:
replace: {
dist: {
options: {
patterns: [
{
match: 'timestamp',
replacement: '<%= new Date().getTime() %>'
}
]
},
files: [
{src: ['src/assets/index.html'], dest: 'build/index.html'}
]
}
}
File src/index.html
:
<body>
@@include
</body>
Gruntfile:
replace: {
dist: {
options: {
patterns: [
{
match: 'include',
replacement: '<%= grunt.file.read("includes/content.html") %>'
}
]
},
files: [
{expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'}
]
}
}
File src/username.txt
:
John Smith
Gruntfile:
replace: {
dist: {
options: {
patterns: [
{
match: /(\w+)\s(\w+)/,
replacement: '$2, $1' // replaces "John Smith" to "Smith, John"
}
]
},
files: [
{expand: true, flatten: true, src: ['src/username.txt'], dest: 'build/'}
]
}
}
foo
instead of @@foo
Gruntfile:
// option 1 (explicitly using an regexp)
replace: {
dist: {
options: {
patterns: [
{
match: /foo/g,
replacement: 'bar'
}
]
},
files: [
{expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'}
]
}
}
// option 2 (easy way)
replace: {
dist: {
options: {
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
usePrefix: false
},
files: [
{expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'}
]
}
}
// option 3 (old way)
replace: {
dist: {
options: {
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
prefix: '' // remove prefix
},
files: [
{expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'}
]
}
}
File src/app.js
:
// filename: @@__SOURCE_FILENAME__
var App = App || (function () {
return {
// app contents
};
}());
window.App = App;
Gruntfile:
replace: {
dist: {
options: {
// pass, we use built-in replacements
},
files: [
{expand: true, flatten: true, src: ['src/**/*.js'], dest: 'build/'}
]
}
}
String
.Task submitted by Ariel Falduto