Log info message if IL linking is disabled. Fixes #302

This commit is contained in:
Steve Sanderson 2018-03-20 21:36:12 +00:00
parent 3e4ba1a35f
commit cf237707f9
5 changed files with 36 additions and 7 deletions

View File

@ -9,6 +9,7 @@ async function boot() {
// Read startup config from the <script> element that's importing this file
const allScriptElems = document.getElementsByTagName('script');
const thisScriptElem = (document.currentScript || allScriptElems[allScriptElems.length - 1]) as HTMLScriptElement;
const isLinkerEnabled = thisScriptElem.getAttribute('linker-enabled') === 'true';
const entryPointDll = getRequiredBootScriptAttribute(thisScriptElem, 'main');
const entryPointMethod = getRequiredBootScriptAttribute(thisScriptElem, 'entrypoint');
const entryPointAssemblyName = getAssemblyNameFromUrl(entryPointDll);
@ -18,6 +19,10 @@ async function boot() {
.map(s => s.trim())
.filter(s => !!s);
if (!isLinkerEnabled) {
console.info('Blazor is running in dev mode without IL stripping. To make the bundle size significantly smaller, publish the application or see https://go.microsoft.com/fwlink/?linkid=870414');
}
// Determine the URLs of the assemblies we want to load
const loadAssemblyUrls = [entryPointDll]
.concat(referenceAssemblies)

View File

@ -33,6 +33,10 @@ namespace Microsoft.AspNetCore.Blazor.Build.Cli.Commands
var mainAssemblyPath = command.Argument("assembly",
"Path to the assembly containing the entry point of the application.");
var linkerEnabledFlag = command.Option("--linker-enabled",
"If set, specifies that the application is being built with linking enabled.",
CommandOptionType.NoValue);
command.OnExecute(() =>
{
if (string.IsNullOrEmpty(mainAssemblyPath.Value) ||
@ -50,6 +54,7 @@ namespace Microsoft.AspNetCore.Blazor.Build.Cli.Commands
references.Values.ToArray(),
jsReferences.Values.ToArray(),
cssReferences.Values.ToArray(),
linkerEnabledFlag.HasValue(),
outputPath.Value());
return 0;
}

View File

@ -21,6 +21,7 @@ namespace Microsoft.AspNetCore.Blazor.Build
IEnumerable<string> assemblyReferences,
IEnumerable<string> jsReferences,
IEnumerable<string> cssReferences,
bool linkerEnabled,
string outputPath)
{
var template = GetTemplate(path);
@ -30,7 +31,7 @@ namespace Microsoft.AspNetCore.Blazor.Build
}
var assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
var entryPoint = GetAssemblyEntryPoint(assemblyPath);
var updatedContent = GetIndexHtmlContents(template, assemblyName, entryPoint, assemblyReferences, jsReferences, cssReferences);
var updatedContent = GetIndexHtmlContents(template, assemblyName, entryPoint, assemblyReferences, jsReferences, cssReferences, linkerEnabled);
var normalizedOutputPath = Normalize(outputPath);
Console.WriteLine("Writing index to: " + normalizedOutputPath);
File.WriteAllText(normalizedOutputPath, updatedContent);
@ -101,7 +102,8 @@ namespace Microsoft.AspNetCore.Blazor.Build
string assemblyEntryPoint,
IEnumerable<string> assemblyReferences,
IEnumerable<string> jsReferences,
IEnumerable<string> cssReferences)
IEnumerable<string> cssReferences,
bool linkerEnabled)
{
var resultBuilder = new StringBuilder();
@ -141,6 +143,7 @@ namespace Microsoft.AspNetCore.Blazor.Build
assemblyName,
assemblyEntryPoint,
assemblyReferences,
linkerEnabled,
tag.Attributes);
// Emit tags to reference any specified JS/CSS files
@ -198,6 +201,7 @@ namespace Microsoft.AspNetCore.Blazor.Build
string assemblyName,
string assemblyEntryPoint,
IEnumerable<string> binFiles,
bool linkerEnabled,
List<KeyValuePair<string, string>> attributes)
{
var assemblyNameWithExtension = $"{assemblyName}.dll";
@ -211,6 +215,15 @@ namespace Microsoft.AspNetCore.Blazor.Build
attributesDict["entrypoint"] = assemblyEntryPoint;
attributesDict["references"] = referencesAttribute;
if (linkerEnabled)
{
attributesDict["linker-enabled"] = "true";
}
else
{
attributesDict.Remove("linker-enabled");
}
resultBuilder.Append("<script");
foreach (var attributePair in attributesDict)
{

View File

@ -260,8 +260,8 @@
<_BlazorCommonInput Include="@(IntermediateAssembly)" />
<_BlazorCommonInput Include="@(_BlazorDependencyInput)" />
<_BlazorCommonInput Include="$(_BlazorShouldLinkApplicationAssemblies)" />
<_BlazorLinkingOption Condition="_BlazorShouldLinkApplicationAssemblies == ''" Include="false" />
<_BlazorLinkingOption Condition="_BlazorShouldLinkApplicationAssemblies != ''" Include="true" />
<_BlazorLinkingOption Condition="'$(_BlazorShouldLinkApplicationAssemblies)' == ''" Include="false" />
<_BlazorLinkingOption Condition="'$(_BlazorShouldLinkApplicationAssemblies)' != ''" Include="true" />
</ItemGroup>
<Hash ItemsToHash="@(_BlazorCommonInput)">
@ -570,6 +570,7 @@
<BlazorIndexHtmlInput Include="@(BlazorItemOutput->WithMetadataValue('Type','Assembly')->'%(FullPath)')" />
<BlazorIndexHtmlInput Include="@(BlazorPackageJsRef->'%(FullPath)')" />
<BlazorIndexHtmlInput Include="@(BlazorPackageCssRef->'%(FullPath)')" />
<BlazorIndexHtmlInput Include="@(_BlazorLinkingOption)" />
</ItemGroup>
<WriteLinesToFile
@ -594,8 +595,11 @@
<_JsReferences Include="@(BlazorPackageJsRef->'_content/%(SourcePackage)/%(RecursiveDir)%(FileName)%(Extension)')" />
<_CssReferences Include="@(BlazorPackageCssRef->'_content/%(SourcePackage)/%(RecursiveDir)%(FileName)%(Extension)')" />
</ItemGroup>
<PropertyGroup>
<_LinkerEnabledFlag Condition="'$(_BlazorShouldLinkApplicationAssemblies)' != ''">--linker-enabled</_LinkerEnabledFlag>
</PropertyGroup>
<Exec Command="$(BlazorBuildExe) build @(IntermediateAssembly) --html-page &quot;$(BlazorIndexHtml)&quot; @(_AppReferences->'--reference &quot;%(Identity)&quot;', ' ') @(_JsReferences->'--js &quot;%(Identity)&quot;', ' ') @(_CssReferences->'--css &quot;%(Identity)&quot;', ' ') --output &quot;$(BlazorIndexHtmlOutputPath)&quot;" />
<Exec Command="$(BlazorBuildExe) build @(IntermediateAssembly) --html-page &quot;$(BlazorIndexHtml)&quot; @(_AppReferences->'--reference &quot;%(Identity)&quot;', ' ') @(_JsReferences->'--js &quot;%(Identity)&quot;', ' ') @(_CssReferences->'--css &quot;%(Identity)&quot;', ' ') $(_LinkerEnabledFlag) --output &quot;$(BlazorIndexHtmlOutputPath)&quot;" />
<ItemGroup Condition="Exists('$(BlazorIndexHtmlOutputPath)')">
<_BlazorIndex Include="$(BlazorIndexHtmlOutputPath)" />

View File

@ -35,7 +35,8 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
"MyNamespace.MyType::MyMethod",
assemblyReferences,
jsReferences,
cssReferences);
cssReferences,
linkerEnabled: true);
// Act & Assert: Start and end is not modified (including formatting)
Assert.StartsWith(htmlTemplatePrefix, instance);
@ -55,6 +56,7 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
Assert.False(scriptElem.HasAttribute("type"));
Assert.Equal(string.Empty, scriptElem.Attributes["custom1"].Value);
Assert.Equal("value", scriptElem.Attributes["custom2"].Value);
Assert.Equal("true", scriptElem.Attributes["linker-enabled"].Value);
// Assert: Also contains script tags referencing JS files
Assert.Equal(
@ -77,7 +79,7 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test
var cssReferences = new string[] { "my/styles.css" };
var content = IndexHtmlWriter.GetIndexHtmlContents(
htmlTemplate, "MyApp.Entrypoint", "MyNamespace.MyType::MyMethod", assemblyReferences, jsReferences, cssReferences);
htmlTemplate, "MyApp.Entrypoint", "MyNamespace.MyType::MyMethod", assemblyReferences, jsReferences, cssReferences, linkerEnabled: true);
// Assert
Assert.Equal(htmlTemplate, content);