diff --git a/Gruntfile.js b/Gruntfile.js index 1e1402d..d0ac151 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', }, diff --git a/README.md b/README.md index bbf6e29..96c8281 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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'}, + ], }, }) ``` @@ -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 diff --git a/tasks/strip_code.js b/tasks/strip_code.js index 26f57e5..0566608 100644 --- a/tasks/strip_code.js +++ b/tasks/strip_code.js @@ -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" );