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

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

Loading…
Cancel
Save