Exemplo n.º 1
0
        static void AddBundle(BundleCollection bundles, string bundlePath, string scriptPath)
        {
            bool isJavascript = scriptPath.EndsWith(".js");
            var resolver = new BundleResolver(bundles);

            if (bundles.Any(b => b.Path == bundlePath))
            {
                var bundle = bundles.First(b => b.Path == bundlePath);
                var contents = resolver.GetBundleContents(bundle.Path);

                if (!contents.Any(s => s == scriptPath))
                {
                    //if bundle already exists, include the script path
                    bundle.Include(scriptPath);
                }
            }
            else
            {
                //if bundle not exists, create the bundle and include the script path
                if (isJavascript)
                {
                    bundles.Add(new ScriptBundle(bundlePath).Include(scriptPath));
                }
                else
                {
                    bundles.Add(new StyleBundle(bundlePath).Include(scriptPath, new CssRewriteUrlTransform()));
                }
            }
        }
        public void EnsureEmptyVirtualPathsDoNotThrowTest()
        {
            BundleCollection col      = new BundleCollection();
            BundleResolver   resolver = new BundleResolver(col);

            Assert.IsFalse(resolver.IsBundleVirtualPath(String.Empty));
            Assert.IsNull(resolver.GetBundleContents(String.Empty));
            Assert.IsNull(resolver.GetBundleUrl(String.Empty));
        }
        public void EnsureNonVirtualPathsDoNotThrowTest()
        {
            BundleCollection col      = new BundleCollection();
            BundleResolver   resolver = new BundleResolver(col);

            Assert.IsFalse(resolver.IsBundleVirtualPath("missingTilde"));
            Assert.IsNull(resolver.GetBundleContents("missingTilde"));
            Assert.IsNull(resolver.GetBundleUrl("missingTilde"));
        }
        public void NonBundleValidUrlTest()
        {
            BundleCollection col = new BundleCollection();

            col.Add(new Bundle("~/js"));
            BundleResolver resolver = new BundleResolver(col);

            Assert.IsFalse(resolver.IsBundleVirtualPath("~/nope"));
            Assert.IsNull(resolver.GetBundleContents("~/nope"));
            Assert.IsNull(resolver.GetBundleUrl("~/nope"));
        }
Exemplo n.º 5
0
        private static IHtmlString GenerateVersion(string html, string[] paths)
        {
            BundleResolver bundle = new BundleResolver(BundleTable.Bundles);

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            var context = new HttpContextWrapper(HttpContext.Current);

            foreach (var p in paths)
            {
                var    list = new List <string>();
                string parm = "", path = p;
                if (p.Contains("?"))
                {
                    parm = p.Substring(p.LastIndexOf("?"));
                    path = p.Replace(parm, "");
                }
                if (BundleTable.EnableOptimizations)
                {
                    var b = bundle.GetBundleUrl(path);//取压缩
                    if (b == null)
                    {
                        b = path;
                    }
                    else if (!string.IsNullOrWhiteSpace(parm))
                    {
                        parm = parm.Replace("?", "&");
                    }

                    list.Add(b);
                }
                else
                {
                    var bs = bundle.GetBundleContents(path);//取原路径
                    if (bs == null)
                    {
                        list.Add(path);
                    }
                    else
                    {
                        list.AddRange(bs);
                    }
                }
                foreach (var bc in list)
                {
                    var u = UrlHelper.GenerateContentUrl(bc, context);
                    sb.AppendFormat(html, u + parm);
                    sb.Append(Environment.NewLine);
                }
            }
            return(new HtmlString(sb.ToString()));
        }
    public static BundleCollection JoinScriptBundle(this BundleCollection bundleCollection, string virtualPath, params string[] bundlesToJoin)
    {
        var    bundleResolver  = new BundleResolver(bundleCollection);
        Bundle allScriptBundle = new ScriptBundle(virtualPath);

        foreach (var bundle in bundlesToJoin)
        {
            foreach (var scriptPath in bundleResolver.GetBundleContents(bundle))
            {
                allScriptBundle = allScriptBundle.Include(scriptPath);
            }
        }
        bundleCollection.Add(allScriptBundle);
        return(bundleCollection);
    }
Exemplo n.º 7
0
        public void DynamicBundleGetBundleContentsTest()
        {
            BundleCollection bundles = new BundleCollection();

            bundles.Add(new DynamicFolderBundle("js", "*.js", new JsMinify()));
            BundleTable.VirtualPathProvider = new FileVirtualPathProvider(TestContext.DeploymentDirectory);
            BundleResolver resolver = new BundleResolver(bundles, new Moq.Mock <HttpContextBase>().Object);
            string         output   = "";

            foreach (var s in resolver.GetBundleContents("~/scripts/js"))
            {
                output += s + "|";
            }
            Assert.AreEqual("~/scripts/first.js|~/scripts/second.js|", output);
        }
Exemplo n.º 8
0
 public void ScriptBundleGetBundleContextTest()
 {
     try {
         BundleCollection bundles = new BundleCollection();
         bundles.Add(new ScriptBundle("~/js").Include("~/scripts/first.js", "~/scripts/second.js"));
         BundleTable.VirtualPathProvider = new FileVirtualPathProvider(TestContext.DeploymentDirectory);
         BundleResolver resolver = new BundleResolver(bundles, new Moq.Mock <HttpContextBase>().Object);
         string         output   = "";
         foreach (var s in resolver.GetBundleContents("~/js"))
         {
             output += s + "|";
         }
         Assert.AreEqual("~/scripts/first.js|~/scripts/second.js|", output);
     }
     finally {
         BundleTable.VirtualPathProvider = null;
     }
 }
Exemplo n.º 9
0
        public void UseBundle(ComposableBundle <T> bundle)
        {
            // ReSharper disable once UseObjectOrCollectionInitializer
            var collection = new BundleCollection();

            collection.Add(_bundle);

            var resolver = new BundleResolver(collection);
            var content  = resolver.GetBundleContents(_bundle.Path)?.ToList();

            RemoveScripts(content);

            foreach (var virtualPath in bundle.VirtualPaths)
            {
                // Stop the script from being added twice
                if (content != null && content.Contains(virtualPath))
                {
                    continue;
                }
                _bundle.Include(virtualPath);
            }
        }
Exemplo n.º 10
0
 private static bool IsFilePathExist(Bundle bundle, string virtualFilePath)
 {
     var resolver = new BundleResolver(BUNDLE_COLLECTION);
     var bundleFilesPath = (List<string>)resolver.GetBundleContents(bundle.Path);
     return bundleFilesPath.Contains(virtualFilePath);
 }