Skip to content

Commit d90119e

Browse files
committed
Added new 'banner' option and general improvements on the README.
1 parent b67d704 commit d90119e

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = function (grunt) {
2424
single: {
2525
options: {
2626
reservedNames: ['upper'],
27+
banner: '// obfuscated by javascript-obfuscator\n'
2728
},
2829
files: {
2930
'tmp/single-out.js': [

README.md

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,47 @@
1-
grunt-contrib-obfuscator
2-
========================
1+
# grunt-contrib-obfuscator [![Build Status](https://travis-ci.org/javascript-obfuscator/grunt-contrib-obfuscator.svg?branch=master)](https://travis-ci.org/javascript-obfuscator/grunt-contrib-obfuscator)
32

4-
Grunt plugin for [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator). You can try the javascript-obfuscator module and see all its options here: https://javascriptobfuscator.herokuapp.com
3+
> Obfuscate JavaScript files using [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator)
54
6-
[![Build Status](https://travis-ci.org/javascript-obfuscator/grunt-contrib-obfuscator.svg?branch=master)](https://travis-ci.org/javascript-obfuscator/grunt-contrib-obfuscator)
5+
You can try the javascript-obfuscator module and see all its options here: https://javascriptobfuscator.herokuapp.com
76

8-
## Installation
7+
## Getting Started
98

10-
Install the package with NPM:
9+
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
1110

12-
`npm install --save grunt-contrib-obfuscator`
11+
```shell
12+
npm install grunt-contrib-obfuscator --save-dev
13+
```
14+
15+
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
16+
17+
```js
18+
grunt.loadNpmTasks('grunt-contrib-obfuscator');
19+
```
20+
21+
## Obfuscator task
22+
_Run this task with the `grunt obfuscator` command._
23+
24+
Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
25+
26+
## Options
27+
28+
[See the options on the obfuscator repo](https://github.com/javascript-obfuscator/javascript-obfuscator#javascript-obfuscator-options).
29+
30+
_Note that at this time the `sourceMap` isn't implemented in this plugin._
31+
32+
In addition the the obfuscator options, you can also use:
33+
34+
#### banner
35+
Type: `String`
36+
Default: `''`
37+
38+
This string will be prepended to the obfuscated output. Template strings (e.g. `<%= config.value %>` will be expanded automatically.
1339

14-
## Usage
40+
## Usage examples
41+
42+
#### Default options
43+
44+
This configuration will obfuscate the input files using the default options.
1545

1646
```javascript
1747
obfuscator: {
@@ -32,8 +62,27 @@ obfuscator: {
3262
}
3363
```
3464

35-
## Options
65+
#### Debug protection and banner
3666

37-
[See the options on the obfuscator repo](https://github.com/javascript-obfuscator/javascript-obfuscator#javascript-obfuscator-options).
67+
Here you code will be protected against debugging
3868

39-
Note that at this time the `sourceMap` isn't implemented in this plugin.
69+
```javascript
70+
obfuscator: {
71+
options: {
72+
banner: '// obfuscated with grunt-contrib-obfuscator.\n',
73+
debugProtection: true,
74+
debugProtectionInterval: true
75+
},
76+
task1: {
77+
options: {
78+
// options for each sub task
79+
},
80+
files: {
81+
'dest/output.js': [
82+
'src/js/file1.js',
83+
'src/js/file2.js'
84+
]
85+
}
86+
}
87+
}
88+
```

tasks/obfuscator.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ function obfuscate(source, options) {
99
return obfuscationResult.getObfuscatedCode();
1010
}
1111

12+
// Converts \r\n to \n
13+
function normalizeLf(string) {
14+
return string.replace(/\r\n/g, '\n');
15+
}
16+
1217
module.exports = function (grunt) {
1318
var getAvailableFiles = function (filesArray) {
1419
return filesArray.filter(function (filepath) {
@@ -27,7 +32,11 @@ module.exports = function (grunt) {
2732
};
2833

2934
this.files.forEach(function (file) {
30-
var options = this.options({});
35+
var options = this.options({
36+
banner: ''
37+
});
38+
39+
var banner = normalizeLf(options.banner);
3140

3241
var availableFiles = getAvailableFiles(file.src);
3342

@@ -50,7 +59,9 @@ module.exports = function (grunt) {
5059
grunt.warn('JavaScript Obfuscation failed at ' + availableFiles + '.');
5160
}
5261

53-
grunt.file.write(file.dest, obfuscated);
62+
var output = banner + obfuscated;
63+
64+
grunt.file.write(file.dest, output);
5465
created.files++;
5566

5667
}, this);

test/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function readFile(file) {
88

99
exports.obfuscator = {
1010
single: function(test) {
11-
test.expect(3);
11+
test.expect(4);
1212

1313
var result = readFile('tmp/single-out.js');
1414

@@ -23,6 +23,9 @@ exports.obfuscator = {
2323
// tests whether the `reservedNames` option was correctly passed to the library
2424
test.ok(result.indexOf('upper') !== -1);
2525

26+
// checks is the `banner` option is working
27+
test.ok(result.indexOf('// obfuscated by javascript-obfuscator\n') === 0);
28+
2629
test.done();
2730
},
2831
multiple: function(test) {

0 commit comments

Comments
 (0)