From 101fa85a23641eb48588ca41d001de1e2eb007ad Mon Sep 17 00:00:00 2001 From: Eric Lanehart Date: Thu, 6 Oct 2016 12:17:12 -0400 Subject: [PATCH] Invalidate all cached preprocessors on changes (#145) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Targeting only changed files doesn’t invalidate cached parent modules that are entry points for views Fixes #144 --- .editorconfig | 16 ++++++++++++++++ lib/server.js | 8 ++++++-- test/solidus.js | 25 ++++++++++++++++++++----- 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ec79ee5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.js] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/lib/server.js b/lib/server.js index afcd3c4..9b45604 100644 --- a/lib/server.js +++ b/lib/server.js @@ -322,9 +322,13 @@ var SolidusServer = function( options ){ // updates the source of a preprocessor this.updatePreprocessor = function( preprocessor_path ){ + var preprocessor_paths = _.values(preprocessors).map(function (preprocessor) { + return preprocessor.path + }); - delete require.cache[require.resolve(preprocessor_path)]; - + _.each(preprocessor_paths, function ( path ){ + delete require.cache[path]; + }); }; // removes an existing preprocessor object diff --git a/test/solidus.js b/test/solidus.js index 1f1b006..dbc2645 100644 --- a/test/solidus.js +++ b/test/solidus.js @@ -1042,18 +1042,33 @@ describe( 'Solidus', function(){ }, FILESYSTEM_DELAY ); }); - var test_preprocessor_contents_2 = 'module.exports=function(context){context.test2 = true;return context;};'; + var parent_preprocessor_contents = 'var child = require("./child");module.exports=function(context){context.test = child();return context;};'; + var child_preprocessor_contents = 'module.exports=function(){return "ok";};'; + var child_preprocessor_contents_2 = 'module.exports=function(){return "okok";};'; - it( 'Updates preprocessors when their files change', function( done ){ + it( 'Invalidates all cached preprocessor modules on a change to any of them', function( done ){ + this.timeout(FILESYSTEM_DELAY * 2.5); var s_request = request( solidus_server.router ); - fs.writeFileSync( 'preprocessors/test.js', test_preprocessor_contents_2, DEFAULT_ENCODING ); + fs.writeFileSync( 'preprocessors/test.js', parent_preprocessor_contents, DEFAULT_ENCODING ); + fs.writeFileSync( 'preprocessors/child.js', child_preprocessor_contents, DEFAULT_ENCODING ); setTimeout( function(){ s_request.get('/test.json') .expect( 200 ) .end( function( err, res ){ if( err ) throw err; - assert( res.body.test2 ); - done(); + assert( res.body.test === 'ok' ); + + fs.writeFileSync( 'preprocessors/child.js', child_preprocessor_contents_2, DEFAULT_ENCODING ); + + setTimeout( function(){ + s_request.get('/test.json') + .expect( 200 ) + .end( function( err, res ){ + if( err ) throw err; + assert( res.body.test === 'okok' ); + done(); + }); + }, FILESYSTEM_DELAY ); }); }, FILESYSTEM_DELAY ); });