UNPKG

7.68 kBMarkdownView Raw
1# grunt-replace
2
3[![Build Status](https://img.shields.io/travis/outaTiME/grunt-replace.svg)](https://travis-ci.org/outaTiME/grunt-replace)
4[![Version](https://img.shields.io/npm/v/grunt-replace.svg)](https://www.npmjs.com/package/grunt-replace)
5![Prerequisite](https://img.shields.io/badge/node-%3E%3D10-blue.svg)
6[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)
7[![Twitter: outa7iME](https://img.shields.io/twitter/follow/outa7iME.svg?style=social)](https://twitter.com/outa7iME)
8
9> Replace text patterns with [applause](https://github.com/outaTiME/applause).
10
11## Install
12
13From NPM:
14
15```shell
16npm install grunt-replace --save-dev
17```
18
19## Usage
20
21Assuming installation via NPM, you can use `grunt-replace` in your gruntfile like this:
22
23```javascript
24module.exports = function (grunt) {
25 grunt.initConfig({
26 replace: {
27 dist: {
28 options: {
29 patterns: [
30 {
31 match: 'foo',
32 replacement: 'bar'
33 }
34 ]
35 },
36 files: [
37 {
38 expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'
39 }
40 ]
41 }
42 }
43 });
44 grunt.loadNpmTasks('grunt-replace');
45 grunt.registerTask('default', ['replace']);
46};
47```
48
49## Options
50
51Supports all the applause [options](https://github.com/outaTiME/applause#options) in addition to the ones below.
52
53### excludeBuiltins
54Type: `Boolean`
55Default: `false`
56
57If set to `true`, the built-in matching rules are excluded.
58
59### force
60Type: `Boolean`
61Default: `true`
62
63Force the copy of files even when those files don't have any match found for replacement.
64
65### noProcess
66Type: `String`
67
68This option is an advanced way to control which file contents are processed.
69
70> `processContentExclude` has been renamed to `noProcess` and the option name will be removed in the future.
71
72### encoding
73Type: `String`
74Default: `grunt.file.defaultEncoding`
75
76The file encoding to copy files with.
77
78### mode
79Type: `Boolean` or `Number`
80Default: `false`
81
82Whether 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.
83
84### timestamp
85Type: `Boolean`
86Default: `false`
87
88Whether to preserve the timestamp attributes (atime and mtime) when copying files. Set to true to preserve files timestamp. But timestamp will not be preserved when the file contents or name are changed during copying.
89
90### silent
91Type: `Boolean`
92Default: `false`
93
94If set to `true`, removes the output from stdout.
95
96### pedantic
97Type: `Boolean`
98Default: `false`
99
100If set to `true`, the task will fail with a `grunt.fail.warn` when no matches are present.
101
102## Built-in replacements
103
104Few matching rules are provided by default and can be used anytime (these will be affected by the `options` given):
105
106 * `__SOURCE_FILE__`:
107
108 Replace match with the source file.
109
110 * `__SOURCE_PATH__`:
111
112 Replace match with the path of source file.
113
114 * `__SOURCE_FILENAME__`:
115
116 Replace match with the filename of source file.
117
118 * `__TARGET_FILE__`:
119
120 Replace match with the target file.
121
122 * `__TARGET_PATH__`:
123
124 Replace match with the path of target file.
125
126 * `__TARGET_FILENAME__`:
127
128 Replace match with the filename of target file.
129
130> If you are looking how to use an `built-in` replacements, check out the [How to insert filename in target](#how-to-insert-filename-in-target) usage.
131
132## Examples
133
134### Basic
135
136File `src/manifest.appcache`:
137
138```
139CACHE MANIFEST
140# @@timestamp
141
142CACHE:
143
144favicon.ico
145index.html
146
147NETWORK:
148*
149```
150
151Task configuration on gruntfile:
152
153```javascript
154{
155 options: {
156 patterns: [
157 {
158 match: 'timestamp',
159 replacement: '<%= Date.now() %>'
160 }
161 ]
162 },
163 files: [
164 {
165 expand: true, flatten: true, src: ['src/manifest.appcache'], dest: 'build/'
166 }
167 ]
168}
169```
170
171### Multiple matching
172
173File `src/manifest.appcache`:
174
175```
176CACHE MANIFEST
177# @@timestamp
178
179CACHE:
180
181favicon.ico
182index.html
183
184NETWORK:
185*
186```
187
188File `src/humans.txt`:
189
190```
191 __ _
192 _ _/__ /./|,//_`
193 /_//_// /_|/// //_, outaTiME v.@@version
194
195/* TEAM */
196 Web Developer / Graphic Designer: Ariel Oscar Falduto
197 Site: https://www.outa.im
198 Twitter: @outa7iME
199 Contact: afalduto at gmail dot com
200 From: Buenos Aires, Argentina
201
202/* SITE */
203 Last update: @@timestamp
204 Standards: HTML5, CSS3, robotstxt.org, humanstxt.org
205 Components: H5BP, Modernizr, jQuery, Bootstrap, LESS, Jade, Grunt
206 Software: Sublime Text, Photoshop, LiveReload
207```
208
209Task configuration on gruntfile:
210
211```javascript
212{
213 options: {
214 patterns: [
215 {
216 match: 'version',
217 replacement: '<%= pkg.version %>'
218 },
219 {
220 match: 'timestamp',
221 replacement: '<%= Date.now() %>'
222 }
223 ]
224 },
225 files: [
226 {
227 expand: true, flatten: true, src: ['src/manifest.appcache', 'src/humans.txt'], dest: 'build/'
228 }
229 ]
230}
231```
232
233### Cache busting
234
235File `src/index.html`:
236
237```html
238<head>
239 <link rel="stylesheet" href="/css/style.css?rel=@@timestamp">
240 <script src="/js/app.js?rel=@@timestamp"></script>
241</head>
242```
243
244Task configuration on gruntfile:
245
246```javascript
247{
248 options: {
249 patterns: [
250 {
251 match: 'timestamp',
252 replacement: '<%= Date.now() %>'
253 }
254 ]
255 },
256 files: [
257 {
258 src: ['src/index.html'], dest: 'build/index.html'
259 }
260 ]
261}
262```
263
264### Include file
265
266File `src/index.html`:
267
268```html
269<body>
270 @@include
271</body>
272```
273
274Task configuration on gruntfile:
275
276```javascript
277{
278 options: {
279 patterns: [
280 {
281 match: 'include',
282 replacement: '<%= grunt.file.read("includes/content.html") %>'
283 }
284 ]
285 },
286 files: [
287 {
288 expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'
289 }
290 ]
291}
292```
293
294### Regular expression
295
296File `src/username.txt`:
297
298```
299John Smith
300```
301
302Task configuration on gruntfile:
303
304```javascript
305{
306 options: {
307 patterns: [
308 {
309 match: /(\w+)\s(\w+)/,
310 replacement: '$2, $1' // Replaces "John Smith" with "Smith, John"
311 }
312 ]
313 },
314 files: [
315 {
316 expand: true, flatten: true, src: ['src/username.txt'], dest: 'build/'
317 }
318 ]
319}
320```
321
322### Lookup for `foo` instead of `@@foo`
323
324Task configuration on gruntfile:
325
326```javascript
327{
328 'opt-1': {
329 options: {
330 patterns: [
331 {
332 match: /foo/g, // Explicitly using a regexp
333 replacement: 'bar'
334 }
335 ]
336 },
337 files: [
338 {
339 expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'
340 }
341 ]
342 },
343 'opt-2': {
344 options: {
345 patterns: [
346 {
347 match: 'foo',
348 replacement: 'bar'
349 }
350 ],
351 usePrefix: false // Using the option provided
352 },
353 files: [
354 {
355 expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'
356 }
357 ]
358 },
359 'opt-3': {
360 options: {
361 patterns: [
362 {
363 match: 'foo',
364 replacement: 'bar'
365 }
366 ],
367 prefix: '' // Removing the prefix manually
368 },
369 files: [
370 {
371 expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'
372 }
373 ]
374 }
375}
376```
377
378### How to insert filename in target
379
380File `src/app.js`:
381
382```javascript
383// Filename: @@__SOURCE_FILENAME__
384
385var App = App || (function () {
386 return {
387 // App contents
388 };
389})();
390
391window.App = App;
392```
393
394Task configuration on gruntfile:
395
396```javascript
397{
398 options: {
399 // Pass, we use built-in replacements
400 },
401 files: [
402 {
403 expand: true, flatten: true, src: ['src/**/*.js'], dest: 'build/'
404 }
405 ]
406}
407```
408
409## Related
410
411- [applause](https://github.com/outaTiME/applause) - Human-friendly replacements
412
413## License
414
415MIT © [outaTiME](https://outa.im)