From 075f2c51c36ed9f1f975e9707aefc480bed85c38 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 8 Jul 2013 16:18:25 -0400 Subject: [PATCH 1/2] Create module cache in prelude closure rather than passing it in. This way it's much easier to know how the value of the cache variable was initialized. Were there any use cases that depended on creating the cache elsewhere? --- index.js | 2 +- prelude.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ec1e47a..24a1b47 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,7 @@ module.exports = function (opts) { if (first) this.queue(prelude + '({'); entries = entries.filter(function (x) { return x !== undefined }); - this.queue('},{},' + JSON.stringify(entries) + ')'); + this.queue('},' + JSON.stringify(entries) + ')'); if (sourcemap) this.queue('\n' + sourcemap.comment()); this.queue(null); diff --git a/prelude.js b/prelude.js index ae8e72f..85c0a3c 100644 --- a/prelude.js +++ b/prelude.js @@ -7,9 +7,10 @@ // anything defined in a previous bundle is accessed via the // orig method which is the requireuire for previous bundles -(function(modules, cache, entry) { +(function(modules, entry) { // Save the require from previous bundle to this closure if any var previousRequire = typeof require == "function" && require; + var cache = {}; function newRequire(name, jumped){ if(!cache[name]) { From b52d99420d30259dcfb366622cfc5f6ecf5a082d Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 8 Jul 2013 16:20:52 -0400 Subject: [PATCH 2/2] Expose require.dumpCache() for resetting the module cache. Resetting the module cache is an important point of hygiene when running multiple tests, and sometimes within a single test, because it prevents earlier test activity from interfering with current test behavior. Without modifying prelude.js, it seems to be impossible to modify the cache in any way. Unfortunately, shipping a modified version of prelude.js requires forking both browserify and browser-pack. The other option would be to use something other than browserify for tests, but then the benefits of end-to-end testing the actual browserified bundle are lost. Am I missing an easier way of clearing the cache? --- prelude.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/prelude.js b/prelude.js index 85c0a3c..8861b2f 100644 --- a/prelude.js +++ b/prelude.js @@ -36,6 +36,13 @@ } return cache[name].exports; } + + // A means of resetting the module cache when necessary; for example, + // between test runs. + newRequire.dumpCache = function() { + cache = {}; + }; + for(var i=0;i