Skip to content

Commit 8110ffb

Browse files
author
vamship
committed
Initial commit
0 parents  commit 8110ffb

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Vim swap files
2+
*.swp
3+
*.swo
4+
5+
# Node dependencies
6+
node_modules
7+
8+
# Webstorm configuration directory
9+
.idea
10+
11+
# OS/X Directory store
12+
.DS_Store
13+
14+
# Temp directories
15+
.tmp

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# karma-requirejs-wrapper-preprocessor
2+
A Karma preprocessor module that can be used to wrap the contents of files with a 'require() {}' call. While it can technically be used in a standalone manner with any data files, this module has been primarily developed to wrap the result of the ng-html2js processor module, for projects that primarily use RequireJS in conjuction with AngularJS.
3+
4+
# Contents
5+
- [Overview](#overview)
6+
- [Installation](#installation)
7+
- [Usage](#usage)
8+
9+
# Overview
10+
This library was intended to be used in conjunction with karma-ng-html2js-preprocessor, in order to make the output of the preprocessor compatible with RequireJS. This library uses preprocessor chaining on Karma, and will only work with versions 0.9+ of Karma.
11+
12+
# Installation
13+
This library can be installed by using the npm tool as follows:
14+
```shell
15+
npm install karma-requirejs-wrapper-preprocessor
16+
```
17+
> ** NOTE: **
18+
> - Use the `--save` or `--save-dev` option to save the dependency in your project's `package.json` file
19+
20+
## Usage
21+
The following is a code snippet from the karma configuration file:
22+
23+
```javascript
24+
module.exports = function(config) {
25+
config.set({
26+
27+
preprocessors: {
28+
// The processors are chained. The output of html2js goes to requirejs-wrapper.
29+
'**/*.html': [ 'ng-html2js', 'requirejs-wrapper' ]
30+
},
31+
32+
// Configuration for ng-html2js
33+
ngHtml2JsPreprocessor: {
34+
...
35+
},
36+
37+
// Configuration for the requirejs wrapper
38+
requireJsWrapper: {
39+
dependencies: [ 'angular' ]
40+
}
41+
42+
```
43+
### Options
44+
45+
The following options are supported:
46+
`dependencies`: An array of strings that will be used to generate the dependencies for the `require() {}` wrapper. The string values will be used as the names of the dependencies, with each value also used as argument names in the function call.
47+
48+
> ** NOTE: **
49+
> Using whitespaces in the names of the dependencies will break your module!

lib/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* jshint node:true */
2+
'use strict';
3+
4+
module.exports = {
5+
'preprocessor:requirejs-wrapper': ['factory', require('./requirejs-wrapper')]
6+
}

lib/requirejs-wrapper.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* jshint node:true */
2+
'use strict';
3+
4+
function _createPreprocessor(logger, config) {
5+
var log = logger.create('preprocessor.requireJsWrapper');
6+
config = config || {};
7+
8+
if( !(config.dependencies instanceof Array) ||
9+
config.dependencies.length <= 0) {
10+
log.warn('No dependencies specified for requirejs wrapper');
11+
}
12+
var dependencies = config.dependencies || [];
13+
14+
return function(content, file, done) {
15+
var dependencyArgs = dependencies.join(', ');
16+
var quoteChar = (dependencies.length <= 0) ? '': '\'';
17+
log.debug('Generating require js wrapper. Dependencies: %s',
18+
dependencyArgs);
19+
content = 'require([' + quoteChar +
20+
dependencies.join('\', \'') +
21+
quoteChar + '],\nfunction(' +
22+
dependencyArgs +
23+
') {\n' + content + '\n})';
24+
done(content);
25+
};
26+
}
27+
28+
_createPreprocessor.$inject = ['logger', 'config.requireJsWrapper'];
29+
30+
module.exports = _createPreprocessor;

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "karma-requirejs-wrapper-preprocessor",
3+
"version": "0.0.1",
4+
"description": "Karma preprocessor that wraps script contents with a \"require(){}\" call. This is designed for use with karma-ng-html2js-preprocessor, by passing the output of that plugin into this one.",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "mocha --reporter=spec"
8+
},
9+
"keywords": [
10+
"karma-plugin",
11+
"karma-preprocessor",
12+
"requirejs"
13+
],
14+
"author": "Vamshi K Ponnapalli <vamshi.ponnapalli@gmail.com>",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"chai": "^1.9.2",
18+
"mocha": "^1.21.5"
19+
},
20+
"directories": {
21+
"test": "test"
22+
},
23+
"dependencies": {
24+
"chai": "^1.9.2"
25+
},
26+
"repository": {
27+
"type": "git",
28+
"url": "https://github.com/vamship/karma-requirejs-wrapper-preprocessor.git"
29+
},
30+
"bugs": {
31+
"url": "https://github.com/vamship/karma-requirejs-wrapper-preprocessor/issues"
32+
},
33+
"homepage": "https://github.com/vamship/karma-requirejs-wrapper-preprocessor"
34+
}

test/index-spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var _index = require('../lib/index');
2+
var expect = require('chai').expect;
3+
4+
describe('index', function() {
5+
it('should expose the methods/properties required by the interface', function() {
6+
expect(_index).to.have.property('preprocessor:requirejs-wrapper').and.to.be.an('Array');
7+
8+
var ref = _index['preprocessor:requirejs-wrapper'];
9+
expect(ref[0]).to.equal('factory');
10+
expect(ref[1]).to.be.a('function');
11+
});
12+
});

test/requirejs-wrapper-spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var _wrapper = require('../lib/requirejs-wrapper');
2+
var expect = require('chai').expect;
3+
4+
describe('requirejs-wrapper', function() {
5+
var _mockLogger = {
6+
create: function() {
7+
return {
8+
debug: function() {},
9+
warn: function() {}
10+
};
11+
}
12+
};
13+
14+
it('should return a function when invoked with no options', function() {
15+
var processor = _wrapper(_mockLogger);
16+
expect(_wrapper).to.be.a('function');
17+
});
18+
19+
it('should return a function when invoked with empty options', function() {
20+
var processor = _wrapper(_mockLogger, {});
21+
expect(_wrapper).to.be.a('function');
22+
});
23+
24+
it('should return a function when invoked with valid, non empty options', function() {
25+
var processor = _wrapper(_mockLogger, {
26+
dependencies: ['angular', 'jquery']
27+
});
28+
expect(_wrapper).to.be.a('function');
29+
});
30+
31+
describe('processor', function() {
32+
var DEFAULT_FILE = 'foo';
33+
var WRAPPER_TEMPLATE_EMPTY = 'require([${DEPS}],\nfunction(${ARGS}) {\n${CONTENTS}\n})';
34+
var WRAPPER_TEMPLATE = 'require([\'${DEPS}\'],\nfunction(${ARGS}) {\n${CONTENTS}\n})';
35+
var DEFAULT_CONTENTS = '(function(foo, bar) { foo.add(bar, "<div><b>This is a test</b></div>"); }';
36+
37+
it('should wrap the input content with a "require() {}" construct when invoked (no options)', function(done) {
38+
var processor = _wrapper(_mockLogger);
39+
var expectedValue = WRAPPER_TEMPLATE_EMPTY.replace('${DEPS}', '')
40+
.replace('${ARGS}', '')
41+
.replace('${CONTENTS}', DEFAULT_CONTENTS);
42+
processor(DEFAULT_CONTENTS, DEFAULT_FILE, function(contents) {
43+
expect(contents).to.equal(expectedValue);
44+
done();
45+
});
46+
});
47+
48+
it('should wrap the input content with a "require() {}" construct when invoked (empty options)', function(done) {
49+
var processor = _wrapper(_mockLogger, {});
50+
var expectedValue = WRAPPER_TEMPLATE_EMPTY.replace('${DEPS}', '')
51+
.replace('${ARGS}', '')
52+
.replace('${CONTENTS}', DEFAULT_CONTENTS);
53+
processor(DEFAULT_CONTENTS, DEFAULT_FILE, function(contents) {
54+
expect(contents).to.equal(expectedValue);
55+
done();
56+
});
57+
});
58+
59+
it('should wrap the input content with a "require() {}" construct when invoked (non empty dependencies)', function(done) {
60+
var dependencies = [ 'angular', 'jquery' ];
61+
var processor = _wrapper(_mockLogger, {
62+
dependencies: dependencies
63+
});
64+
var expectedValue = WRAPPER_TEMPLATE.replace('${DEPS}', dependencies.join('\', \''))
65+
.replace('${ARGS}', dependencies.join(', '))
66+
.replace('${CONTENTS}', DEFAULT_CONTENTS);
67+
processor(DEFAULT_CONTENTS, DEFAULT_FILE, function(contents) {
68+
expect(contents).to.equal(expectedValue);
69+
done();
70+
});
71+
});
72+
});
73+
74+
});

0 commit comments

Comments
 (0)