Skip to content

Commit

Permalink
fix bundling; closes #3091
Browse files Browse the repository at this point in the history
This became too difficult to manage on the command-line.

What we're trying to do here is ship a version of the `Buffer` shim
which plays well with IE9/IE10.  Browserify ships with a version which
does NOT play well, meaning we have to force it to use the version
we choose (`buffer@4.9.x`).

The fix is in two parts:

1.  `insertGlobalVars` option replaces usages of global `Buffer` with
`require('/path/to/mocha/node_modules/buffer').Buffer`
2.  Any *other* module which explicitly requires `buffer` or, yes,
`buffer/`, must *also* use `/path/to/mocha/node_modules/buffer`

If *both* of these are not in place, Browserify will use its *own*
version of the `buffer` shim.
  • Loading branch information
boneskull committed Dec 12, 2017
1 parent 4c0df70 commit c05db7b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 25 deletions.
10 changes: 2 additions & 8 deletions Makefile
@@ -1,4 +1,4 @@
BROWSERIFY := "node_modules/.bin/browserify"
BROWSERIFY := "scripts/build.js"
KARMA := "node_modules/.bin/karma"
MOCHA := "bin/mocha"
NYC := "node_modules/.bin/nyc"
Expand All @@ -20,13 +20,7 @@ all: mocha.js
mocha.js: $(SRC) browser-entry.js
@printf "==> [Browser :: build]\n"
mkdir -p ${@D}
$(BROWSERIFY) ./browser-entry \
--require buffer/:buffer \
--plugin ./scripts/dedefine \
--ignore 'fs' \
--ignore 'glob' \
--ignore 'path' \
--ignore 'supports-color' > $@
$(BROWSERIFY) > $@

clean:
@printf "==> [Clean]\n"
Expand Down
34 changes: 17 additions & 17 deletions karma.conf.js
Expand Up @@ -4,6 +4,9 @@ var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var baseBundleDirpath = path.join(__dirname, '.karma');
var builder = require('./scripts/build');
var build = builder.build;
var bundlerOptions = builder.options;

var browserPlatformPairs = {
'chrome@latest': 'Windows 8',
Expand Down Expand Up @@ -32,23 +35,20 @@ module.exports = function (config) {
preprocessors: {
'test/**/*.js': ['browserify']
},
browserify: {
debug: true,
configure: function configure (b) {
b.ignore('glob')
.ignore('fs')
.ignore('path')
.ignore('supports-color')
.require(path.join(__dirname, 'node_modules', 'buffer'), {expose: 'buffer'})
.on('bundled', function (err, content) {
if (!err && bundleDirpath) {
// write bundle to directory for debugging
fs.writeFileSync(path.join(bundleDirpath,
'bundle.' + Date.now() + '.js'), content);
}
});
}
},
browserify: Object.assign({insertGlobalVars: bundlerOptions.insertGlobalVars},
{
debug: true,
configure: function configure (b) {
build(b)
.on('bundled', function (err, content) {
if (!err && bundleDirpath) {
// write bundle to directory for debugging
fs.writeFileSync(path.join(bundleDirpath, 'bundle.' +
Date.now() + '.js'), content);
}
});
}
}),
reporters: ['mocha'],
colors: true,
browsers: ['PhantomJS'],
Expand Down
57 changes: 57 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -321,6 +321,7 @@
"supports-color": "4.4.0"
},
"devDependencies": {
"aliasify": "^2.1.0",
"assert": "^1.4.1",
"browserify": "^14.4.0",
"buffer": "^4.9.1",
Expand Down
46 changes: 46 additions & 0 deletions scripts/build.js
@@ -0,0 +1,46 @@
#!/usr/bin/env node
'use strict';

/**
* Mocha's build script which is sadly too complex to manage from the command
* line
* @type {Browserify}
*/

const browserify = require('browserify');
const path = require('path');
const dedefine = require('./dedefine');
const aliasify = require('aliasify');

const options = {
basedir: path.join(__dirname, '..'),
entries: ['./browser-entry.js'],
insertGlobalVars: {
Buffer (file, basedir) {
const filepath = path.join(path.relative(path.dirname(file), basedir),
'node_modules',
'buffer');
return `require('${filepath}').Buffer`;
}
}
};

const build = (b) => b.ignore('fs')
.ignore('glob')
.ignore('path')
.ignore('supports-color')
.transform(aliasify, {
replacements: {
'^buffer/?': () => require.resolve('buffer/index.js')
},
global: true
})
.plugin(dedefine);

exports.build = build;
exports.options = options;

if (require.main === module) {
build(browserify(options)).bundle()
.pipe(process.stdout);
}

0 comments on commit c05db7b

Please sign in to comment.