forked from arty-name/cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundler.js
72 lines (61 loc) · 1.89 KB
/
bundler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var fs = require('fs');
var through = require('through');
var browserify = require('browserify');
var exorcist = require('exorcist');
function fixGherkinLexers(file) {
var data = '';
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);
}
return through(write, end);
}
function Bundler(bundlePath) {
var mapPath = bundlePath + '.map';
var self = {
bundle: function (callback) {
var _callback = callback;
callback = function (err) {
if (_callback) _callback(err);
_callback = null;
};
var operation = browserify({debug: true, standalone: 'Cucumber'})
.transform({global: true}, fixGherkinLexers)
.transform({global:true}, 'uglifyify')
.exclude('./lib/cucumber/cli') // TODO: doesn't work, fix this
.require('./bundle-main', { expose: 'cucumber' })
.bundle()
.pipe(exorcist(mapPath))
.pipe(fs.createWriteStream(bundlePath, 'utf8'));
operation.on('error', function (err) {
callback(err);
});
operation.on('finish', function () {
callback();
});
}
};
return self;
}
module.exports = Bundler;