Exemplo n.º 1
0
 public void SpriterInitialization(BuildResult buildResult)
 {
     if (SpriteGeneration && SpriteGenerator == null)
     {
         SpriteGenerator = new SpriteHolder(Owner.DiskCache, Owner.Logger);
         BundlePngUrl    = buildResult.AllocateName("bundle.png");
     }
 }
Exemplo n.º 2
0
        public void Build(bool compress, bool mangle, bool beautify)
        {
            var diskCache   = Project.Owner.DiskCache;
            var cssLink     = "";
            var cssToBundle = new List <SourceFromPair>();

            foreach (var source in BuildResult.Path2FileInfo.Values.OrderBy(f => f.Owner.FullPath).ToArray())
            {
                if (source.Type == FileCompilationType.Css || source.Type == FileCompilationType.ImportedCss)
                {
                    cssToBundle.Add(new SourceFromPair(source.Owner.Utf8Content, source.Owner.FullPath));
                }
                else if (source.Type == FileCompilationType.Resource)
                {
                    FilesContent.GetOrAddValueRef(BuildResult.ToOutputUrl(source)) = source.Owner.ByteContent;
                }
            }

            if (cssToBundle.Count > 0)
            {
                string cssPath      = BuildResult.AllocateName("bundle.css");
                var    cssProcessor = new CssProcessor(Project.Tools);
                var    cssContent   = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) =>
                {
                    var full         = PathUtils.Join(from, url);
                    var fullJustName = full.Split('?', '#')[0];
                    BuildResult.Path2FileInfo.TryGetValue(fullJustName, out var fileAdditionalInfo);
                    FilesContent.GetOrAddValueRef(BuildResult.ToOutputUrl(fileAdditionalInfo)) = fileAdditionalInfo.Owner.ByteContent;
                    return(PathUtils.GetFile(fileAdditionalInfo.OutputUrl) +
                           full.Substring(fullJustName.Length));
                }).Result;
                var cssImports = "";
                foreach (var match in Regex.Matches(cssContent, "@import .*;"))
                {
                    cssImports += match.ToString();
                    cssContent  = cssContent.Replace(match.ToString(), "");
                }
                FilesContent.GetOrAddValueRef(cssPath) = cssImports + cssContent;
                cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">";
            }

            if (Project.SpriteGeneration)
            {
                _bundlePng = Project.BundlePngUrl;
                var bundlePngContent = Project.SpriteGenerator.BuildImage(true);
                if (bundlePngContent != null)
                {
                    _bundlePngInfo = new List <float>();
                    foreach (var slice in bundlePngContent)
                    {
                        FilesContent.GetOrAddValueRef(PathUtils.InjectQuality(_bundlePng, slice.Quality)) = slice.Content;
                        _bundlePngInfo.Add(slice.Quality);
                    }
                }
                else
                {
                    _bundlePng = null;
                }
            }

            var bundler = new BundlerImpl(_tools);

            bundler.Callbacks = this;
            if (Project.ExampleSources.Count > 0)
            {
                bundler.MainFiles = new[] { Project.ExampleSources[0] };
            }
            else
            {
                bundler.MainFiles = new[] { Project.MainFile };
            }

            _mainJsBundleUrl = BuildResult.BundleJsUrl;
            bundler.Compress = compress;
            bundler.Mangle   = mangle;
            bundler.Beautify = beautify;
            var defines = new Dictionary <string, object>();

            foreach (var p in Project.Defines)
            {
                defines.Add(p.Key, p.Value);
            }

            bundler.Defines = defines;
            bundler.Bundle();
            if (!Project.NoHtml)
            {
                BuildFastBundlerIndexHtml(cssLink);
                FilesContent.GetOrAddValueRef("index.html") = _indexHtml;
            }
        }