Skip to content

Commit

Permalink
Update the README with plugin info.
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Jul 8, 2013
1 parent eafc284 commit 1cbd617
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ module.exports = function(grunt) {
},
start_end_options: {
options: {
startComment: '{test}',
endComment: '{/test}',
start_comment: '{test}',
end_comment: '{/test}',
},
src: 'tmp/start_end_options.js',
},
Expand Down
64 changes: 45 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# grunt-strip-code

> Remove dev and test only code in production builds
The grunt-strip-code plugin is used to strip code from production builds that is only needed in development and test environments. grunt-strip-code uses start and end coments to identify the code sections to strip out.

A sample use case for dev or test only code is to expose hidden functions for unit testing without exposing them in production. [This article](http://philipwalton.com/articles/how-to-test-private-javascript-functions/) explains the concept and implementation.

## Getting Started
This plugin requires Grunt `~0.4.1`
Expand Down Expand Up @@ -37,47 +39,68 @@ grunt.initConfig({

### Options

#### options.separator
#### options.start_comment
Type: `String`
Default value: `', '`
Default value: `test-code`

A string value that is used to do something with whatever.
The text inside the opening comment used to identify code to strip.

#### options.punctuation
#### options.end_comment
Type: `String`
Default value: `'.'`

A string value that is used to do something else with whatever else.
The text inside the closing comment used to identify code to strip.

#### options.pattern
Type: `String`
Default value: (a generated RegExp matching the start and end comment)

If you want to strip code but don't want to wrap the code in start and end comments, you can supply your own RegExp to match. If `pattern` is specified, `start_comment` and `end_comment` are ignored.

### Usage Examples

#### Default Options
In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.`
#### Using custom start and end comments
The following configuration will strip out code that begins with the `/* start-test-block */` comment and ends with the `/* end-test-block */` comment from all `.js` files in the `dist/` folder.

```js
grunt.initConfig({
strip_code: {
options: {},
files: {
'dest/default_options': ['src/testing', 'src/123'],
options: {
start_comment: 'start-test-block',
end_comment: 'end-test-block',
},
src: 'dist/*.js'
},
})
```

#### Custom Options
In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!`
#### Using your own pattern

The following configuration will remove `log()` statements from all `.js` files in the `dist/` folder

```js
grunt.initConfig({
strip_code: {
options: {
separator: ': ',
punctuation: ' !!!',
},
files: {
'dest/default_options': ['src/testing', 'src/123'],
pattern: /log\(\)/g
},
src: 'dist/*.js'
},
})
```

#### Specifying `src` and `dest` files.

The normal behavior is to strip out code in the source files and then save those files with the same name. If you need to save them to a different name, you can specify a `dest` option as well.

```js
grunt.initConfig({
strip_code: {
options: { },
files: [
{src: 'dist/my-app-test.js', dest: 'my-app.js'},
{src: 'dist/my-lib-test.js', dest: 'my-lib.js'},
],
},
})
```
Expand All @@ -86,4 +109,7 @@ grunt.initConfig({
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](http://gruntjs.com/).

## Release History
_(Nothing yet)_

=== 0.1.0

* First Release
8 changes: 4 additions & 4 deletions tasks/strip_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ module.exports = function(grunt) {
grunt.registerMultiTask("strip_code", "Strip code matching a specified patterna.", function(target) {

var options = this.options({
startComment: "test-code",
endComment: "end-test-code"
start_comment: "test-code",
end_comment: "end-test-code"
})
, pattern = options.pattern || new RegExp(
"[\\t ]*\\/\\* ?"
+ options.startComment
+ options.start_comment
+ " ?\\*\\/[\\s\\S]*?\\/\\* ?"
+ options.endComment
+ options.end_comment
+ " ?\\*\\/[\\t ]*\\n?"
, "g"
);
Expand Down

0 comments on commit 1cbd617

Please sign in to comment.