Пример #1
0
        private static Type GetAppComponentType(CustomAssemblyLoader assemblyLoader, string typeNameOfRootComponent, Assembly appAssembly)
        {
            var rootComponentAssembly = appAssembly;

            var rootComponentTypeNameParts = typeNameOfRootComponent.Split(',');
            var rootComponentTypeName      = rootComponentTypeNameParts[0].Trim();
            var rootComponentAssemblyName  = rootComponentTypeNameParts.Length > 1 ? rootComponentTypeNameParts[1].Trim() : "";

            if (rootComponentAssemblyName != "")
            {
                rootComponentAssembly = assemblyLoader.LoadAssembly(rootComponentAssemblyName);
                if (rootComponentAssembly == null)
                {
                    throw new ArgumentException($"The assembly that has component type \"{typeNameOfRootComponent}\" colud not load.");
                }
            }

            var appComponentType = rootComponentAssembly.GetType(rootComponentTypeName);

            if (appComponentType == null)
            {
                var assemblies = appAssembly.GetReferencedAssemblies()
                                 .Where(asmname => !string.IsNullOrEmpty(asmname.Name))
                                 .Where(asmname => !asmname.Name !.StartsWith("Microsoft."))
                                 .Where(asmname => !asmname.Name !.StartsWith("System."))
                                 .Select(asmname => assemblyLoader.LoadAssembly(asmname.Name !))
                                 .Where(asm => asm != null)
                                 .Prepend(appAssembly) as IEnumerable <Assembly>;

                appComponentType = assemblies
                                   .SelectMany(asm => asm.GetTypes())
                                   .Where(t => t.Name == "App")
                                   .Where(t => t.IsSubclassOf(typeof(ComponentBase)))
                                   .FirstOrDefault();
            }

            if (appComponentType == null)
            {
                throw new ArgumentException($"The component type \"{typeNameOfRootComponent}\" was not found.");
            }
            return(appComponentType);
        }
Пример #2
0
        internal static BlazorWasmPrerenderingOptions BuildPrerenderingOptions(CustomAssemblyLoader assemblyLoader, CommandLineOptions commandLineOptions)
        {
            if (string.IsNullOrEmpty(commandLineOptions.IntermediateDir))
            {
                throw new ArgumentException("The -i|--intermediatedir parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.PublishedDir))
            {
                throw new ArgumentException("The -p|--publisheddir parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.AssemblyName))
            {
                throw new ArgumentException("The -a|--assemblyname parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.TypeNameOfRootComponent))
            {
                throw new ArgumentException("The -t|--typenameofrootcomponent parameter is required.");
            }
            if (string.IsNullOrEmpty(commandLineOptions.SelectorOfRootComponent))
            {
                throw new ArgumentException("The --selectorofrootcomponent parameter is required.");
            }
#if ENABLE_HEADOUTLET
            if (string.IsNullOrEmpty(commandLineOptions.SelectorOfHeadOutletComponent))
            {
                throw new ArgumentException("The --selectorofheadoutletcomponent parameter is required.");
            }
#endif
            if (string.IsNullOrEmpty(commandLineOptions.FrameworkName))
            {
                throw new ArgumentException("The -f|--frameworkname parameter is required.");
            }
            if (commandLineOptions.RenderMode != RenderMode.Static && commandLineOptions.RenderMode != RenderMode.WebAssemblyPrerendered)
            {
                throw new ArgumentException($"The -r|--rendermode parameter value \"{commandLineOptions.RenderMode}\" is not supported. (Only \"Static\" and \"WebAssemblyPrerendered\" are supported.)");
            }

            var webRootPath  = Path.Combine(commandLineOptions.PublishedDir, "wwwroot");
            var frameworkDir = Path.Combine(webRootPath, "_framework");

            var middlewarePackages = MiddlewarePackageReference.Build(folderToScan: frameworkDir, commandLineOptions.MiddlewarePackages);
            var middlewareDllsDir  = PrepareMiddlewareDlls(middlewarePackages, commandLineOptions.IntermediateDir, commandLineOptions.FrameworkName);
            SetupCustomAssemblyLoader(assemblyLoader, webRootPath, middlewareDllsDir);

            var appAssembly = assemblyLoader.LoadAssembly(commandLineOptions.AssemblyName);
            if (appAssembly == null)
            {
                throw new ArgumentException($"The application assembly \"{commandLineOptions.AssemblyName}\" colud not load.");
            }
            var appComponentType = GetAppComponentType(assemblyLoader, commandLineOptions.TypeNameOfRootComponent, appAssembly);

            var indexHtmlPath           = Path.Combine(webRootPath, "index.html");
            var enableGZipCompression   = File.Exists(indexHtmlPath + ".gz");
            var enableBrotliCompression = File.Exists(indexHtmlPath + ".br");

            var htmlFragment = IndexHtmlFragments.Load(indexHtmlPath, commandLineOptions.SelectorOfRootComponent, commandLineOptions.SelectorOfHeadOutletComponent, commandLineOptions.DeleteLoadingContents);
            var options      = new BlazorWasmPrerenderingOptions
            {
                WebRootPath         = webRootPath,
                ApplicationAssembly = appAssembly,

                RootComponentType = appComponentType,
#if ENABLE_HEADOUTLET
                HeadOutletComponentType = typeof(Microsoft.AspNetCore.Components.Web.HeadOutlet),
#endif
                RenderMode            = commandLineOptions.RenderMode,
                IndexHtmlFragments    = htmlFragment,
                DeleteLoadingContents = commandLineOptions.DeleteLoadingContents,

                EnableGZipCompression   = enableGZipCompression,
                EnableBrotliCompression = enableBrotliCompression,
                MiddlewarePackages      = middlewarePackages
            };
            return(options);
        }
Пример #3
0
        private static async Task <StaticlizeCrawlingResult> PreRenderToStaticFilesAsync(CommandLineOptions commandLineOptions, CustomAssemblyLoader assemblyLoader, BlazorWasmPrerenderingOptions prerenderingOptions)
        {
            using var webHost = await ServerSideRenderingWebHost.StartWebHostAsync(
                      assemblyLoader,
                      commandLineOptions.Environment,
                      prerenderingOptions);

            var server        = webHost.Services.GetRequiredService <IServer>();
            var baseAddresses = server.Features.Get <IServerAddressesFeature>() !.Addresses;
            var baseUrl       = baseAddresses.First();

            Console.WriteLine($"Start fetching...[{baseUrl}]");

            var crawler = new StaticlizeCrawler(
                baseUrl,
                commandLineOptions.UrlPathToExplicitFetch,
                prerenderingOptions.WebRootPath,
                commandLineOptions.OutputStyle,
                prerenderingOptions.EnableGZipCompression,
                prerenderingOptions.EnableBrotliCompression);
            var crawlingResult = await crawler.SaveToStaticFileAsync();


            if (crawlingResult != StaticlizeCrawlingResult.Nothing && !commandLineOptions.KeepRunning)
            {
                Console.WriteLine();
                Console.WriteLine("INFORMATION");
                Console.WriteLine("===========");
                Console.WriteLine("The crawler encountered errors and/or warnings.");
                Console.WriteLine("If you want to keep running the pre-rendering server process for debugging it on live, you can do that by setting the \"BlazorWasmPrerenderingKeepServer\" MSBuild property to \"true\".");
                Console.WriteLine();
                Console.WriteLine("ex) dotnet publish -p:BlazorWasmPrerenderingKeepServer=true");
                Console.WriteLine();
            }

            Console.WriteLine("Fetching complete.");

            await ServiceWorkerAssetsManifest.UpdateAsync(
                prerenderingOptions.WebRootPath,
                commandLineOptions.ServiceWorkerAssetsManifest);

            if (commandLineOptions.KeepRunning)
            {
                Console.WriteLine();
                Console.WriteLine("The pre-rendering server will keep running because the \"-k\" option switch is specified.");
                Console.WriteLine("To stop the pre - rendering server and stop build, press Ctrl + C.");
            }
            else
            {
                await webHost.StopAsync();
            }

            await webHost.WaitForShutdownAsync();

            return(crawlingResult);
        }