예제 #1
0
        string GetGlobalDefines()
        {
            var res = new StringBuilder();

            foreach (var def in _project.BuildDefines(_mainBuildResult))
            {
                var val = TypeConverter.ToAst(def.Value).PrintToString();
                res.Append($"var {def.Key} = {val};");
            }

            return(res.ToString());
        }
예제 #2
0
        public void Build(bool compress, bool mangle, bool beautify, bool _, string?__)
        {
            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)
                {
                    _mainBuildResult.FilesContent.GetOrAddValueRef(_buildResult.ToOutputUrl(source)) = source.Owner.ByteContent;
                }
            }

            if (cssToBundle.Count > 0)
            {
                string cssPath      = _mainBuildResult.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);
                    _mainBuildResult.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(), "");
                }

                _mainBuildResult.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)
                    {
                        _mainBuildResult.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) > 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;
            bundler.Defines  = _project.BuildDefines(_mainBuildResult);
            bundler.Bundle();
            if (!_project.NoHtml)
            {
                BuildFastBundlerIndexHtml(cssLink);
                _mainBuildResult.FilesContent.GetOrAddValueRef("index.html") = _indexHtml;
            }

            if (_project.SubProjects != null)
            {
                var newSubBundlers = new RefDictionary <string, BundleBundler>();
                foreach (var(projPath, subProject) in _project.SubProjects.OrderBy(a => a.Value?.Variant == "serviceworker"))
                {
                    if (subProject == null)
                    {
                        continue;
                    }
                    if (_subBundlers == null || !_subBundlers.TryGetValue(projPath, out var subBundler))
                    {
                        subBundler = new BundleBundler(_tools, _mainBuildResult, subProject, _buildResult.SubBuildResults.GetOrFakeValueRef(projPath));
                    }

                    newSubBundlers.GetOrAddValueRef(projPath) = subBundler;
                    subBundler.Build(compress, mangle, beautify, false, null);
                }

                _subBundlers = newSubBundlers;
            }
            else
            {
                _subBundlers = null;
            }
        }