Beispiel #1
0
 /// <summary>
 /// Resolve the actual BundleFile objects that should be part of the bundle.
 /// Virtual so directory items can do the correct thing
 /// </summary>
 /// <param name="files"></param>
 /// <param name="context"></param>
 public virtual void AddFiles(List <BundleFile> files, BundleContext context)
 {
     files.Add(new BundleFile(VirtualPath, context.VirtualPathProvider.GetFile(VirtualPath), Transforms));
 }
 public IEnumerable <BundleFile> OrderFiles(BundleContext context, IEnumerable <BundleFile> files)
 {
     return(files.OrderBy(f => f.VirtualFile.Name.Length));
 }
        /// <summary>
        /// Concatenates the contents of bundle files(after applying any item transforms) to produce the bundle content.
        /// </summary>
        /// <param name="bundle">The <see cref="Bundle"/> object from which to build the combined content.</param>
        /// <param name="context">The <see cref="BundleContext"/> object that contains state for both the framework configuration and the HTTP request.</param>
        /// <param name="files">The files contained in the bundle.</param>
        /// <returns>The combined content of all files in the bundle.</returns>
        /// <remarks>
        /// Instrumentation mode(for tooling) adds the following to the bundle content
        ///
        /// 1. A bundle preamble at the start that consists of name/value pairs with a required Boundary token:
        /// /* Bundle=System.Web.Optimization.Bundle;Boundary=MQA2ADkAMgA2ADIANgAwADYANwA=; */
        ///
        /// 2. A file header that contains the boundary value specified in the preamble, and the virtual file path
        /// /* MQA2ADkAMgA2ADIANgAwADYANwA= "~/mod/modernizr-1.0.js" */
        ///
        /// 3. Followed by the actual contents of the file
        /// </remarks>
        public string BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable <BundleFile> files)
        {
            if (files == null)
            {
                return(String.Empty);
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (bundle == null)
            {
                throw new ArgumentNullException("bundle");
            }

            StringBuilder bundleBlob = new StringBuilder();
            string        bundleHash = "";

            if (context.EnableInstrumentation)
            {
                bundleHash = GetBoundaryIdentifier(bundle);
                bundleBlob.AppendLine(GenerateBundlePreamble(bundleHash));
            }

            string concatToken = null;

            if (!String.IsNullOrEmpty(bundle.ConcatenationToken))
            {
                concatToken = bundle.ConcatenationToken;
            }
            else
            {
                // If JsMinify is used, and no ConcatenationToken is specified, use ';'
                foreach (IBundleTransform transform in bundle.Transforms)
                {
                    if (typeof(JsMinify).IsAssignableFrom(transform.GetType()))
                    {
                        concatToken = ";" + Environment.NewLine;
                        break;
                    }
                }
            }
            if (concatToken == null || context.EnableInstrumentation)
            {
                // If no token specified or we are in instrumentation mode separate using new lines
                concatToken = Environment.NewLine;
            }

            foreach (BundleFile file in files)
            {
                if (context.EnableInstrumentation)
                {
                    bundleBlob.Append(GetFileHeader(context, file.VirtualFile, GetInstrumentedFileHeaderFormat(bundleHash)));
                }
                // Apply per file transforms before concatinating
                bundleBlob.Append(file.ApplyTransforms());
                bundleBlob.Append(concatToken);
            }

            return(bundleBlob.ToString());
        }
Beispiel #4
0
 /// <summary>
 /// Filters a set of files and returns a new set that excludes ignored files.
 /// </summary>
 /// <param name="context">The <see cref="BundleContext"/> object that contains state for both the framework configuration and the HTTP request.</param>
 /// <param name="files">Set of input files to compare with the ignore list.</param>
 /// <returns>Set of files with ignored files excluded.</returns>
 public IEnumerable <BundleFile> FilterIgnoredFiles(BundleContext context, IEnumerable <BundleFile> files)
 {
     return(files.Where(f => !ShouldIgnore(context, f.VirtualFile.Name)));
 }