Skip to content

Commit

Permalink
Skip bundle minifying if all the inputs are older than the minified f…
Browse files Browse the repository at this point in the history
…ile (#438)

* Skip bundle minifying if all the inputs are older than the minified file

* Make sure to not use cache if gzip is enabled
  • Loading branch information
NicolasDorier authored and madskristensen committed Aug 30, 2019
1 parent bc7f41f commit 122b9f2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/BundlerMinifier.Core/Bundle/Bundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public bool OutputIsMinFile
get { return Path.GetFileName(OutputFileName).Contains(".min."); }
}

public DateTime MostRecentWrite { get; set; }

/// <summary>
/// Converts the relative output file to an absolute file path.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/BundlerMinifier.Core/Bundle/BundleFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,14 @@ private bool ProcessBundle(string baseFolder, Bundle bundle)

if (bundle.IsMinificationEnabled || bundle.IsGzipEnabled)
{
var outputWriteTime = File.GetLastWriteTimeUtc(minFile);
if (!bundle.IsGzipEnabled && bundle.MostRecentWrite < outputWriteTime)
return false;
var result = BundleMinifier.MinifyBundle(bundle);

// If no change is detected, then the minFile is not modified, so we need to update the write time manually
if (!result.Changed && File.Exists(minFile))
File.SetLastWriteTimeUtc(minFile, DateTime.UtcNow);
changed |= result.Changed;

if (bundle.IsMinificationEnabled && bundle.SourceMap && !string.IsNullOrEmpty(result.SourceMap))
Expand Down
5 changes: 5 additions & 0 deletions src/BundlerMinifier.Core/Bundle/BundleHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static IEnumerable<Bundle> GetBundles(string configFile)

public static void ProcessBundle(string baseFolder, Bundle bundle)
{
DateTime mostRecentWrite = default(DateTime);
StringBuilder sb = new StringBuilder();
List<string> inputFiles = bundle.GetAbsoluteInputFiles();

Expand All @@ -102,6 +103,9 @@ public static void ProcessBundle(string baseFolder, Bundle bundle)
{
content = FileHelpers.ReadAllText(file);
}
var lastWriteFile = System.IO.File.GetLastWriteTimeUtc(file);
if (mostRecentWrite < lastWriteFile)
mostRecentWrite = lastWriteFile;

// adding new line only if there are more than 1 files
// otherwise we are preserving file integrity
Expand All @@ -112,6 +116,7 @@ public static void ProcessBundle(string baseFolder, Bundle bundle)
}
}

bundle.MostRecentWrite = mostRecentWrite;
bundle.Output = sb.ToString();
}

Expand Down
1 change: 0 additions & 1 deletion src/BundlerMinifier.Core/Minify/BundleMinifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public static MinificationResult MinifyBundle(Bundle bundle)
string file = bundle.GetAbsoluteOutputFile();
string extension = Path.GetExtension(file).ToUpperInvariant();
var minResult = new MinificationResult(file, null, null);

if (!string.IsNullOrEmpty(bundle.Output) && bundle.IsMinificationEnabled)
{
try
Expand Down

0 comments on commit 122b9f2

Please sign in to comment.