Package Exports
- broccoli-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 (broccoli-replace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
broccoli-replace
Replace text patterns with a given replacement using pattern-replace.
Install
From NPM:
npm install broccoli-replace --save-devReplace Filter
Assuming installation via NPM, you can use broccoli-replace in your brocfile like this:
module.exports = function (broccoli) {
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
srcFiles = replace(srcFiles, {
files: [
'**/*.html' // replace only html files in src
],
patterns: [
{
match: 'foo',
replacement: 'bar'
}
]
});
return [srcFiles];
};Options
files
Type: Array
Default: []
Define the source files that will be used for replacements, you can use globbing via minimatch library.
This is a mandatory value, an empty definition will ignore any kind of replacement.
patterns
Type: Array
Define patterns that will be used to replace the contents of source files.
Check out pattern-replace documentation for more details.
variables
Type: Object
This is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support), you can still using but for more control you should use the new patterns way.
Check out pattern-replace documentation for more details.
prefix
Type: String
Default: @@
The prefix added for matching (prevent bad replacements / easy way).
Check out pattern-replace documentation for more details.
usePrefix
Type: Boolean
Default: true
If set to false, we match the pattern without prefix concatenation (useful when you want to lookup an simple string).
Check out pattern-replace documentation for more details.
preservePrefix
Type: Boolean
Default: false
If set to true, we preserve the prefix in target.
Check out pattern-replace documentation for more details.
delimiter
Type: String
Default: .
The delimiter used to flatten when using object as replacement.
Check out pattern-replace documentation for more details.
Usage Examples
Basic
File src/manifest.appcache:
CACHE MANIFEST
# @@timestamp
CACHE:
favicon.ico
index.html
NETWORK:
*Brocfile:
module.exports = function (broccoli) {
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
srcFiles = replace(srcFiles, {
files: [
'manifest.appcache'
],
patterns: [
{
match: 'timestamp',
replacement: new Date().getTime()
}
]
});
return [srcFiles];
};Multiple matching
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
Brocfile:
module.exports = function (broccoli) {
var pkg = require('./package.json');
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
srcFiles = replace(srcFiles, {
files: [
'manifest.appcache',
'humans.txt'
],
patterns: [
{
match: 'version',
replacement: pkg.version
},
{
match: 'timestamp',
replacement: new Date().getTime()
}
]
});
return [srcFiles];
};Cache busting
File src/index.html:
<head>
<link rel="stylesheet" href="/css/style.css?rel=@@timestamp">
<script src="/js/app.js?rel=@@timestamp"></script>
</head>Brocfile:
module.exports = function (broccoli) {
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
srcFiles = replace(srcFiles, {
files: [
'index.html'
],
patterns: [
{
match: 'timestamp',
replacement: new Date().getTime()
}
]
});
return [srcFiles];
};Include file
File src/index.html:
<body>
@@include
</body>Brocfile:
module.exports = function (broccoli) {
var fs = require('fs');
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
srcFiles = replace(srcFiles, {
files: [
'index.html'
],
patterns: [
{
match: 'include',
replacement: fs.readFileSync('./includes/content.html').toString()
}
]
});
return [srcFiles];
};Regular expression
File src/username.txt:
John SmithBrocfile:
module.exports = function (broccoli) {
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
srcFiles = replace(srcFiles, {
files: [
'username.txt'
],
patterns: [
{
match: /(\w+)\s(\w+)/,
replacement: '$2, $1' // replaces "John Smith" to "Smith, John"
}
]
});
return [srcFiles];
};Lookup for foo instead of @@foo
The String matching type or expression in false generates a simple variable lookup mechanism @@string, to skip this mode use one of the below rules ... make your choice:
Brocfile:
module.exports = function (broccoli) {
var replace = require('broccoli-replace');
var srcFiles = broccoli.makeTree('src');
// option 1 (explicitly using an regexp)
var replacer_op1 = replace(srcFiles, {
files: [
'foo.txt'
],
patterns: [
{
match: /foo/g,
replacement: 'bar'
}
]
});
// option 2 (easy way)
var replacer_op2 = replace(srcFiles, {
files: [
'foo.txt'
],
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
usePrefix: false
});
// option 3 (old way)
var replacer_op3 = replace(srcFiles, {
files: [
'foo.txt'
],
patterns: [
{
match: 'foo',
replacement: 'bar'
}
],
prefix: '' // remove prefix
});
return [replacer_op1, replacer_op2, replacer_op3];
};Release History
- 2014-02-25 v0.0.3 Code normalization and documentation updated.
- 2014-02-23 v0.0.2 Use Filter instead of Transformer.
- 2014-02-22 v0.0.1 Initial version.
Task submitted by Ariel Falduto