Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }