Beispiel #1
0
        public void Build(BuildCtx buildCtx)
        {
            var buildModuleCtx = new BuildModuleCtx()
            {
                _buildCtx    = buildCtx,
                _owner       = this,
                _result      = new BuildResult(),
                ToCheck      = new OrderedHashSet <string>(),
                ToCompile    = new OrderedHashSet <string>(),
                ToCompileDts = new OrderedHashSet <string>()
            };
            ITSCompiler compiler = null;

            try
            {
                ProjectOptions.BuildCache.StartTransaction();
                compiler           = buildCtx.CompilerPool.GetTs();
                compiler.DiskCache = DiskCache;
                compiler.Ctx       = buildModuleCtx;
                var compOpt = buildCtx.TSCompilerOptions.Clone();
                compOpt.rootDir     = Owner.FullPath;
                compOpt.outDir      = "_virtual";
                compOpt.module      = ModuleKind.Commonjs;
                compOpt.declaration = true;
                if (!ProjectOptions.TypeScriptVersionOverride && DevDependencies != null &&
                    DevDependencies.Contains("typescript"))
                {
                    ProjectOptions.Tools.SetTypeScriptPath(Owner.FullPath);
                }
                else
                {
                    ProjectOptions.Tools.SetTypeScriptVersion(ProjectOptions.TypeScriptVersion);
                }
                compiler.MergeCompilerOptions(compOpt);
                compiler.MergeCompilerOptions(ProjectOptions.CompilerOptions);
                var positionIndependentOptions = compiler.CompilerOptions.Clone();
                positionIndependentOptions.rootDir = null;
                var trueTSVersion = compiler.GetTSVersion();
                buildCtx.ShowTsVersion(trueTSVersion);
                ProjectOptions.ConfigurationBuildCacheId = ProjectOptions.BuildCache.MapConfiguration(trueTSVersion,
                                                                                                      JsonConvert.SerializeObject(positionIndependentOptions, Formatting.None,
                                                                                                                                  new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));
                var wasSomeError = false;
                do
                {
                    buildModuleCtx.ChangedDts   = false;
                    buildModuleCtx.CrawledCount = 0;
                    buildModuleCtx.ToCheck.Clear();
                    buildModuleCtx.ToCompile.Clear();
                    buildModuleCtx.ToCompileDts.Clear();
                    buildModuleCtx.LocalResolveCache.Clear();
                    ProjectOptions.HtmlHeadExpanded = buildModuleCtx.ExpandHtmlHead(ProjectOptions.HtmlHead);
                    foreach (var src in buildCtx.Sources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(compOpt.rootDir, src));
                    }

                    if (ProjectOptions.IncludeSources != null)
                    {
                        foreach (var src in ProjectOptions.IncludeSources)
                        {
                            buildModuleCtx.CheckAdd(PathUtils.Join(compOpt.rootDir, src));
                        }
                    }

                    buildModuleCtx.Crawl();
                    if (buildModuleCtx.ToCompile.Count != 0)
                    {
                        if (buildCtx.Verbose)
                        {
                            compiler.MeasurePerformance = true;
                        }
                        var start = DateTime.UtcNow;
                        buildModuleCtx.OutputedJsFiles  = 0;
                        buildModuleCtx.OutputedDtsFiles = 0;
                        compiler.CreateProgram(Owner.FullPath,
                                               buildModuleCtx.ToCompile.Concat(buildModuleCtx.ToCompileDts).ToArray());
                        if (!compiler.CompileProgram())
                        {
                            wasSomeError = true;
                            break;
                        }

                        ProjectOptions.CurrentBuildCommonSourceDirectory = compiler.CommonSourceDirectory;
                        ProjectOptions.CommonSourceDirectory             = ProjectOptions.CommonSourceDirectory == null
                            ? compiler.CommonSourceDirectory
                            : PathUtils.CommonDir(ProjectOptions.CommonSourceDirectory, compiler.CommonSourceDirectory);
                        compiler.GatherSourceInfo();
                        if (ProjectOptions.SpriteGeneration)
                        {
                            ProjectOptions.SpriteGenerator.ProcessNew();
                        }
                        if (!compiler.EmitProgram())
                        {
                            wasSomeError = true;
                            break;
                        }

                        buildModuleCtx.UpdateCacheIds();
                        Logger.Info(
                            $"Compiled Src: {buildModuleCtx.ToCompile.Count} Dts: {buildModuleCtx.ToCompileDts.Count} => Js: {buildModuleCtx.OutputedJsFiles} Dts: {buildModuleCtx.OutputedDtsFiles} in {(DateTime.UtcNow - start).TotalSeconds:F1}s");
                        buildModuleCtx.ToCompile.Clear();
                        buildModuleCtx.Crawl();
                    }
                } while (buildModuleCtx.ChangedDts || 0 < buildModuleCtx.ToCompile.Count);

                if (ProjectOptions.BuildCache.IsEnabled && !wasSomeError)
                {
                    ProjectOptions.StoreResultToBuildCache(buildModuleCtx._result);
                }
                buildCtx.BuildResult = buildModuleCtx._result;
            }
            finally
            {
                if (compiler != null)
                {
                    buildCtx.CompilerPool.ReleaseTs(compiler);
                }
                ProjectOptions.BuildCache.EndTransaction();
            }
        }
Beispiel #2
0
        public void Build(BuildCtx buildCtx)
        {
            var buildModuleCtx = new BuildModuleCtx()
            {
                _buildCtx    = buildCtx,
                _owner       = this,
                _result      = new BuildResult(),
                ToCheck      = new OrderedHashSet <string>(),
                ToCompile    = new OrderedHashSet <string>(),
                ToCompileDts = new OrderedHashSet <string>()
            };
            ITSCompiler compiler = null;

            try
            {
                ProjectOptions.BuildCache.StartTransaction();
                compiler           = buildCtx.CompilerPool.GetTs();
                compiler.DiskCache = DiskCache;
                compiler.Ctx       = buildModuleCtx;
                var compOpt = buildCtx.TSCompilerOptions.Clone();
                compOpt.rootDir     = Owner.FullPath;
                compOpt.outDir      = "_virtual";
                compOpt.module      = ModuleKind.CommonJS;
                compOpt.declaration = true;
                ProjectOptions.Tools.SetTypeScriptVersion(ProjectOptions.TypeScriptVersion);
                compiler.MergeCompilerOptions(compOpt);
                compiler.MergeCompilerOptions(ProjectOptions.CompilerOptions);
                ProjectOptions.ConfigurationBuildCacheId = ProjectOptions.BuildCache.MapConfiguration(ProjectOptions.TypeScriptVersion, JsonConvert.SerializeObject(compiler.CompilerOptions, Formatting.None, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));
                do
                {
                    buildModuleCtx.ChangedDts          = false;
                    buildModuleCtx.CrawledCount        = 0;
                    buildModuleCtx.TrullyCompiledCount = 0;
                    buildModuleCtx.ToCheck.Clear();
                    buildModuleCtx.ToCompile.Clear();
                    buildModuleCtx.ToCompileDts.Clear();
                    ProjectOptions.HtmlHeadExpanded = buildModuleCtx.ExpandHtmlHead(ProjectOptions.HtmlHead);
                    foreach (var src in buildCtx.Sources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(compOpt.rootDir, src));
                    }
                    buildModuleCtx.Crawl();
                    if (buildModuleCtx.ToCompile.Count != 0)
                    {
                        if (buildCtx.Verbose)
                        {
                            compiler.MeasurePerformance = true;
                        }
                        compiler.CreateProgram(Owner.FullPath, buildModuleCtx.ToCompile.Concat(buildModuleCtx.ToCompileDts).ToArray());
                        if (!compiler.CompileProgram())
                        {
                            break;
                        }
                        ProjectOptions.CommonSourceDirectory = compiler.CommonSourceDirectory;
                        compiler.GatherSourceInfo();
                        if (ProjectOptions.SpriteGeneration)
                        {
                            ProjectOptions.SpriteGenerator.ProcessNew();
                        }
                        if (!compiler.EmitProgram())
                        {
                            break;
                        }
                        buildModuleCtx.UpdateCacheIds();
                        buildModuleCtx.Crawl();
                    }
                } while (buildModuleCtx.ChangedDts || buildModuleCtx.TrullyCompiledCount < buildModuleCtx.ToCompile.Count);
                if (ProjectOptions.BuildCache.IsEnabled)
                {
                    StoreResultToBuildCache(buildModuleCtx._result);
                }
                buildCtx.BuildResult = buildModuleCtx._result;
            }
            finally
            {
                if (compiler != null)
                {
                    buildCtx.CompilerPool.ReleaseTs(compiler);
                }
                ProjectOptions.BuildCache.EndTransaction();
            }
        }
Beispiel #3
0
        public void Build(bool compress, bool mangle, bool beautify)
        {
            var diskCache = Project.Owner.DiskCache;
            var root      = Project.Owner.Owner.FullPath;

            _jsFilesContent = new Dictionary <string, string>();
            var cssLink     = "";
            var cssToBundle = new List <SourceFromPair>();

            foreach (var source in BuildResult.Path2FileInfo)
            {
                if (source.Value.Type == FileCompilationType.TypeScript || source.Value.Type == FileCompilationType.JavaScript || source.Value.Type == FileCompilationType.JavaScriptAsset)
                {
                    if (source.Value.Output == null)
                    {
                        continue; // Skip d.ts
                    }
                    _jsFilesContent[PathUtils.ChangeExtension(source.Key, "js").ToLowerInvariant()] = source.Value.Output;
                }
                else if (source.Value.Type == FileCompilationType.Css)
                {
                    cssToBundle.Add(new SourceFromPair(source.Value.Owner.Utf8Content, source.Value.Owner.FullPath));
                }
                else if (source.Value.Type == FileCompilationType.Resource)
                {
                    FilesContent[source.Value.OutputUrl] = source.Value.Owner.ByteContent;
                }
            }
            if (cssToBundle.Count > 0)
            {
                string cssPath      = Project.AllocateName("bundle.css");
                var    cssProcessor = new CssProcessor(Project.Tools);
                FilesContent[cssPath] = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) =>
                {
                    var full               = PathUtils.Join(from, url);
                    var fullJustName       = full.Split('?', '#')[0];
                    var fileAdditionalInfo = BuildModuleCtx.AutodetectAndAddDependencyCore(Project, fullJustName, diskCache.TryGetItem(from) as IFileCache);
                    FilesContent[fileAdditionalInfo.OutputUrl] = fileAdditionalInfo.Owner.ByteContent;
                    return(PathUtils.SplitDirAndFile(fileAdditionalInfo.OutputUrl).Item2 + full.Substring(fullJustName.Length));
                }).Result;
                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[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[] { PathUtils.ChangeExtension(Project.ExampleSources[0], "js") };
            }
            else
            {
                bundler.MainFiles = new[] { PathUtils.ChangeExtension(Project.MainFile, "js") };
            }
            _mainJsBundleUrl = Project.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["index.html"] = _indexHtml;
            }
        }
Beispiel #4
0
        public void Build(BuildCtx buildCtx, BuildResult buildResult, int iterationId)
        {
            var tryDetectChanges = !buildCtx.ProjectStructureChanged;

            buildResult.HasError = false;
            if (!buildResult.Incremental || !tryDetectChanges)
            {
                buildResult.RecompiledIncrementaly.Clear();
            }
            var buildModuleCtx = new BuildModuleCtx()
            {
                BuildCtx    = buildCtx,
                Owner       = this,
                Result      = buildResult,
                ToCheck     = new OrderedHashSet <string>(),
                IterationId = iterationId
            };

            try
            {
                ProjectOptions.BuildCache.StartTransaction();
                ITSCompiler compiler = null;
                try
                {
                    if (!tryDetectChanges)
                    {
                        if (!ProjectOptions.TypeScriptVersionOverride && DevDependencies != null &&
                            DevDependencies.Contains("typescript"))
                        {
                            ProjectOptions.Tools.SetTypeScriptPath(Owner.FullPath);
                        }
                        else
                        {
                            ProjectOptions.Tools.SetTypeScriptVersion(ProjectOptions.TypeScriptVersion);
                        }
                    }
                    compiler = buildCtx.CompilerPool.GetTs(DiskCache, buildCtx.CompilerOptions);
                    var trueTSVersion = compiler.GetTSVersion();
                    buildCtx.ShowTsVersion(trueTSVersion);
                    ProjectOptions.ConfigurationBuildCacheId = ProjectOptions.BuildCache.MapConfiguration(trueTSVersion,
                                                                                                          JsonConvert.SerializeObject(buildCtx.CompilerOptions, Formatting.None, TSCompilerOptions.GetSerializerSettings()));
                }
                finally
                {
                    if (compiler != null)
                    {
                        buildCtx.CompilerPool.ReleaseTs(compiler);
                    }
                }
                if (buildModuleCtx.Result.CommonSourceDirectory == null)
                {
                    buildModuleCtx.Result.CommonSourceDirectory = Owner.FullPath;
                }
                buildResult.TaskForSemanticCheck = buildCtx.StartTypeCheck().ContinueWith((task) =>
                {
                    if (task.IsCompletedSuccessfully)
                    {
                        buildResult.SemanticResult = task.Result;
                        buildResult.NotifySemanticResult(task.Result);
                    }
                });
                if (tryDetectChanges)
                {
                    if (!buildModuleCtx.CrawlChanges())
                    {
                        buildResult.Incremental = true;
                        goto noDependencyChangeDetected;
                    }
                    buildCtx.ProjectStructureChanged = true;
                    buildResult.Incremental          = false;
                    buildModuleCtx.Result.JavaScriptAssets.Clear();
                    foreach (var info in buildModuleCtx.Result.Path2FileInfo)
                    {
                        info.Value.IterationId = iterationId - 1;
                    }
                }
                buildModuleCtx.CrawledCount = 0;
                buildModuleCtx.ToCheck.Clear();
                ProjectOptions.HtmlHeadExpanded = buildModuleCtx.ExpandHtmlHead(ProjectOptions.HtmlHead);
                if (buildCtx.MainFile != null)
                {
                    buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, buildCtx.MainFile), FileCompilationType.Unknown);
                }
                if (buildCtx.ExampleSources != null)
                {
                    foreach (var src in buildCtx.ExampleSources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, src), FileCompilationType.Unknown);
                    }
                }
                if (buildCtx.TestSources != null)
                {
                    foreach (var src in buildCtx.TestSources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, src), FileCompilationType.Unknown);
                    }
                }

                if (ProjectOptions.IncludeSources != null)
                {
                    foreach (var src in ProjectOptions.IncludeSources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, src), FileCompilationType.Unknown);
                    }
                }

                buildModuleCtx.Crawl();
                noDependencyChangeDetected :;
                ProjectOptions.CommonSourceDirectory = buildModuleCtx.Result.CommonSourceDirectory;
                if (ProjectOptions.SpriteGeneration)
                {
                    ProjectOptions.SpriteGenerator.ProcessNew();
                }
                var hasError = false;
                foreach (var item in buildModuleCtx.Result.Path2FileInfo)
                {
                    if (item.Value.HasError)
                    {
                        hasError = true;
                        break;
                    }
                }
                buildModuleCtx.Result.HasError = hasError;
                if (ProjectOptions.BuildCache.IsEnabled)
                {
                    buildModuleCtx.StoreResultToBuildCache(buildModuleCtx.Result);
                }
            }
            finally
            {
                ProjectOptions.BuildCache.EndTransaction();
            }
        }