1.0.13 • Published 9 years ago

robscure v1.0.13

Weekly downloads
2
License
-
Repository
github
Last release
9 years ago

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: 1. reads the contents of each input file 2. lists all modules defined in all files 3. assigns to each module a new key 4. 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

1.0.13

9 years ago

1.0.12

9 years ago

1.0.11

9 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago