Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add check for file hash check (#299)
  • Loading branch information
Torben Rahbek Koch committed Sep 4, 2016
commit 36f9d1a2709d4bb826b18ee432aab5146e8ceb5c
53 changes: 27 additions & 26 deletions src/React.Core/Babel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,35 +351,36 @@ string filename
{
var outputPath = GetOutputPath(filename);
var contents = _fileSystem.ReadAsString(filename);
if (MustTranspileFile(contents, outputPath))
{
var result = TransformWithHeader(filename, contents, null);
if (CacheIsValid(contents, outputPath))
return outputPath;

var result = TransformWithHeader(filename, contents, null);

var sourceMapPath = GetSourceMapOutputPath(filename);
_fileSystem.WriteAsString(outputPath, result.Code);
_fileSystem.WriteAsString(sourceMapPath, result.SourceMap == null ? string.Empty : result.SourceMap.ToJson());
}
return outputPath;
var sourceMapPath = GetSourceMapOutputPath(filename);
_fileSystem.WriteAsString(outputPath, result.Code);
_fileSystem.WriteAsString(sourceMapPath, result.SourceMap == null ? string.Empty : result.SourceMap.ToJson());
return outputPath;
}

/// <summary>
/// Checks whether an input file (given as inputFileContents) should be transpiled
/// by calculating the hash and comparing it to the hash value stored
/// in the file given by outputPath. If the outputPath file does not
/// exist the input file should always be transpiled.
/// </summary>
/// <param name="inputFileContents">The contents of the input file.</param>
/// <param name="outputPath">The output path of the (possibly previously) generated file.</param>
/// <returns>Returns true if the file should be transpiled, false otherwise.</returns>
public virtual bool MustTranspileFile(string inputFileContents, string outputPath)
{
if (!File.Exists(outputPath))
return true;
/// <summary>
/// Checks whether an input file (given as inputFileContents) should be transpiled
/// by calculating the hash and comparing it to the hash value stored
/// in the file given by outputPath. If the outputPath file does not
/// exist the input file should always be transpiled.
/// </summary>
/// <param name="inputFileContents">The contents of the input file.</param>
/// <param name="outputPath">The output path of the (possibly previously) generated file.</param>
/// <returns>Returns true if the file should be transpiled, false otherwise.</returns>
public virtual bool CacheIsValid(string inputFileContents, string outputPath)
{
if (!_fileSystem.FileExists(outputPath))
return false;

var hashForInputFile = _fileCacheHash.CalculateHash(inputFileContents);
var existingOutputContents = _fileSystem.ReadAsString(outputPath);
var fileHasNotChanged = _fileCacheHash.ValidateHash(existingOutputContents, hashForInputFile);
return !fileHasNotChanged;
}
var hashForInputFile = _fileCacheHash.CalculateHash(inputFileContents);
var existingOutputContents = _fileSystem.ReadAsString(outputPath);
var fileHasNotChanged = _fileCacheHash.ValidateHash(existingOutputContents, hashForInputFile);
return fileHasNotChanged;
}
}
}
28 changes: 28 additions & 0 deletions src/React.Tests/Core/BabelTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public void SetUp()
_fileSystem = new Mock<IFileSystem>();
_fileSystem.Setup(x => x.MapPath(It.IsAny<string>())).Returns<string>(x => x);

// Per default the output file should not exist, then individual tests
// can choose otherwise.
_fileSystem.Setup(x => x.FileExists(It.IsAny<string>())).Returns(false);

_fileCacheHash = new Mock<IFileCacheHash>();

_babel = new Babel(
Expand Down Expand Up @@ -150,8 +154,32 @@ public void ShouldSaveTransformationResult()
var resultFilename = _babel.TransformAndSaveFile("foo.jsx");
Assert.AreEqual("foo.generated.js", resultFilename);
StringAssert.EndsWith("React.DOM.div('Hello World')", result);
System.IO.File.WriteAllText("c:\\temp\\foo.generated.js", result, System.Text.Encoding.UTF8);
}

[Test]
public void ShouldSkipTransformationIfCacheIsValid()
{
_fileSystem.Setup(x => x.ReadAsString("foo.jsx")).Returns("<div>Hello World</div>");
_fileSystem.Setup(x => x.FileExists(It.IsAny<string>())).Returns(true);
_fileCacheHash.Setup(x => x.ValidateHash(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
_environment.Setup(x => x.ExecuteWithBabel<JavaScriptWithSourceMap>(
"ReactNET_transform_sourcemap",
It.IsAny<string>(),
It.IsAny<string>(), // Babel config
"foo.jsx" // File name
)).Returns(new JavaScriptWithSourceMap { Code = "React.DOM.div('Hello World')" });

string result = null;
_fileSystem.Setup(x => x.WriteAsString("foo.generated.js", It.IsAny<string>())).Callback(
(string filename, string contents) => result = contents
);

var resultFilename = _babel.TransformAndSaveFile("foo.jsx");
Assert.AreEqual("foo.generated.js", resultFilename);
Assert.IsNull(result, "There should be no result. Cached result should have been used.");
}

private void SetUpEmptyCache()
{
_cache.Setup(x => x.Get<JavaScriptWithSourceMap>("JSX_v3_foo.jsx", null)).Returns((JavaScriptWithSourceMap)null);
Expand Down