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 |
|
13 | From NPM:
|
14 |
|
15 | ```shell
|
16 | npm install grunt-replace --save-dev
|
17 | ```
|
18 |
|
19 | ## Usage
|
20 |
|
21 | Assuming installation via NPM, you can use `grunt-replace` in your gruntfile like this:
|
22 |
|
23 | ```javascript
|
24 | module.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 |
|
51 | Supports all the applause [options](https://github.com/outaTiME/applause#options) in addition to the ones below.
|
52 |
|
53 | ### excludeBuiltins
|
54 | Type: `Boolean`
|
55 | Default: `false`
|
56 |
|
57 | If set to `true`, the built-in matching rules are excluded.
|
58 |
|
59 | ### force
|
60 | Type: `Boolean`
|
61 | Default: `true`
|
62 |
|
63 | Force the copy of files even when those files don't have any match found for replacement.
|
64 |
|
65 | ### noProcess
|
66 | Type: `String`
|
67 |
|
68 | This 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
|
73 | Type: `String`
|
74 | Default: `grunt.file.defaultEncoding`
|
75 |
|
76 | The file encoding to copy files with.
|
77 |
|
78 | ### mode
|
79 | Type: `Boolean` or `Number`
|
80 | Default: `false`
|
81 |
|
82 | 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.
|
83 |
|
84 | ### timestamp
|
85 | Type: `Boolean`
|
86 | Default: `false`
|
87 |
|
88 | Whether 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
|
91 | Type: `Boolean`
|
92 | Default: `false`
|
93 |
|
94 | If set to `true`, removes the output from stdout.
|
95 |
|
96 | ### pedantic
|
97 | Type: `Boolean`
|
98 | Default: `false`
|
99 |
|
100 | If set to `true`, the task will fail with a `grunt.fail.warn` when no matches are present.
|
101 |
|
102 | ## Built-in replacements
|
103 |
|
104 | Few 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 |
|
136 | File `src/manifest.appcache`:
|
137 |
|
138 | ```
|
139 | CACHE MANIFEST
|
140 | # @@timestamp
|
141 |
|
142 | CACHE:
|
143 |
|
144 | favicon.ico
|
145 | index.html
|
146 |
|
147 | NETWORK:
|
148 | *
|
149 | ```
|
150 |
|
151 | Task 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 |
|
173 | File `src/manifest.appcache`:
|
174 |
|
175 | ```
|
176 | CACHE MANIFEST
|
177 | # @@timestamp
|
178 |
|
179 | CACHE:
|
180 |
|
181 | favicon.ico
|
182 | index.html
|
183 |
|
184 | NETWORK:
|
185 | *
|
186 | ```
|
187 |
|
188 | File `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 |
|
209 | Task 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 |
|
235 | File `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 |
|
244 | Task 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 |
|
266 | File `src/index.html`:
|
267 |
|
268 | ```html
|
269 | <body>
|
270 | @@include
|
271 | </body>
|
272 | ```
|
273 |
|
274 | Task 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 |
|
296 | File `src/username.txt`:
|
297 |
|
298 | ```
|
299 | John Smith
|
300 | ```
|
301 |
|
302 | Task 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 |
|
324 | Task 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 |
|
380 | File `src/app.js`:
|
381 |
|
382 | ```javascript
|
383 | // Filename: @@__SOURCE_FILENAME__
|
384 |
|
385 | var App = App || (function () {
|
386 | return {
|
387 | // App contents
|
388 | };
|
389 | })();
|
390 |
|
391 | window.App = App;
|
392 | ```
|
393 |
|
394 | Task 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 |
|
415 | MIT © [outaTiME](https://outa.im)
|