Example #1
0
        private string GetEntryOutput(IRequestedEntryModel requestedEntry, string sinkName, string dependencyOf = null)
        {
            var finalOutput = "";

            if (_viewData.TryMarkEntryAsSerialized(requestedEntry))
            {
                // Prevent importing if no script requires this one
                if (requestedEntry.RequiredBy.Any())
                {
                    var dependants = requestedEntry.RequiredBy
                                     .Where(dependant => _viewData.TryGetRequestedEntryByName(dependant, out var _));
                    if (!dependants.Any())
                    {
                        if (_settings.PrintComments)
                        {
                            finalOutput += $"<!-- Preventing output of \"{requestedEntry.GetIdentifier()}\" because there are no dependants. -->\n";
                        }
                        return(finalOutput);
                    }
                }

                // Resolve requirements
                foreach (var requirement in requestedEntry.Requires)
                {
                    IRequestedEntryModel requirementEntry;

                    // Check if the dependency has been requested
                    if (!_viewData.TryGetRequestedEntryByName(requirement, out requirementEntry))
                    {
                        // If not, try build a new request for entry from webpack
                        if (_webpackManifest.Value.ContainsKey(requirement))
                        {
                            var requirementEntryModel = RequestedEntryModel.BuildWebpackEntry(requirement);
                            requirementEntryModel.Sink = sinkName;
                            requirementEntry           = requirementEntryModel;
                        }
                    }

                    if (requirementEntry != null)
                    {
                        finalOutput += GetEntryOutput(requirementEntry, sinkName, requestedEntry.Name);
                    }
                    else
                    {
                        if (_settings.PrintComments)
                        {
                            finalOutput += $"<!-- Cannot resolve \"{requirement}\" requested by \"{requestedEntry.Name}\". -->\n";
                        }
                    }
                }

                finalOutput += _serializer.SerializeEntry(requestedEntry, dependencyOf);
            }


            return(finalOutput);
        }
Example #2
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = null;
            if (context.AllAttributes.TryGetAttribute("name", out var name))
            {
                var entryName = name.Value.ToString();
                if (_entriesManifest.Value.ContainsKey(entryName))
                {
                    var entry = RequestedEntryModel.BuildWebpackEntry(entryName);

                    if (context.AllAttributes.TryGetAttribute("key", out var key))
                    {
                        entry.Key = key.Value.ToString();
                    }

                    if (context.AllAttributes.TryGetAttribute("sink", out var sink))
                    {
                        entry.Sink = sink.Value.ToString();
                    }

                    if (context.AllAttributes.TryGetAttribute("async", out var async))
                    {
                        entry.Async = true;
                    }

                    if (context.AllAttributes.TryGetAttribute("defer", out var defer))
                    {
                        entry.Defer = true;
                    }

                    if (context.AllAttributes.TryGetAttribute("css-only", out var cssOnly))
                    {
                        entry.CSSOnly = true;
                    }

                    if (context.AllAttributes.TryGetAttribute("js-only", out var jsOnly))
                    {
                        entry.JSOnly = true;
                    }

                    if (context.AllAttributes.TryGetAttribute("requires", out var requires))
                    {
                        entry.Requires = requires.Value.ToString().Split(',');
                    }

                    if (context.AllAttributes.TryGetAttribute("required-by", out var requiredBy))
                    {
                        entry.RequiredBy = requiredBy.Value.ToString().Split(',');
                    }

                    _viewData.AddEntry(entry);
                }
            }
        }