Skip to content

Commit a81e555

Browse files
ndhouleJoshua Appelman
authored and
Joshua Appelman
committed
Build using Browserify
Signed-off-by: Joshua Appelman <[email protected]> Closes #1193
1 parent 2b2c576 commit a81e555

16 files changed

+9461
-2994
lines changed

Makefile

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1+
BROWSERIFY := node_modules/.bin/browserify
12
ESLINT := node_modules/.bin/eslint
3+
24
REPORTER ?= spec
35
TM_BUNDLE = JavaScript\ mocha.tmbundle
46
SRC = $(shell find lib -name "*.js" -type f | sort)
57
SUPPORT = $(wildcard support/*.js)
68

79
all: mocha.js
810

9-
lib/browser/diff.js: node_modules/diff/diff.js
10-
cp node_modules/diff/diff.js lib/browser/diff.js
11-
12-
lib/browser/escape-string-regexp.js: node_modules/escape-string-regexp/index.js
13-
cp node_modules/escape-string-regexp/index.js lib/browser/escape-string-regexp.js
14-
15-
mocha.js: $(SRC) $(SUPPORT) lib/browser/diff.js lib/browser/escape-string-regexp.js
16-
@node support/compile $(SRC)
17-
@cat \
18-
support/head.js \
19-
_mocha.js \
20-
support/tail.js \
21-
support/foot.js \
22-
> mocha.js
11+
# TODO: Remove filesize echos, just for comparison during browserify transition
12+
mocha.js: $(SRC) $(SUPPORT)
13+
@$(BROWSERIFY) ./support/browser-entry \
14+
--ignore 'fs' \
15+
--ignore 'glob' \
16+
--ignore 'jade' \
17+
--ignore 'path' \
18+
--ignore 'supports-color' \
19+
--exclude './lib-cov/mocha' > $@
2320

2421
clean:
2522
rm -f mocha.js

lib/browser/escape-string-regexp.js

-21
This file was deleted.

lib/browser/fs.js

Whitespace-only changes.

lib/browser/glob.js

Whitespace-only changes.

lib/browser/path.js

Whitespace-only changes.

lib/mocha.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var escapeRe = require('escape-string-regexp');
1212
var path = require('path');
13+
var reporters = require('./reporters');
1314
var utils = require('./utils');
1415

1516
/**
@@ -22,7 +23,7 @@ exports = module.exports = Mocha;
2223
* To require local UIs and reporters when running in node.
2324
*/
2425

25-
if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
26+
if (!process.browser) {
2627
var cwd = process.cwd();
2728
module.paths.push(cwd, path.join(cwd, 'node_modules'));
2829
}
@@ -33,7 +34,7 @@ if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
3334

3435
exports.utils = utils;
3536
exports.interfaces = require('./interfaces');
36-
exports.reporters = require('./reporters');
37+
exports.reporters = reporters;
3738
exports.Runnable = require('./runnable');
3839
exports.Context = require('./context');
3940
exports.Runner = require('./runner');
@@ -150,11 +151,11 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
150151
} else {
151152
reporter = reporter || 'spec';
152153
var _reporter;
153-
try {
154-
_reporter = require('./reporters/' + reporter);
155-
} catch (err) {
156-
// Ignore
154+
// Try to load a built-in reporter.
155+
if (reporters[reporter]) {
156+
_reporter = reporters[reporter];
157157
}
158+
// Try to load reporters from process.cwd() and node_modules
158159
if (!_reporter) {
159160
try {
160161
_reporter = require(reporter);

lib/reporters/base.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var tty = require('tty');
66
var diff = require('diff');
77
var ms = require('../ms');
88
var utils = require('../utils');
9-
var supportsColor = process.env ? require('supports-color') : null;
9+
var supportsColor = process.browser ? null : require('supports-color');
1010

1111
/**
1212
* Expose `Base`.
@@ -37,9 +37,7 @@ var isatty = tty.isatty(1) && tty.isatty(2);
3737
* Enable coloring by default, except in the browser interface.
3838
*/
3939

40-
exports.useColors = process.env
41-
? (supportsColor || (process.env.MOCHA_COLORS !== undefined))
42-
: false;
40+
exports.useColors = !process.browser && (supportsColor || (process.env.MOCHA_COLORS !== undefined));
4341

4442
/**
4543
* Inline diffs instead of +/-

lib/reporters/html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
var Base = require('./base');
88
var utils = require('../utils');
99
var Progress = require('../browser/progress');
10-
var escapeRe = require('../browser/escape-string-regexp');
10+
var escapeRe = require('escape-string-regexp');
1111
var escape = utils.escape;
1212

1313
/**

lib/reporters/index.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
exports.Base = require('./base');
2-
exports.Dot = require('./dot');
3-
exports.Doc = require('./doc');
4-
exports.TAP = require('./tap');
5-
exports.JSON = require('./json');
6-
exports.HTML = require('./html');
7-
exports.List = require('./list');
8-
exports.Min = require('./min');
9-
exports.Spec = require('./spec');
10-
exports.Nyan = require('./nyan');
11-
exports.XUnit = require('./xunit');
12-
exports.Markdown = require('./markdown');
13-
exports.Progress = require('./progress');
14-
exports.Landing = require('./landing');
15-
exports.JSONCov = require('./json-cov');
16-
exports.HTMLCov = require('./html-cov');
17-
exports.JSONStream = require('./json-stream');
1+
// Alias exports to a their normalized format Mocha#reporter to prevent a need
2+
// for dynamic (try/catch) requires, which Browserify doesn't handle.
3+
exports.Base = exports.base = require('./base');
4+
exports.Dot = exports.dot = require('./dot');
5+
exports.Doc = exports.doc = require('./doc');
6+
exports.TAP = exports.tap = require('./tap');
7+
exports.JSON = exports.json = require('./json');
8+
exports.HTML = exports.html = require('./html');
9+
exports.List = exports.list = require('./list');
10+
exports.Min = exports.min = require('./min');
11+
exports.Spec = exports.spec = require('./spec');
12+
exports.Nyan = exports.nyan = require('./nyan');
13+
exports.XUnit = exports.xunit = require('./xunit');
14+
exports.Markdown = exports.markdown = require('./markdown');
15+
exports.Progress = exports.progress = require('./progress');
16+
exports.Landing = exports.landing = require('./landing');
17+
exports.JSONCov = exports['json-cov'] = require('./json-cov');
18+
exports.HTMLCov = exports['html-cov'] = require('./html-cov');
19+
exports.JSONStream = exports['json-stream'] = require('./json-stream');

lib/runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ function filterLeaks(ok, globals) {
736736
* @return {Array}
737737
*/
738738
function extraGlobals() {
739-
if (typeof process === 'object' && typeof process.version === 'string') {
739+
if (!process.browser) {
740740
var nodeVersion = process.version.split('.').reduce(function(a, v) {
741741
return a << 8 | v;
742742
});

0 commit comments

Comments
 (0)