public void AddDependenciesFromSourceInfo(TSFileAdditionalInfo fileInfo) { var sourceInfo = fileInfo.SourceInfo; if (sourceInfo == null) { return; } sourceInfo.assets.ForEach(a => { if (a.name == null) { return; } var assetName = a.name; AutodetectAndAddDependency(assetName, fileInfo.Owner); }); if (_owner.ProjectOptions.SpriteGeneration) { var spriteHolder = _owner.ProjectOptions.SpriteGenerator; spriteHolder.Process(sourceInfo.sprites); } else { sourceInfo.sprites.ForEach(s => { if (s.name == null) { return; } var assetName = s.name; AutodetectAndAddDependency(assetName, fileInfo.Owner); }); } }
void TryToResolveFromBuildCache(TSFileAdditionalInfo itemInfo) { itemInfo.TakenFromBuildCache = false; var bc = _owner.ProjectOptions.BuildCache; if (bc.IsEnabled) { var fbc = bc.FindTSFileBuildCache(itemInfo.Owner.HashOfContent, _owner.ProjectOptions.ConfigurationBuildCacheId); if (fbc != null) { if ((fbc.LocalImports?.Count ?? 0) == 0 && (fbc.ModuleImports?.Count ?? 0) == 0) { itemInfo.StartCompiling(); itemInfo.Output = fbc.JsOutput; itemInfo.MapLink = fbc.MapLink; var fullPath = PathUtils.ChangeExtension(itemInfo.Owner.FullPath, "d.ts"); var dirPath = PathUtils.Parent(fullPath); var fileOnly = fullPath.Substring(dirPath.Length + 1); var dc = _owner.DiskCache.TryGetItem(dirPath) as IDirectoryCache; var wasChange = dc.WriteVirtualFile(fileOnly, fbc.DtsOutput); var output = dc.TryGetChild(fileOnly) as IFileCache; itemInfo.DtsLink = TSFileAdditionalInfo.Get(output, _owner.DiskCache); if (wasChange) { ChangedDts = true; } itemInfo.RememberLastCompilationCacheIds(); itemInfo.TakenFromBuildCache = true; } } } }
public void RefreshDependenciesFromSourceInfo(TSFileAdditionalInfo fileInfo) { var sourceInfo = fileInfo.SourceInfo; if (sourceInfo == null) { return; } sourceInfo.assets.ForEach(a => { if (a.name == null) { return; } CheckAdd(a.name); }); if (!_owner.ProjectOptions.SpriteGeneration) { sourceInfo.sprites.ForEach(s => { if (s.name == null) { return; } CheckAdd(s.name); }); } }
TSFileAdditionalInfo ReportDependency(TSFileAdditionalInfo owner, TSFileAdditionalInfo dep) { if (dep != null) { owner.ReportDependency(dep.Owner.FullPath); } return(dep); }
public void AddSource(TSFileAdditionalInfo file) { _result.Path2FileInfo[file.Owner.FullPath] = file; if (file.MyProject != null && file.ImportedAsModule != null) { _result.Modules.TryAdd(file.ImportedAsModule, file.MyProject); } }
public static TSFileAdditionalInfo AutodetectAndAddDependencyCore(ProjectOptions projectOptions, string depName, IFileCache usedFrom) { var dc = projectOptions.Owner.DiskCache; var extension = PathUtils.GetExtension(depName); var depFile = dc.TryGetItem(depName) as IFileCache; if (depFile == null) { if (usedFrom != null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("In " + usedFrom.FullPath + " missing dependency " + depName); Console.ForegroundColor = ConsoleColor.Gray; TSFileAdditionalInfo.Get(usedFrom, dc) .ReportDiag(true, -3, "Missing dependency " + depName, 0, 0, 0, 0); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Somethere missing dependency " + depName); Console.ForegroundColor = ConsoleColor.Gray; } return(null); } var assetFileInfo = TSFileAdditionalInfo.Get(depFile, dc); if (projectOptions.BundleCss && extension == "css") { assetFileInfo.Type = FileCompilationType.Css; return(assetFileInfo); } if (assetFileInfo.OutputUrl == null) { assetFileInfo.OutputUrl = projectOptions.AllocateName(PathUtils.Subtract(depFile.FullPath, projectOptions.Owner.Owner.FullPath)); } switch (extension) { case "css": assetFileInfo.Type = FileCompilationType.Css; break; case "js": assetFileInfo.Type = FileCompilationType.JavaScriptAsset; break; default: assetFileInfo.Type = FileCompilationType.Resource; break; } return(assetFileInfo); }
public string resolveModuleMain(string name, TSFileAdditionalInfo parentInfo) { var moduleInfo = TSProject.FindInfoForModule(_owner.Owner, _owner.DiskCache, name, out var diskName); if (moduleInfo == null) { return(null); } if (name != diskName) { parentInfo.ReportDiag(false, 2, "Module import has wrong casing '" + name + "' on disk '" + diskName + "'", 0, 0, 0, 0); } moduleInfo.LoadProjectJson(true); parentInfo.ImportingModule(moduleInfo); var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile); var item = _owner.DiskCache.TryGetItem(mainFile) as IFileCache; if (item == null || item.IsInvalid) { return(null); } var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache); itemInfo.ImportedAsModule = name; AddSource(itemInfo); if (!IsTsOrTsx(mainFile)) { itemInfo.Type = FileCompilationType.JavaScript; CheckAdd(mainFile); if (moduleInfo.TypesMainFile != null) { var dtsPath = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.TypesMainFile); item = _owner.DiskCache.TryGetItem(dtsPath) as IFileCache; itemInfo.DtsLink = TSFileAdditionalInfo.Get(item, _owner.DiskCache); if (item != null && !item.IsInvalid) { return(dtsPath); } } return(null); } itemInfo.Type = FileCompilationType.TypeScript; CheckAdd(item.FullPath); TryToResolveFromBuildCache(itemInfo); Crawl(); if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath)) { return(itemInfo.DtsLink.Owner.FullPath); } return(item.FullPath); }
public string ToOutputUrl(string fileName) { var assetFileInfo = TSFileAdditionalInfo.Get(_owner.DiskCache.TryGetItem(fileName) as IFileCache, _owner.DiskCache); if (_owner.ProjectOptions.BundleCss && assetFileInfo.Type == FileCompilationType.Css) { return(fileName); } if (assetFileInfo.OutputUrl == null) { assetFileInfo.OutputUrl = _owner.ProjectOptions.AllocateName(PathUtils.Subtract(fileName, _owner.Owner.FullPath)); } return(assetFileInfo.OutputUrl); }
public void reportDiag(bool isError, int code, string text, string fileName, int startLine, int startCharacter, int endLine, int endCharacter) { var fc = _owner.DiskCache.TryGetItem(fileName) as IFileCache; if (fc == null) { throw new Exception("Cannot found " + fileName); } var fi = TSFileAdditionalInfo.Get(fc, _owner.DiskCache); fi.ReportDiag(isError, code, text, startLine, startCharacter, endLine, endCharacter); }
public string resolveLocalImport(string name, TSFileAdditionalInfo parentInfo) { var dirPath = PathUtils.Parent(name); var fileOnly = name.Substring(dirPath.Length + 1); var dc = _owner.DiskCache.TryGetItem(dirPath) as IDirectoryCache; if (dc == null || dc.IsInvalid) { return(null); } var item = ExtensionsToImport.Select(ext => dc.TryGetChild(fileOnly + ext) as IFileCache).FirstOrDefault(i => i != null && !i.IsInvalid); if (item == null) { return(null); } if (item.FullPath.Substring(0, name.Length) != name) { parentInfo.ReportDiag(false, 1, "Local import has wrong casing '" + name + "' on disk '" + item.FullPath + "'", 0, 0, 0, 0); } var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache); parentInfo.ImportingLocal(itemInfo); if (IsDts(item.FullPath)) { var jsItem = dc.TryGetChild(fileOnly + ".js") as IFileCache; if (jsItem != null) { var jsItemInfo = TSFileAdditionalInfo.Get(jsItem, _owner.DiskCache); jsItemInfo.Type = FileCompilationType.JavaScript; parentInfo.ImportingLocal(jsItemInfo); CheckAdd(jsItem.FullPath); } // implementation for .d.ts file does not have same name, it needs to be added to build by b.asset("lib.js") and cannot have dependencies } else { itemInfo.Type = FileCompilationType.TypeScript; AddSource(itemInfo); } CheckAdd(item.FullPath); TryToResolveFromBuildCache(itemInfo); Crawl(); if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath)) { return(itemInfo.DtsLink.Owner.FullPath); } return(item.FullPath); }
public void reportDiag(bool isError, int code, string text, string fileName, int startLine, int startCharacter, int endLine, int endCharacter) { var fc = _owner.DiskCache.TryGetItem(fileName) as IFileCache; if (fc == null) { throw new Exception("Cannot found " + fileName); } var fi = TSFileAdditionalInfo.Get(fc, _owner.DiskCache); Console.ForegroundColor = isError ? ConsoleColor.Red : ConsoleColor.Yellow; Console.WriteLine(PathUtils.Subtract(fileName, _owner.Owner.FullPath) + "(" + (startLine + 1) + "," + (startCharacter + 1) + "): " + (isError ? "error" : "warning") + " TS" + code + ": " + text); Console.ForegroundColor = ConsoleColor.Gray; fi.ReportDiag(isError, code, text, startLine, startCharacter, endLine, endCharacter); }
void ReportDependenciesFromCss(TSFileAdditionalInfo info) { if (info.TranspilationDependencies != null) { foreach (var dep in info.TranspilationDependencies) { var fullJustName = PathUtils.Join(info.Owner.Parent.FullPath, dep.Import); var fileAdditionalInfo = AutodetectAndAddDependency(fullJustName); if (fileAdditionalInfo == null) { info.ReportDiag(true, -3, "Missing dependency " + dep.Import, 0, 0, 0, 0); } info.ReportDependency(fullJustName); } } }
bool TryToResolveFromBuildCache(TSFileAdditionalInfo itemInfo) { itemInfo.TakenFromBuildCache = false; var bc = Owner.ProjectOptions.BuildCache; if (bc.IsEnabled) { byte[] hashOfContent; try { hashOfContent = itemInfo.Owner.HashOfContent; } catch { // File was probably renamed or deleted return(false); } var confId = Owner.ProjectOptions.ConfigurationBuildCacheId; var fbc = bc.FindTSFileBuildCache(hashOfContent, confId); if (fbc != null) { if (MatchingTranspilationDendencies(itemInfo.Owner, fbc.TranspilationDependencies)) { if (MakeSourceInfoAbsolute(fbc.SourceInfo, itemInfo.Owner.FullPath)) { itemInfo.Output = fbc.Output; itemInfo.MapLink = fbc.MapLink; if (itemInfo.MapLink?.sources?.Count == 1) { itemInfo.MapLink.sources[0] = itemInfo.Owner.FullPath; } itemInfo.SourceInfo = fbc.SourceInfo; itemInfo.TranspilationDependencies = fbc.TranspilationDependencies; itemInfo.TakenFromBuildCache = true; AddDependenciesFromSourceInfo(itemInfo); //_owner.Logger.Info("Loaded from cache " + itemInfo.Owner.FullPath); return(true); } } } } return(false); }
bool TryToResolveFromBuildCacheCss(TSFileAdditionalInfo itemInfo) { itemInfo.TakenFromBuildCache = false; var bc = Owner.ProjectOptions.BuildCache; if (bc.IsEnabled) { var hashOfContent = itemInfo.Owner.HashOfContent; var fbc = bc.FindTSFileBuildCache(hashOfContent, 0); if (fbc != null) { itemInfo.Output = fbc.Output; itemInfo.TranspilationDependencies = fbc.TranspilationDependencies; itemInfo.TakenFromBuildCache = true; //_owner.Logger.Info("Loaded from cache " + itemInfo.Owner.FullPath); return(true); } } return(false); }
public string readFile(string fullPath) { var file = TryGetFile(fullPath); if (file == null) { return(null); } TSFileAdditionalInfo.Get(file, _owner.DiskCache).StartCompiling(); /* * var testPath = PathUtils.Subtract(fullPath, _buildCtx.TSCompilerOptions.baseUrl); * if (!testPath.StartsWith("../")) * { * testPath = PathUtils.Join("../DUMP_PATH", testPath); * Directory.CreateDirectory(PathUtils.Parent(testPath)); * File.WriteAllText(testPath, file.Utf8Content); * } * //*/ return(file.Utf8Content); }
public TSFileAdditionalInfo CheckAdd(string fullNameWithExtension, FileCompilationType compilationType) { if (!Result.Path2FileInfo.TryGetValue(fullNameWithExtension, out var info)) { var fc = Owner.DiskCache.TryGetItem(fullNameWithExtension) as IFileCache; if (fc == null || fc.IsInvalid) { return(null); } info = TSFileAdditionalInfo.Create(fc, Owner.DiskCache); info.Type = compilationType; Result.CommonSourceDirectory = PathUtils.CommonDir(Result.CommonSourceDirectory, fc.FullPath); Result.Path2FileInfo.Add(fullNameWithExtension, info); } else { if (info.Owner.IsInvalid) { Result.Path2FileInfo.Remove(fullNameWithExtension); return(null); } } if (!ToCheck.Contains(fullNameWithExtension)) { ToCheck.Add(fullNameWithExtension); } if (info.Type == FileCompilationType.Unknown) { info.Type = compilationType; } if (info.Type == FileCompilationType.JavaScriptAsset) { if (Result.JavaScriptAssets.AddUnique(info) && _noDependencyCheck) { _noDependencyCheck = false; } } return(info); }
public IList <string> GetPlainJsDependencies(string name) { var diskCache = Project.Owner.DiskCache; var file = diskCache.TryGetItem(PathUtils.ChangeExtension(name, "ts")) as IFileCache; if (file == null) { file = diskCache.TryGetItem(PathUtils.ChangeExtension(name, "tsx")) as IFileCache; } if (file == null) { return(new List <string>()); } var fileInfo = TSFileAdditionalInfo.Get(file, diskCache); var sourceInfo = fileInfo.SourceInfo; if (sourceInfo == null || sourceInfo.assets == null) { return(new List <string>()); } return(sourceInfo.assets.Select(i => i.name).Where(i => i.EndsWith(".js")).ToList()); }
public TSFileAdditionalInfo CrawlFile(string fileName) { if (!Result.Path2FileInfo.TryGetValue(fileName, out var info)) { if (_noDependencyCheck) { _noDependencyCheck = false; } var fileCache = Owner.DiskCache.TryGetItem(fileName) as IFileCache; if (fileCache == null || fileCache.IsInvalid) { if (BuildCtx.Verbose) { Owner.Logger.Warn("Crawl skipping missing file " + fileName); } return(null); } info = TSFileAdditionalInfo.Create(fileCache, Owner.DiskCache); info.Type = FileCompilationType.Unknown; Result.Path2FileInfo.Add(fileName, info); } else { if (info.Owner.IsInvalid) { if (BuildCtx.Verbose) { Owner.Logger.Warn("Crawl skipping missing file " + fileName); } return(null); } } if (_noDependencyCheck) { if (info.IterationId != IterationId) { info.IterationId = IterationId; CrawlInfo(info); } } else { if (info.Type == FileCompilationType.Unknown) { if (IsDts(fileName)) { info.Type = FileCompilationType.TypeScriptDefinition; } else if (IsTsOrTsx(fileName)) { info.Type = FileCompilationType.TypeScript; } else { var ext = PathUtils.GetExtension(fileName); if (ext.SequenceEqual("css")) { info.Type = FileCompilationType.Css; } else if (ext.SequenceEqual("js") || ext.SequenceEqual("jsx")) { info.Type = FileCompilationType.EsmJavaScript; } } } if (info.IterationId != IterationId) { info.IterationId = IterationId; CrawlInfo(info); } foreach (var dep in info.Dependencies) { CheckAdd(dep, FileCompilationType.Unknown); } } return(info); }
public string ResolveLocalImport(string name, TSFileAdditionalInfo parentInfo, TSProject moduleInfo, string importedAsModule) { var dirPath = PathUtils.Parent(name); var fileOnly = name.Substring(dirPath.Length + 1); var dc = _owner.DiskCache.TryGetItemPreferReal(dirPath) as IDirectoryCache; if (dc == null || dc.IsInvalid) { return(null); } var isJson = false; var isCss = false; IFileCache item = null; if (fileOnly.EndsWith(".json")) { item = dc.TryGetChild(fileOnly, true) as IFileCache; if (item != null) { isJson = true; } } if (fileOnly.EndsWith(".css")) { item = dc.TryGetChild(fileOnly, true) as IFileCache; if (item != null) { isCss = true; } } if (item == null) { item = (parentInfo.Type == FileCompilationType.EsmJavaScript ? ExtensionsToImportFromJs : ExtensionsToImport).Select(ext => dc.TryGetChild(fileOnly + ext, true) as IFileCache) .FirstOrDefault(i => i != null && !i.IsInvalid); } if (item == null) { parentInfo.ReportDiag(false, -15, "Cannot resolve import '" + name + "'", 0, 0, 0, 0); return(null); } if (item.FullPath.Substring(0, name.Length) != name) { parentInfo.ReportDiag(false, -1, "Local import has wrong casing '" + name + "' on disk '" + item.FullPath + "'", 0, 0, 0, 0); } var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache); parentInfo.ImportingLocal(itemInfo); itemInfo.MyProject = moduleInfo ?? parentInfo.MyProject; if (importedAsModule != null) { itemInfo.ImportedAsModule = importedAsModule; } if (isCss) { itemInfo.Type = FileCompilationType.ImportedCss; AddSource(itemInfo); CheckAdd(item.FullPath); var po = itemInfo.MyProject.ProjectOptions; if (!po.BundleCss) { if (itemInfo.OutputUrl == null) { itemInfo.OutputUrl = po.AllocateName(PathUtils.Subtract(item.FullPath, po.Owner.Owner.FullPath)); } } return(null); } if (IsDts(item.Name)) { if (dc.TryGetChild(fileOnly + ".js", true) is IFileCache jsItem) { var jsItemInfo = TSFileAdditionalInfo.Get(jsItem, _owner.DiskCache); jsItemInfo.Type = FileCompilationType.JavaScript; jsItemInfo.MyProject = itemInfo.MyProject; parentInfo.ImportingLocal(jsItemInfo); CheckAdd(jsItem.FullPath); } // implementation for .d.ts file does not have same name, it needs to be added to build by b.asset("lib.js") and cannot have dependencies } else { itemInfo.Type = isJson ? FileCompilationType.Json : parentInfo.Type; AddSource(itemInfo); } if (LocalResolveCache.TryGetValue(name, out var res)) { return(res); } CheckAdd(item.FullPath); TryToResolveFromBuildCache(itemInfo); if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath) && !itemInfo.NeedsCompilation()) { res = itemInfo.DtsLink.Owner.FullPath; } else { res = item.FullPath; } LocalResolveCache.Add(name, res); return(res); }
void Transpile(TSFileAdditionalInfo info) { ITSCompiler compiler = null; try { compiler = BuildCtx.CompilerPool.GetTs(Owner.DiskCache, BuildCtx.CompilerOptions); //_owner.Logger.Info("Transpiling " + info.Owner.FullPath); var result = compiler.Transpile(info.Owner.FullPath, info.Owner.Utf8Content); if (result.Diagnostics != null) { info.ReportDiag(result.Diagnostics); info.HasError = result.Diagnostics.Any(d => d.IsError); } else { info.HasError = false; } if (info.HasError) { info.Output = null; info.MapLink = null; } else { info.Output = SourceMap.RemoveLinkToSourceMap(result.JavaScript); info.MapLink = SourceMap.Parse(result.SourceMap, info.Owner.Parent.FullPath); } } finally { if (compiler != null) { BuildCtx.CompilerPool.ReleaseTs(compiler); } } if (info.HasError) { info.SourceInfo = null; return; } var backupCurrentlyTranspiling = _currentlyTranspiling; try { if (_currentlyTranspiling == null) { _currentlyTranspiling = info; } var parser = new Parser(new Options(), info.Output); var toplevel = parser.Parse(); toplevel.FigureOutScope(); var ctx = new ResolvingConstEvalCtx(info.Owner.FullPath, this); string resolver(IConstEvalCtx myctx, string text) { if (text.StartsWith("resource:", StringComparison.Ordinal)) { return("resource:" + resolver(myctx, text.Substring("resource:".Length))); } if (text.StartsWith("node_modules/", StringComparison.Ordinal)) { return(ResolveImport(info.Owner.FullPath, text.Substring("node_modules/".Length), false, true)); } var res = PathUtils.Join(PathUtils.Parent(myctx.SourceName), text); return(res); } var sourceInfo = GatherBobrilSourceInfo.Gather(toplevel, ctx, resolver); info.SourceInfo = sourceInfo; AddDependenciesFromSourceInfo(info); } finally { _currentlyTranspiling = backupCurrentlyTranspiling; } }
void CrawlInfo(TSFileAdditionalInfo info) { if (info.Owner.ChangeId != info.ChangeId) { info.ChangeId = info.Owner.ChangeId; Result.RecompiledIncrementaly.Add(info); var oldDependencies = new StructList <string>(); if (_noDependencyCheck) { oldDependencies.TransferFrom(ref info.Dependencies); } info.StartCompiling(); switch (info.Type) { case FileCompilationType.Json: info.Output = null; info.MapLink = null; info.SourceInfo = null; break; case FileCompilationType.EsmJavaScript: case FileCompilationType.TypeScript: info.HasError = false; if (!TryToResolveFromBuildCache(info)) { info.Output = null; info.MapLink = null; info.SourceInfo = null; Transpile(info); } break; case FileCompilationType.JavaScriptAsset: case FileCompilationType.JavaScript: info.Output = info.Owner.Utf8Content; info.MapLink = SourceMap.Identity(info.Output, info.Owner.FullPath); break; case FileCompilationType.Resource: break; case FileCompilationType.Css: case FileCompilationType.ImportedCss: if (!TryToResolveFromBuildCacheCss(info)) { var cssProcessor = BuildCtx.CompilerPool.GetCss(); try { info.Output = info.Owner.Utf8Content; cssProcessor.ProcessCss(info.Owner.Utf8Content, ((TSFileAdditionalInfo)info).Owner.FullPath, (string url, string from) => { var urlJustName = url.Split('?', '#')[0]; info.ReportTranspilationDependency(null, urlJustName, null); return(url); }).Wait(); } finally { BuildCtx.CompilerPool.ReleaseCss(cssProcessor); } } ReportDependenciesFromCss(info); break; } if (_noDependencyCheck) { if (!info.Dependencies.SequenceEqual(oldDependencies)) { if (BuildCtx.Verbose) { Owner.Logger.Info("Dependency change detected " + info.Owner.FullPath); } _noDependencyCheck = false; } } } }
public void Crawl() { while (CrawledCount < ToCheck.Count) { var fileName = ToCheck[(int)CrawledCount]; CrawledCount++; var fileCache = _owner.DiskCache.TryGetItem(fileName) as IFileCache; if (fileCache == null || fileCache.IsInvalid) { if (_buildCtx.Verbose) { Console.WriteLine("Crawl skipping missing file " + fileName); } continue; } var fileAdditional = TSFileAdditionalInfo.Get(fileCache, _owner.DiskCache); AddSource(fileAdditional); if (fileAdditional.Type == FileCompilationType.Unknown) { fileAdditional.Type = FileCompilationType.TypeScript; } if (fileAdditional.NeedsCompilation()) { switch (fileAdditional.Type) { case FileCompilationType.Json: if (fileAdditional.MyProject == null) { fileAdditional.MyProject = _owner; } fileAdditional.Output = null; fileAdditional.MapLink = null; break; case FileCompilationType.TypeScript: if (fileAdditional.MyProject == null) { fileAdditional.MyProject = _owner; } fileAdditional.Output = null; fileAdditional.MapLink = null; // d.ts files are compiled always but they don't have any output so needs to be in separate set if (fileName.EndsWith(".d.ts")) { ToCompileDts.Add(fileName); } else { ToCompile.Add(fileName); } break; case FileCompilationType.JavaScript: case FileCompilationType.JavaScriptAsset: fileAdditional.StartCompiling(); fileAdditional.Output = fileAdditional.Owner.Utf8Content; fileAdditional.MapLink = SourceMap.Identity(fileAdditional.Output, fileAdditional.Owner.FullPath); _result.RecompiledLast.Add(fileAdditional); break; case FileCompilationType.Resource: fileAdditional.StartCompiling(); _result.RecompiledLast.Add(fileAdditional); break; case FileCompilationType.Css: fileAdditional.StartCompiling(); if (!_owner.ProjectOptions.BundleCss) { var cssProcessor = _buildCtx.CompilerPool.GetCss(); try { fileAdditional.Output = cssProcessor.ProcessCss(fileAdditional.Owner.Utf8Content, fileAdditional.Owner.FullPath, (string url, string from) => { var full = PathUtils.Join(from, url); var fullJustName = full.Split('?', '#')[0]; var fileAdditionalInfo = AutodetectAndAddDependency(fullJustName, fileAdditional.Owner); fileAdditional.ImportingLocal(fileAdditionalInfo); return(PathUtils.Subtract(fileAdditionalInfo.OutputUrl, PathUtils.Parent(fileAdditional.OutputUrl)) + full.Substring(fullJustName.Length)); }).Result; } finally { _buildCtx.CompilerPool.ReleaseCss(cssProcessor); } _result.RecompiledLast.Add(fileAdditional); } break; } } else { foreach (var localAdditional in fileAdditional.LocalImports) { var localName = localAdditional.Owner.FullPath; if (localName.EndsWith(".d.ts")) { continue; // we cannot handle change in .d.ts without source } CheckAdd(localName); } foreach (var moduleInfo in fileAdditional.ModuleImports) { moduleInfo.LoadProjectJson(true); var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile); if (mainFile.EndsWith(".d.ts")) { continue; // we cannot handle change in .d.ts without source } CheckAdd(mainFile); } RefreshDependenciesFromSourceInfo(fileAdditional); } } }
public IDictionary <long, object[]> getPreEmitTransformations(string fileName) { var fc = _owner.DiskCache.TryGetItem(fileName) as IFileCache; if (fc == null) { return(null); } var fai = TSFileAdditionalInfo.Get(fc, _owner.DiskCache); var sourceInfo = fai.SourceInfo; if (sourceInfo == null) { return(null); } var res = new Dictionary <long, object[]>(); sourceInfo.assets.ForEach(a => { if (a.name == null) { return; } res[a.nodeId] = new object[] { 0, ToOutputUrl(a.name) }; }); if (_owner.ProjectOptions.SpriteGeneration) { var spriteHolder = _owner.ProjectOptions.SpriteGenerator; spriteHolder.Retrieve(sourceInfo.sprites); sourceInfo.sprites.ForEach(s => { if (s.name == null) { return; } if (s.hasColor == true && s.color == null) { res[s.nodeId] = new object[] { 5, 2, 1, s.owidth, 5, 2, 2, s.oheight, 5, 2, 3, s.ox, 5, 2, 4, s.oy, 5, 4, "spritebc" }; } else { res[s.nodeId] = new object[] { 2, 0, s.owidth, 4, 2, 1, s.oheight, 4, 2, 2, s.ox, 4, 2, 3, s.oy, 4, 4, "spriteb" }; } }); } else { sourceInfo.sprites.ForEach(s => { if (s.name == null) { return; } res[s.nodeId] = new object[] { 0, ToOutputUrl(s.name) }; }); } var trdb = _owner.ProjectOptions.TranslationDb; if (trdb != null) { sourceInfo.translations.ForEach(t => { if (t.message == null) { return; } if (t.withParams) { var err = trdb.CheckMessage(t.message, t.knownParams); if (err != null) { fai.ReportDiag(false, -7, "Problem with translation message \"" + t.message + "\" " + err.ToString(), 0, 0, 0, 0); } } if (t.justFormat) { return; } var id = trdb.AddToDB(t.message, t.hint, t.withParams); var finalId = trdb.MapId(id); res[t.nodeId] = new object[] { 2, 0, finalId, 1 + (t.withParams ? 1 : 0) }; }); } var styleDefNaming = _owner.ProjectOptions.StyleDefNaming; var styleDefPrefix = _owner.ProjectOptions.PrefixStyleNames; sourceInfo.styleDefs.ForEach(s => { var skipEx = s.isEx ? 1 : 0; if (s.userNamed) { if (styleDefNaming == StyleDefNamingStyle.AddNames || styleDefNaming == StyleDefNamingStyle.PreserveNames) { if (styleDefPrefix.Length > 0) { if (s.name != null) { res[s.nodeId] = new object[] { 2, 2 + skipEx, styleDefPrefix + s.name, 3 + skipEx }; } else { res[s.nodeId] = new object[] { 3, 2 + skipEx, styleDefPrefix, 3 + skipEx }; } } } else { res[s.nodeId] = new object[] { 1, 2 + skipEx }; } } else { if (styleDefNaming == StyleDefNamingStyle.AddNames && s.name != null) { // TODO: heuristicaly improve s.name by filename res[s.nodeId] = new object[] { 2, 2 + skipEx, styleDefPrefix + s.name, 3 + skipEx }; } } }); if (res.Count == 0) { return(null); } return(res); }
public void writeFile(string fileName, string data) { if (fileName.EndsWith(".js.map")) { var relativeTo = PathUtils.Parent(PathUtils.Join(_owner.Owner.FullPath, fileName)); var sourceMap = SourceMap.Parse(data, relativeTo); var sourceFullPath = sourceMap.sources[0]; var sourceForMap = _result.Path2FileInfo[sourceFullPath]; sourceForMap.MapLink = sourceMap; return; } if (!fileName.StartsWith("_virtual/")) { throw new Exception("writeFile does not start with _virtual"); } var fullPathWithVirtual = PathUtils.Join(_owner.ProjectOptions.CurrentBuildCommonSourceDirectory, fileName); fileName = fileName.Substring(9); var fullPath = PathUtils.Join(_owner.ProjectOptions.CurrentBuildCommonSourceDirectory, fileName); if (fileName.EndsWith(".json")) { _result.Path2FileInfo.TryGetValue(fullPath, out var sourceForJs); _result.RecompiledLast.Add(sourceForJs); return; } if (fullPath.EndsWith(".js")) { OutputedJsFiles++; data = SourceMap.RemoveLinkToSourceMap(data); var sourceName = fullPath.Substring(0, fullPath.Length - ".js".Length) + ".ts"; TSFileAdditionalInfo sourceForJs = null; if (!_result.Path2FileInfo.TryGetValue(sourceName, out sourceForJs)) { _result.Path2FileInfo.TryGetValue(sourceName + "x", out sourceForJs); } if (sourceForJs == null) { if (!_result.Path2FileInfo.TryGetValue(fullPath, out sourceForJs)) { _result.Path2FileInfo.TryGetValue(fullPath + "x", out sourceForJs); } } sourceForJs.Output = data; _result.RecompiledLast.Add(sourceForJs); return; } if (!fullPath.EndsWith(".d.ts")) { throw new Exception("Unknown extension written by TS " + fullPath); } OutputedDtsFiles++; data = new Regex("\\/\\/\\/ *<reference path=\\\"(.+)\\\" *\\/>").Replace(data, (m) => { var origPath = m.Groups[1].Value; var newPath = PathUtils.Subtract(PathUtils.Join(PathUtils.Parent(fullPathWithVirtual), origPath), PathUtils.Parent(fullPath)); return("/// <reference path=\"" + newPath + "\" />"); }); var dirPath = PathUtils.Parent(fullPath); var fileOnly = fullPath.Substring(dirPath.Length + 1); var dc = _owner.DiskCache.TryGetItem(dirPath) as IDirectoryCache; var wasChange = dc.WriteVirtualFile(fileOnly, data); var output = dc.TryGetChild(fileOnly) as IFileCache; var outputInfo = TSFileAdditionalInfo.Get(output, _owner.DiskCache); var sourceName2 = fullPath.Substring(0, fullPath.Length - ".d.ts".Length) + ".ts"; TSFileAdditionalInfo source = null; if (!_result.Path2FileInfo.TryGetValue(sourceName2, out source)) { _result.Path2FileInfo.TryGetValue(sourceName2 + "x", out source); } source.DtsLink = outputInfo; outputInfo.MyProject = source.MyProject; if (wasChange) { ChangedDts = true; } }
public void AddDependenciesFromSourceInfo(TSFileAdditionalInfo fileInfo) { var sourceInfo = fileInfo.SourceInfo; if (sourceInfo == null) { return; } sourceInfo.Imports?.ForEach(i => { var resolved = ResolveImport(fileInfo.Owner.FullPath, i.Name); if (resolved != null && resolved != "?") { fileInfo.ReportDependency(resolved); } else { fileInfo.ReportDiag(true, -3, "Missing import " + i.Name, i.StartLine, i.StartCol, i.EndLine, i.EndCol); } }); sourceInfo.Assets?.ForEach(a => { if (a.Name == null) { return; } var assetName = a.Name; if (assetName.StartsWith("resource:")) { assetName = assetName.Substring(9); if (ReportDependency(fileInfo, AutodetectAndAddDependency(assetName, true)) == null) { fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, a.StartLine, a.StartCol, a.EndLine, a.EndCol); } } else { if (ReportDependency(fileInfo, AutodetectAndAddDependency(assetName)) == null) { fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, a.StartLine, a.StartCol, a.EndLine, a.EndCol); } } }); if (sourceInfo.Sprites != null) { if (Owner.ProjectOptions.SpriteGeneration) { var spriteHolder = Owner.ProjectOptions.SpriteGenerator; spriteHolder.Process(sourceInfo.Sprites); } else { sourceInfo.Sprites.ForEach(s => { if (s.Name == null) { return; } var assetName = s.Name; if (ReportDependency(fileInfo, AutodetectAndAddDependency(assetName)) == null) { fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, s.NameStartLine, s.NameStartCol, s.NameEndLine, s.NameEndCol); } }); } } if (sourceInfo.Translations != null) { var trdb = Owner.ProjectOptions.TranslationDb; if (trdb != null) { sourceInfo.Translations.ForEach(t => { if (t.Message == null) { return; } if (t.WithParams) { var err = trdb.CheckMessage(t.Message, t.KnownParams); if (err != null) { fileInfo.ReportDiag(false, -7, "Problem with translation message \"" + t.Message + "\" " + err.ToString(), t.StartLine, t.StartCol, t.EndLine, t.EndCol); } } }); } } }
public string resolveModuleMain(string name, TSFileAdditionalInfo parentInfo) { var moduleInfo = TSProject.FindInfoForModule(_owner.Owner, _owner.DiskCache, _owner.Logger, name, out var diskName); if (moduleInfo == null) { return(null); } if (name != diskName) { parentInfo.ReportDiag(false, -2, "Module import has wrong casing '" + name + "' on disk '" + diskName + "'", 0, 0, 0, 0); } moduleInfo.LoadProjectJson(true); parentInfo.ImportingModule(moduleInfo); var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile); var item = _owner.DiskCache.TryGetItem(mainFile) as IFileCache; if (item == null || item.IsInvalid) { return(null); } var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache); moduleInfo.MainFileInfo = itemInfo; itemInfo.ImportedAsModule = name; itemInfo.MyProject = moduleInfo; var parentProject = parentInfo.MyProject; if (parentProject.IsRootProject && ((parentProject.Dependencies == null || !parentProject.Dependencies.Contains(name)) && (parentProject.DevDependencies == null || !parentProject.DevDependencies.Contains(name)))) { parentInfo.ReportDiag(false, -12, "Importing module " + name + " without being in package.json as dependency", 0, 0, 0, 0); } if (moduleInfo.ProjectOptions?.ObsoleteMessage != null) { if (!PragmaParser.ParseIgnoreImportingObsolete(parentInfo.Owner.Utf8Content).Contains(name)) { parentInfo.ReportDiag(false, -14, "Importing obsolete module: " + moduleInfo.ProjectOptions?.ObsoleteMessage, 0, 0, 0, 0); } } AddSource(itemInfo); if (!IsTsOrTsx(mainFile)) { itemInfo.Type = FileCompilationType.JavaScript; CheckAdd(mainFile); if (moduleInfo.TypesMainFile != null) { var dtsPath = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.TypesMainFile); item = _owner.DiskCache.TryGetItem(dtsPath) as IFileCache; itemInfo.DtsLink = TSFileAdditionalInfo.Get(item, _owner.DiskCache); if (item != null && !item.IsInvalid) { return(dtsPath); } } return(null); } itemInfo.Type = FileCompilationType.TypeScript; CheckAdd(item.FullPath); TryToResolveFromBuildCache(itemInfo); if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath) && !itemInfo.NeedsCompilation()) { return(itemInfo.DtsLink.Owner.FullPath); } return(item.FullPath); }
public void AddSource(TSFileAdditionalInfo file) { _result.Path2FileInfo[file.Owner.FullPath] = file; }
TSFileAdditionalInfo GetFileInfo(IFileCache file) { return(TSFileAdditionalInfo.Get(file, _owner.DiskCache)); }
public string resolveLocalImport(string name, TSFileAdditionalInfo parentInfo) { return(ResolveLocalImport(name, parentInfo, null, null)); }
public string resolveModuleMain(string name, TSFileAdditionalInfo parentInfo) { if (!_owner.ProjectOptions.AllowModuleDeepImport) { if (!parentInfo.Owner.Name.EndsWith(".d.ts") && (name.Contains('/') || name.Contains('\\'))) { parentInfo.ReportDiag(true, -10, "Absolute import '" + name + "' must be just simple module name", 0, 0, 0, 0); return(null); } } var mname = PathUtils.EnumParts(name).First().name; var moduleInfo = TSProject.FindInfoForModule(_owner.Owner, parentInfo.Owner.Parent, _owner.DiskCache, _owner.Logger, mname, out var diskName); if (moduleInfo == null) { return(null); } if (mname != diskName) { parentInfo.ReportDiag(false, -2, "Module import has wrong casing '" + mname + "' on disk '" + diskName + "'", 0, 0, 0, 0); } moduleInfo.LoadProjectJson(true); if (mname.Length != name.Length) { return(ResolveLocalImport(PathUtils.Join(moduleInfo.Owner.FullPath, name.Substring(mname.Length + 1)), parentInfo, moduleInfo, name)); } parentInfo.ImportingModule(moduleInfo); var mainFile = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile); var item = _owner.DiskCache.TryGetItemPreferReal(mainFile) as IFileCache; if (item == null || item.IsInvalid) { return(null); } var itemInfo = TSFileAdditionalInfo.Get(item, _owner.DiskCache); moduleInfo.MainFileInfo = itemInfo; itemInfo.ImportedAsModule = name; itemInfo.MyProject = moduleInfo; var parentProject = parentInfo.MyProject; if (parentProject != null && parentProject.IsRootProject && !parentInfo.Owner.FullPath.Contains("/node_modules/") && ((parentProject.Dependencies == null || !parentProject.Dependencies.Contains(name)) && (parentProject.DevDependencies == null || !parentProject.DevDependencies.Contains(name)))) { parentInfo.ReportDiag(false, -12, "Importing module " + name + " without being in package.json as dependency", 0, 0, 0, 0); } if (moduleInfo.ProjectOptions?.ObsoleteMessage != null) { if (!PragmaParser.ParseIgnoreImportingObsolete(parentInfo.Owner.Utf8Content).Contains(name)) { parentInfo.ReportDiag(false, -14, "Importing obsolete module: " + moduleInfo.ProjectOptions?.ObsoleteMessage, 0, 0, 0, 0); } } AddSource(itemInfo); if (!IsTsOrTsx(mainFile)) { if (moduleInfo.MainFileNeedsToBeCompiled) { itemInfo.Type = FileCompilationType.EsmJavaScript; CheckAdd(mainFile); TryToResolveFromBuildCache(itemInfo); } else { itemInfo.Type = FileCompilationType.JavaScript; CheckAdd(mainFile); } if (moduleInfo.TypesMainFile != null) { var dtsPath = PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.TypesMainFile); item = _owner.DiskCache.TryGetItem(dtsPath) as IFileCache; itemInfo.DtsLink = TSFileAdditionalInfo.Get(item, _owner.DiskCache); if (item != null && !item.IsInvalid) { return(dtsPath); } } return(null); } itemInfo.Type = FileCompilationType.TypeScript; CheckAdd(item.FullPath); TryToResolveFromBuildCache(itemInfo); if (itemInfo.DtsLink != null && !ToCompile.Contains(item.FullPath) && !itemInfo.NeedsCompilation()) { var fp = itemInfo.DtsLink.Owner.FullPath; _readFileMap[fp] = itemInfo.DtsLink.Owner; return(fp); } return(item.FullPath); }