Skip to content

Commit

Permalink
Invalidate all cached preprocessors on changes (#145)
Browse files Browse the repository at this point in the history
Targeting only changed files doesn’t invalidate cached parent modules that are entry points for views

Fixes #144
  • Loading branch information
pushred authored Oct 6, 2016
1 parent 52af4a3 commit 101fa85
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions test/solidus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
});
Expand Down

0 comments on commit 101fa85

Please sign in to comment.