├── test ├── data │ ├── A.js │ ├── D.js │ ├── C.js │ ├── B.js │ └── app.js └── Hackerify.spec.js ├── example ├── index.html ├── run.sh ├── label.js ├── answer.js ├── app.js └── bundle.js ├── .gitignore ├── index.js ├── karma.conf.js ├── package.json ├── LICENSE └── README.md /test/data/A.js: -------------------------------------------------------------------------------- 1 | module.exports = 'Hackerify'; -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /example/run.sh: -------------------------------------------------------------------------------- 1 | ../node_modules/.bin/browserify ./app.js > bundle.js -------------------------------------------------------------------------------- /example/label.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return 'The answer is: '; 3 | } -------------------------------------------------------------------------------- /test/data/D.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | loc: function() { 3 | return 30; 4 | } 5 | } -------------------------------------------------------------------------------- /test/data/C.js: -------------------------------------------------------------------------------- 1 | var D = require('./D'); 2 | module.exports = function() { 3 | return 'LOC: ' + D.loc(); 4 | } -------------------------------------------------------------------------------- /example/answer.js: -------------------------------------------------------------------------------- 1 | var label = require('./label'); 2 | module.exports = function() { 3 | return label() + 42; 4 | } -------------------------------------------------------------------------------- /test/data/B.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | what: 'A Node.js module.', 3 | description: function() { 4 | return 'My job is to stub required modules within browserify.'; 5 | } 6 | } -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | var Hackerify = require('../index'); 2 | Hackerify(arguments, { 3 | './label': function() { 4 | return 'The answer is not always '; 5 | } 6 | }); 7 | 8 | var a = require('./answer'); 9 | alert(a()); -------------------------------------------------------------------------------- /test/data/app.js: -------------------------------------------------------------------------------- 1 | var A = require('./A'); 2 | var B = require('./B'); 3 | var C = require('./C'); 4 | 5 | module.exports = function() { 6 | var message = ''; 7 | message += 'I am ' + A + '. '; 8 | message += B.what + ' ' + B.description(); 9 | message += ' ' + C(); 10 | return message; 11 | }; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function(args, stub) { 2 | args = [].slice.call(args); 3 | var modules = args[4]; 4 | if(modules) { 5 | var collection = {}; 6 | for(var i in modules) { 7 | var map = modules[i][1]; 8 | for(var mName in map) { 9 | collection[mName] = map[mName]; 10 | } 11 | } 12 | for(var s in stub) { 13 | if(collection[s]) { 14 | modules[collection[s]] = [(function(st) { 15 | return function(require, module, exports) { 16 | module.exports = st; 17 | }; 18 | })(stub[s]), {}]; 19 | } 20 | } 21 | } else { 22 | throw new Error('Missing modules\' collection'); 23 | } 24 | }; -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function(config) { 2 | config.set({ 3 | basePath: '.', 4 | frameworks: ['browserify', 'mocha', 'chai'], 5 | files: ['test/**/*.spec.js'], 6 | preprocessors: { 7 | 'test/**/*.spec.js': [ 'browserify' ] 8 | }, 9 | exclude: [], 10 | reporters: ['mocha'], 11 | // web server port 12 | port: 9876, 13 | colors: true, 14 | logLevel: config.LOG_INFO, 15 | autoWatch: false, 16 | browsers: ['PhantomJS'], 17 | singleRun: true, 18 | client: { 19 | captureConsole: true 20 | }, 21 | browserify: { 22 | debug: false, 23 | insertGlobals: false, 24 | detectGlobals: true, 25 | noBuiltins: true, 26 | transform: [] 27 | } 28 | }); 29 | } -------------------------------------------------------------------------------- /test/Hackerify.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var chai = require('chai'); 4 | var expect = chai.expect; 5 | 6 | var Hackerify = require('../index'); 7 | Hackerify(arguments, { 8 | './A': 'H.a.c.k.e.r.f.y', 9 | './B': { 10 | what: 'A node module.', 11 | description: function() { 12 | return 'My job is stubbing required modules within Browserify.'; 13 | } 14 | }, 15 | './D': { 16 | loc: function() { 17 | return 24; 18 | } 19 | } 20 | }); 21 | 22 | describe('Given a browserify bundle', function() { 23 | 24 | it('With stubbing', function(done) { 25 | var app = require('./data/app'); 26 | expect(app()).to.be.equal('I am H.a.c.k.e.r.f.y. A node module. My job is stubbing required modules within Browserify. LOC: 24'); 27 | done(); 28 | }); 29 | 30 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hackerify", 3 | "version": "1.0.1", 4 | "description": "~30 lines of JavaScript for stubbing Browserify modules", 5 | "main": "./index.js", 6 | "author": { 7 | "name": "Krasimir Tsonev", 8 | "email": "info@krasimirtsonev.com", 9 | "url": "http://krasimirtsonev.com" 10 | }, 11 | "license": "MIT", 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "browserify": "10.2.3", 15 | "karma": "0.12.36", 16 | "mocha": "2.2.5", 17 | "karma-chai": "0.1.0", 18 | "karma-chrome-launcher": "0.1.12", 19 | "karma-mocha": "0.1.10", 20 | "karma-mocha-reporter": "1.0.2", 21 | "karma-phantomjs-launcher": "0.1.4", 22 | "karma-spec-reporter": "0.0.19", 23 | "karma-browserify": "4.2.1" 24 | }, 25 | "keywords": [ 26 | "browserify", 27 | "stub", 28 | "mock", 29 | "modules", 30 | "require" 31 | ], 32 | "repository": { 33 | "type": "git", 34 | "url": "https://github.com/krasimir/hackerify.git" 35 | }, 36 | "scripts": { 37 | "test": "./node_modules/.bin/karma start karma.conf.js" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Krasimir Tsonev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hackerify 2 | 3 | * ~30 lines of JavaScript for stubbing Browserify modules. 4 | * Defining the stubs in only one place. Works for modules required deeply in your app. 5 | 6 | ## Instalation 7 | 8 | `npm install hackerify` 9 | 10 | ## Usage 11 | 12 | Let's say that you have the following modules: 13 | 14 | ```js 15 | // label.js 16 | module.exports = function() { 17 | return 'The answer is: '; 18 | } 19 | 20 | // answer.js 21 | var label = require('./label'); 22 | module.exports = function() { 23 | return label() + 42; 24 | } 25 | 26 | // app.js 27 | var a = require('./answer'); 28 | console.log(a()); 29 | ``` 30 | 31 | And you want to stub the function in `label.js`. All you have to do is adding the following code in `app.js`. 32 | 33 | ```js 34 | // app.js 35 | 36 | var Hackerify = require('hackerify'); 37 | Hackerify(arguments, { 38 | './label': function() { 39 | return 'The answer is not always '; 40 | } 41 | }); 42 | 43 | var a = require('./answer'); 44 | console.log(a()); 45 | ``` 46 | 47 | Notice the `arguments` variable. No, that's not a typo. When you use Browserify your code is put in a closure. So `arguments` is refering the arguments passed to that closure. 48 | 49 | ## Testing 50 | 51 | * `npm install` 52 | * `npm test` 53 | 54 | ## The example 55 | 56 | * `npm install` 57 | * `cd ./example` 58 | * `sh ./run.sh` 59 | * Open `./example/index.html` in a browser -------------------------------------------------------------------------------- /example/bundle.js: -------------------------------------------------------------------------------- 1 | (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o