Skip to content

Commit

Permalink
Rewrite bundling system (close #186)
Browse files Browse the repository at this point in the history
Browserify has been updated and the whole bundling process was
revisited. Cucumber is now a global function (window.Cucumber) as well
as CucumberHTML. It's also uglified.

Also, all Gherkin lexers are now available in the browser.
  • Loading branch information
jbpros committed Sep 12, 2014
1 parent e941d66 commit c28ee86
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 9,186 deletions.
2 changes: 2 additions & 0 deletions bundle-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.CucumberHTML = require('cucumber-html');
module.exports = require('./lib/cucumber');
87 changes: 68 additions & 19 deletions bundler.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
var fs = require('fs');
var through = require('through');
var browserify = require('browserify');

var Bundler = function () {
var requires = [
__dirname + '/node_modules/cucumber-html'
];
function Bundler() {
var source;

var self = browserify({
mount: '/cucumber.js',
require: requires,
ignore: ['./cucumber/cli', 'connect']
});
var self = {
middleware: function (req, res, next) {
function serveBundle(res) {
console.log("/cucumber.js 200", source.length);
res.setHeader('Content-Type', 'application/javascript');
res.write(source);
res.end();
};

self.addEntry('underscore.js', {dirname: __dirname+"/node_modules/underscore", target: "/node_modules/underscore"});
self.addEntry('lib/gherkin.js', {dirname: __dirname+"/node_modules/gherkin", target: "/node_modules/gherkin"});
self.addEntry('lib/cucumber.js', {dirname: __dirname, target: "/cucumber"});
self.addEntry('lib/gherkin/lexer/en.js', {dirname: __dirname+"/node_modules/gherkin", target: "/node_modules/gherkin/lexer/en"});
if (req.originalUrl === '/cucumber.js') {
if (!source) {
self.bundle(function (err) {
if (err) return next(err);
serveBundle(res);
});
} else {
serveBundle(res);
}
} else {
next();
}
},

self.prepend('(function(context) {');
if (process.env.DEBUG_LEVEL)
self.append("context.cucumberRequire = require;\n");
self.append("context.Cucumber = require('/cucumber');\ncontext.CucumberHTML = require('cucumber-html/src/main/resources/cucumber/formatter/formatter');\n})(window);");
bundle: function (callback) {
var bundler = browserify({debug: true, standalone: 'Cucumber'});
bundler.transform({global: true}, function (file) {
var data = '';
return through(write, end);
function write (buf) { data += buf }
function end () {
var path = __dirname + '/node_modules/gherkin/lib';
var lexersPath = path + '/gherkin/lexer';
if (file === path + '/gherkin.js') {
// Patch gherkin so that all lexers are available statically:
var bufferPrefix = '';
var dirFiles = fs.readdirSync(lexersPath);
dirFiles.forEach(function (dirFile) {
var matches = dirFile.match(/^(.*)\.js$/);
if (matches && !dirFile.match(/\.min\.js$/)) {
bufferPrefix += "require('./gherkin/lexer/" + matches[1] + "');\n";
}
});
data = bufferPrefix + data;
}
var ignoredFiles = [
__dirname + '/lib/cucumber/cli.js'
];
if (ignoredFiles.indexOf(file) > -1) {
data = '';
}
this.queue(data);
this.queue(null);
}
});
bundler.transform({global:true}, 'uglifyify');
bundler.exclude('./lib/cucumber/cli'); // TODO: doesn't work, fix this
bundler.require('./bundle-main', { expose: 'cucumber' });
bundler.bundle(function (err, _source, map) {
if (err) return callback(err);
source = _source;
callback(null, source);
});
}
};

return self;
};
}

module.exports = Bundler;
module.exports = Bundler;
7 changes: 3 additions & 4 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function($) {
var CucumberHTMLListener = function($root) {
var CucumberHTML = window.CucumberHTML;
var formatter = new CucumberHTML.DOMFormatter($root);

function CucumberHTMLListener($root) {
var formatter = new CucumberHTML.DOMFormatter($root);

formatter.uri('report.feature');

Expand Down Expand Up @@ -70,7 +70,6 @@
};

function runFeature() {
var Cucumber = window.Cucumber;
var supportCode;
var output = $('#output');
var errors = $('#errors');
Expand Down
14 changes: 10 additions & 4 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ <h2>Feature source</h2>
When I increment the variable by 1
Then the variable should contain 2

Scenario: much more complex stuff
Given a variable set to 100
When I increment the variable by 6
Then the variable should contain 106</textarea>
Scenario Outline: much more complex stuff
Given a variable set to <var>
When I increment the variable by <increment>
Then the variable should contain <result>

Examples:
| var | increment | result |
| 100 | 5 | 105 |
| 99 | 1234 | 1333 |
| 12 | 5 | 18 |</textarea>
<h2>Step definitions</h2>
<textarea id="step-definitions">///// Your World /////

Expand Down
9 changes: 6 additions & 3 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ var bundler = Bundler();
var port = process.env.PORT || 9797;
var app = connect();
app.use(serveStatic(__dirname));
app.use(bundler);
app.use(bundler.middleware);

http.createServer(app).listen(port);
console.log('Accepting connections on port ' + port + '...');
console.log('Bundling Cucumber.js...');
bundler.bundle(function (err, source) {
http.createServer(app).listen(port);
console.log('Accepting connections on port ' + port + '...');
});
3 changes: 1 addition & 2 deletions lib/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var Cucumber = function(featureSource, supportCodeInitializer, options) {
};
Cucumber.Api = require('./cucumber/api');
Cucumber.Ast = require('./cucumber/ast');
// browserify won't load ./cucumber/cli and throw an exception:
try { Cucumber.Cli = require('./cucumber/cli'); } catch(e) {}
Cucumber.Cli = require('./cucumber/cli');
Cucumber.Debug = require('./cucumber/debug'); // Untested namespace
Cucumber.Listener = require('./cucumber/listener');
Cucumber.Parser = require('./cucumber/parser');
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"node": "0.6 || 0.8 || 0.10 || 0.11"
},
"dependencies": {
"browserify": "1.15.5",
"browserify": "5.11.1",
"coffee-script": "1.8.0",
"cucumber-html": "0.2.3",
"gherkin": "2.12.2",
Expand All @@ -80,7 +80,9 @@
"jasmine-node": "1.14.5",
"mkdirp": "0.3.5",
"rimraf": "2.2.8",
"serve-static": "1.6.1"
"serve-static": "1.6.1",
"through": "2.3.4",
"uglifyify": "2.5.0"
},
"scripts": {
"test": "./bin/cucumber.js && jasmine-node spec"
Expand Down
Loading

0 comments on commit c28ee86

Please sign in to comment.