Esempio n. 1
0
        public static TSProject?Create(IDirectoryCache?dir, IDiskCache diskCache, ILogger logger, string?diskName, bool virtualProject = false)
        {
            if (dir == null)
            {
                return(null);
            }
            if (dir.Project != null)
            {
                return((TSProject)dir.Project);
            }
            var proj = new TSProject
            {
                Owner          = dir,
                DiskCache      = diskCache,
                Logger         = logger,
                Name           = diskName,
                Valid          = true,
                ProjectOptions = new ProjectOptions()
            };

            proj.Virtual = virtualProject;
            proj.ProjectOptions.Owner = proj;
            if (virtualProject)
            {
                proj.ProjectOptions.FillProjectOptionsFromPackageJson(null);
            }
            else
            {
                dir.Project = proj;
            }

            return(proj);
        }
Esempio n. 2
0
        public string ResolveRequire(string name, string from)
        {
            if (name.StartsWith("./") || name.StartsWith("../"))
            {
                return(PathUtils.Join(PathUtils.Parent(from), name) + ".js");
            }

            var mname      = PathUtils.EnumParts(name).First().name;
            var diskCache  = Project.Owner.DiskCache;
            var moduleInfo = TSProject.FindInfoForModule(Project.Owner.Owner, diskCache.TryGetItem(PathUtils.Parent(from)) as IDirectoryCache, diskCache, Project.Owner.Logger, mname,
                                                         out var diskName);

            if (moduleInfo == null)
            {
                Project.Owner.Logger.Error($"Bundler cannot resolve {name} from {@from}");
                return(null);
            }
            if (mname.Length != name.Length)
            {
                return(PathUtils.ChangeExtension(
                           PathUtils.Join(moduleInfo.Owner.FullPath, name.Substring(mname.Length + 1)), "js"));
            }
            var mainFile =
                PathUtils.ChangeExtension(PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile), "js");

            return(mainFile);
        }
Esempio n. 3
0
        internal static TSProject CreateInvalid(string name)
        {
            var proj = new TSProject
            {
                Name  = name,
                Valid = false
            };

            return(proj);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public string ResolveRequire(string name, string from)
        {
            if (name.StartsWith("./") || name.StartsWith("../"))
            {
                return(PathUtils.Join(PathUtils.Parent(from), name) + ".js");
            }
            var diskCache  = Project.Owner.DiskCache;
            var moduleInfo = TSProject.FindInfoForModule(Project.Owner.Owner, diskCache, Project.Owner.Logger, name, out var diskName);

            if (moduleInfo == null)
            {
                return(null);
            }
            var mainFile = PathUtils.ChangeExtension(PathUtils.Join(moduleInfo.Owner.FullPath, moduleInfo.MainFile), "js");

            return(mainFile);
        }
Esempio n. 6
0
        public static TSProject Get(IDirectoryCache dir, IDiskCache diskCache, ILogger logger, string diskName)
        {
            if (dir == null)
            {
                return(null);
            }
            if (dir.AdditionalInfo == null)
            {
                var proj = new TSProject
                {
                    Owner          = dir, DiskCache = diskCache, Logger = logger, Name = diskName,
                    ProjectOptions = new ProjectOptions()
                };
                proj.ProjectOptions.Owner = proj;
                dir.AdditionalInfo        = proj;
            }

            return((TSProject)dir.AdditionalInfo);
        }
Esempio n. 7
0
        public static TSProject Create(IDirectoryCache dir, IDiskCache diskCache, ILogger logger, string diskName)
        {
            if (dir == null)
            {
                return(null);
            }
            var proj = new TSProject
            {
                Owner          = dir,
                DiskCache      = diskCache,
                Logger         = logger,
                Name           = diskName,
                Valid          = true,
                ProjectOptions = new ProjectOptions()
            };

            proj.ProjectOptions.Owner = proj;
            return(proj);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
 TSProject GetDirectoryInfo(IDirectoryCache dir)
 {
     return(TSProject.Get(dir, _owner.DiskCache, _owner.Logger));
 }
Esempio n. 12
0
        public TSProject?ResolveModule(string name)
        {
            if (Result.Modules.TryGetValue(name, out var module))
            {
                if (module.IterationId == IterationId)
                {
                    return(module.Valid ? module : null);
                }
                for (uint i = 0; i < module.NegativeChecks.Count; i++)
                {
                    if (CheckItemExistence(module.NegativeChecks[i]))
                    {
                        goto again;
                    }
                }

                if (module.Valid)
                {
                    module.LoadProjectJson(true, Owner.ProjectOptions);
                    if (module.PackageJsonChangeId == -1)
                    {
                        goto again;
                    }
                }

                module.IterationId = IterationId;
                return(module.Valid ? module : null);
            }

            again :;
            var negativeChecks = new BTDB.Collections.StructList <string>();
            var dir            = Owner.Owner.FullPath;

            while (dir.Length > 0)
            {
                var dc = Owner.DiskCache.TryGetItem(dir + "/node_modules/" + name) as IDirectoryCache;
                if (dc == null || dc.IsInvalid)
                {
                    negativeChecks.Add(dir + "/node_modules/" + name);
                }
                else
                {
                    if (dc.FullPath != dir + "/node_modules/" + name)
                    {
                        // Create it with proper casing
                        return(ResolveModule(dc.Name));
                    }

                    module = TSProject.Create(dc, Owner.DiskCache, Owner.Logger, name);
                    module.LoadProjectJson(true, Owner.ProjectOptions);
                    if (module.PackageJsonChangeId != -1)
                    {
                        module.NegativeChecks.AddRange(negativeChecks.AsSpan());
                        module.IterationId   = IterationId;
                        Result.Modules[name] = module;
                        return(module);
                    }
                }

                dir = PathUtils.Parent(dir).ToString();
            }

            module = TSProject.CreateInvalid(name);
            module.NegativeChecks.TransferFrom(ref negativeChecks);
            module.IterationId   = IterationId;
            Result.Modules[name] = module;
            return(null);
        }