update script loading

pull/702/head
Luke Pulverenti 9 years ago
parent 52a4367f58
commit 03eee93b4e

@ -42,12 +42,7 @@ namespace MediaBrowser.WebDashboard.Api
{
Stream resourceStream;
if (path.Equals("scripts/all.js", StringComparison.OrdinalIgnoreCase))
{
resourceStream = await GetAllJavascript(mode, localizationCulture, appVersion, enableMinification).ConfigureAwait(false);
enableMinification = false;
}
else if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase))
if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase))
{
resourceStream = await GetAllCss(enableMinification).ConfigureAwait(false);
enableMinification = false;
@ -441,123 +436,41 @@ namespace MediaBrowser.WebDashboard.Api
{
var builder = new StringBuilder();
var versionString = !string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase) ? "?v=" + version : string.Empty;
var files = new List<string>
{
"scripts/all.js" + versionString
};
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
{
files.Insert(0, "cordova.js");
}
var tags = files.Select(s => string.Format("<script src=\"{0}\" async></script>", s)).ToArray();
builder.Append(string.Join(string.Empty, tags));
return builder.ToString();
}
/// <summary>
/// Gets a stream containing all concatenated javascript
/// </summary>
/// <returns>Task{Stream}.</returns>
private async Task<Stream> GetAllJavascript(string mode, string culture, string version, bool enableMinification)
{
var memoryStream = new MemoryStream();
var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);
await AppendResource(memoryStream, "bower_components/jquery/dist/jquery.min.js", newLineBytes).ConfigureAwait(false);
//await AppendLocalization(memoryStream, culture, excludePhrases).ConfigureAwait(false);
await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
builder.Append("<script>");
if (!string.IsNullOrWhiteSpace(mode))
{
var appModeBytes = Encoding.UTF8.GetBytes(string.Format("window.appMode='{0}';", mode));
await memoryStream.WriteAsync(appModeBytes, 0, appModeBytes.Length).ConfigureAwait(false);
builder.AppendFormat("window.appMode='{0}';", mode);
}
// Write the version string for the dashboard comparison function
var versionString = string.Format("window.dashboardVersion='{0}';", version);
var versionBytes = Encoding.UTF8.GetBytes(versionString);
builder.AppendFormat("window.dashboardVersion='{0}';", version);
builder.Append("</script>");
await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);
await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
var builder = new StringBuilder();
var versionString = !string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase) ? "?v=" + version : string.Empty;
var commonFiles = new[]
var files = new List<string>
{
"bower_components/requirejs/require.js"
}.ToList();
"bower_components/requirejs/require.js" + versionString,
"scripts/site.js" + versionString
};
foreach (var file in commonFiles)
if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
{
using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath(file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
{
using (var streamReader = new StreamReader(fs))
{
var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
builder.Append(text);
builder.Append(Environment.NewLine);
}
}
files.Insert(0, "cordova.js");
}
foreach (var file in GetScriptFiles())
var tags = files.Select(s =>
{
var path = GetDashboardResourcePath("scripts/" + file);
using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
if (s.IndexOf("require", StringComparison.OrdinalIgnoreCase) == -1)
{
using (var streamReader = new StreamReader(fs))
{
var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
builder.Append(text);
builder.Append(Environment.NewLine);
}
return string.Format("<script src=\"{0}\" async></script>", s);
}
}
var js = builder.ToString();
if (enableMinification)
{
try
{
var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);
return string.Format("<script src=\"{0}\"></script>", s);
if (result.Errors.Count > 0)
{
_logger.Error("Error minifying javascript: " + result.Errors[0].Message);
}
else
{
js = result.MinifiedContent;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error minifying javascript", ex);
}
}
}).ToArray();
var bytes = Encoding.UTF8.GetBytes(js);
await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
builder.Append(string.Join(string.Empty, tags));
memoryStream.Position = 0;
return memoryStream;
}
private IEnumerable<string> GetScriptFiles()
{
return new[]
{
"extensions.js",
"site.js"
};
return builder.ToString();
}
/// <summary>

@ -1009,9 +1009,6 @@
<Content Include="dashboard-ui\css\tileitem.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\css\userimage.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\edititemmetadata.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

Loading…
Cancel
Save