robscure
v1.0.13
Published
Obfuscates modules defined using R.js
Downloads
3
Readme
robscure
GruntJs task to further minify and obfuscate the keys defined using R.js https://www.npmjs.com/package/robscure
Premise
R.js is an implementation for dependencies management, freely inspired by RequireJs and Angular dependency injection mechanism. https://github.com/RobertoPrevato/R.js R.js has a syntax which is similar to the one of RequireJs, but follows a different approach and aims at simplicity.
R(key, dependencies, function); to define an object with its dependencies
R(key); to require an object
R([key1, key2, key3, ...]) to require an array of objects
robscure is a GruntJs task to further minify and obfuscate the keys defined using R.js. Example: Let's imagine a single web application which includes multiple bundled and minified JavaScript files, for different parts of the application.
- common.min.js -> common functions
- profile.min.js -> for profile page
- settings.min.js -> for settings page
- [...]
Normally after minification the keys of the modules would stay in clear, so:
R("model", [], function () { ... });
R("dashboard", ["model"], function () { ... });
Using robscure task is possible to further obfuscate the code, obtaining something like:
R("♥", [], function () { ... });
R("♪", ["♥"], function () { ... });
Limitations
robscure doesn't support the following: variable keys: module keys must be constant strings.
//NOT supported:
var a = "somekey";
R(a, [], function () { ... });
R("some-module", [a], function (dep) { ... });
dynamic dependencies: module dependencies must be a constant array
//NOT supported:
var deps = [];
deps.push(aKey);
R("module-name", deps, function () { ... });
calls to R function using .call or .apply
//NOT supported:
R.call(null, "module-name", [], function { ... });
R.apply(null, ["module-name", [], function { ... }]);
Getting Started
This plugin requires Grunt ~0.4.5
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install robscure --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('robscure');
The "robscure" task
Overview
In your project's Gruntfile, add a section named robscure
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
robscure: {
website: {
// these are all the built or minified files of all areas: the task will look for each
// for each file, a new file will be generated, with obfuscated module names
areas: [
"../scripts/main.min.js",
"../scripts/area-one.min.js",
"../scripts/area-two.min.js",
"../scripts/area-three.min.js"
],
// allows to specify whether an exception should be thrown if duplicated modules names are found across multiple areas (default: true)
exceptionIfDuplicated: true,
// the suffix to use for generated files (default: ".obs")
fileSuffix: ".obs"
}
}
});
Options
options.areas
Type: Array
Array containing paths to all the built or minified files of all areas: the task will look for all modules defined in all areas, and for each file generate a new file with obfuscated dependencies. It is recommended to use minified files, not built files.
options.fileSuffix
Type: String
Default value: .obs
Suffix to apply to output filenames.
options.getFileName
Type: Function
Default value: null
If specified, allows to define a function that returns a filename for new files.
options.chars
Type: String
Default value: Я♪♫♦♥♠♣zyxwvutsrqponmlkjihgfedcba
_^][ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#!`
Characters that can compose the modules key.
options.exceptionIfDuplicated
Type: Boolean
Default value: true
A boolean indicating whether to throw an exception if duplicated modules definitions are found across all input files. When defininig multiple modules, it's important to avoid to accidentally override modules defined in different files.
Usage Examples
Default Options
In this example four minified files that relate to different areas of the application are processed to obfuscate the modules definitions in all of them. In details, the task performs the following operations:
- reads the contents of each input file
- lists all modules defined in all files
- assigns to each module a new key
- for each input file generates a new file, with contents in which the module names have been replaced with the new keys
grunt.initConfig({
robscure: {
website: {
areas: [
"../scripts/main.min.js",
"../scripts/area-one.min.js",
"../scripts/area-two.min.js",
"../scripts/area-three.min.js"
]
}
}
});
The names of the output files are obtained adding a suffix to each input file name, so it is only necessary to specify the input files path.
Release History
2015-05-30 first version released