示例#1
0
        /// <summary>
        /// Resolve references and build all scripts in memory
        /// </summary>
        /// <param name="path">Path to the file to be read</param>
        public static void Bundle(string path)
        {
            string line;
            var    dirInfo = new FileInfo(path).Directory;

            CompiledFiles.Add(FileHash.GetMd5(path));

            using (var sr = new StreamReader(path))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    var reference = GetReference(line);

                    if (!String.IsNullOrEmpty(reference) && !Path.IsPathRooted(reference))
                    {
                        reference = Path.Combine(dirInfo.FullName, reference);
                    }

                    if (!String.IsNullOrEmpty(reference) && !reference.EndsWith(".js", StringComparison.InvariantCultureIgnoreCase))
                    {
                        reference += ".js";
                    }

                    var referenceHash = String.IsNullOrEmpty(reference) ? null : FileHash.GetMd5(reference);

                    if (!String.IsNullOrWhiteSpace(reference) && !CompiledFiles.Contains(referenceHash))
                    {
                        CompiledFiles.Add(referenceHash);

                        Bundle(reference);
                    }

                    if (String.IsNullOrEmpty(reference))
                    {
                        Compiled.AppendLine(line);
                    }
                }
                Compiled.AppendLine();
            }
        }
示例#2
0
        /// <summary>
        /// Combine all JS files ordering by references, bundle and minify
        /// </summary>
        /// <param name="sourcePath">Path of the root folder to search for JS files</param>
        /// <param name="configType">Determines what modifications will be made on the compilation</param>
        /// <returns></returns>
        public static string CompileAll(string sourcePath, Program.ConfigurationType configType)
        {
            foreach (var f in ImportedFiles)
            {
                CompiledFiles.Add(FileHash.GetMd5(f));
            }

            var files = Directory.GetFiles(sourcePath, "*.js", SearchOption.AllDirectories).ToList();

            foreach (var f in files)
            {
                if (!CompiledFiles.Contains(FileHash.GetMd5(f)))
                {
                    Bundle(f);
                }
            }

            if (configType == Program.ConfigurationType.Release)
            {
                return(Min.MinifyJavaScript(Compiled.ToString()));
            }

            return(Compiled.ToString());
        }