From bbc1682525ae539bdff022960b0411eac0f363b6 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Mon, 8 Aug 2016 00:26:24 -0700 Subject: [PATCH] avoid define() calls in dependencies; closes #2424 (#2425) --- Makefile | 1 + mocha.js | 4 ++-- scripts/dedefine.js | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 scripts/dedefine.js diff --git a/Makefile b/Makefile index 73e941ef13..c4cefe8f2f 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ all: mocha.js mocha.js: $(SRC) browser-entry.js @printf "==> [Browser :: build]\n" $(BROWSERIFY) ./browser-entry \ + --plugin ./scripts/dedefine \ --ignore 'fs' \ --ignore 'glob' \ --ignore 'path' \ diff --git a/mocha.js b/mocha.js index f98c2eca50..51dc5ee017 100644 --- a/mocha.js +++ b/mocha.js @@ -9314,7 +9314,7 @@ function objectToString(o) { /*global module */ if (typeof module !== 'undefined' && module.exports) { module.exports = JsDiff; - } else if (typeof define === 'function' && define.amd) { + } else if (false) { /*global define */ define([], function() { return JsDiff; }); } else if (typeof global.JsDiff === 'undefined') { @@ -10076,7 +10076,7 @@ module.exports = Array.isArray || function (arr) { ;(function () { // Detect the `define` function exposed by asynchronous module loaders. The // strict `define` check is necessary for compatibility with `r.js`. - var isLoader = typeof define === "function" && define.amd; + var isLoader = false; // A set of types used to distinguish objects from primitives. var objectTypes = { diff --git a/scripts/dedefine.js b/scripts/dedefine.js new file mode 100644 index 0000000000..c647c0e083 --- /dev/null +++ b/scripts/dedefine.js @@ -0,0 +1,26 @@ +'use strict'; + +/** + * This is a transform stream we're using to strip AMD calls from + * dependencies in our Browserify bundle. + */ + +var through = require('through2'); +var defineRx = /typeof define === ['"]function['"] && define\.amd/g; + +function createStream() { + return through.obj(function(chunk, enc, next) { + this.push(String(chunk) + .replace(defineRx, 'false')); + next(); + }); +} + +module.exports = function(b) { + function wrap() { + b.pipeline.get('wrap').push(createStream()); + } + + b.on('reset', wrap); + wrap(); +};