Esempio n. 1
0
        public (string?, SourceMap?) ReadContent(string name)
        {
            if (name == "<empty>")
            {
                return("module.exports = {};", null);
            }
            if (!_buildResult.Path2FileInfo.TryGetValue(name, out var fileInfo))
            {
                throw new InvalidOperationException("Bundler ReadContent does not exists:" + name);
            }

            if (fileInfo.Type == FileCompilationType.ImportedCss || fileInfo.Type == FileCompilationType.Css)
            {
                return("", null);
            }
            if (fileInfo.Type == FileCompilationType.Json)
            {
                if (BuildSourceMap)
                {
                    return(fileInfo.Owner.Utf8Content,
                           SourceMap.Identity(fileInfo.Owner.Utf8Content, fileInfo.Owner.FullPath));
                }
                return(fileInfo.Owner.Utf8Content, null);
            }

            if (fileInfo.Type == FileCompilationType.JavaScriptAsset ||
                fileInfo.Type == FileCompilationType.JavaScript)
            {
                if (BuildSourceMap)
                {
                    return(fileInfo.Output, SourceMap.Identity(fileInfo.Output, fileInfo.Owner.FullPath));
                }
                return(fileInfo.Output, null);
            }

            if (fileInfo.Type == FileCompilationType.TypeScriptDefinition)
            {
                return("", null);
            }

            if (fileInfo.Type is FileCompilationType.TypeScript or FileCompilationType.EsmJavaScript)
            {
                if (BuildSourceMap)
                {
                    var sourceMapBuilder = new SourceMapBuilder();
                    var adder            = sourceMapBuilder.CreateSourceAdder(fileInfo.Output, fileInfo.MapLink);
                    var sourceReplacer   = new SourceReplacer();
                    _project.ApplySourceInfo(sourceReplacer, fileInfo.SourceInfo, _buildResult);
                    sourceReplacer.Apply(adder);
                    return(sourceMapBuilder.Content(), sourceMapBuilder.Build(".", "."));
                }
                else
                {
                    var sourceMapBuilder = new SourceMapBuilder();
                    var adder            = sourceMapBuilder.CreateSourceAdder(fileInfo.Output, fileInfo.MapLink);
                    var sourceReplacer   = new SourceReplacer();
                    _project.ApplySourceInfo(sourceReplacer, fileInfo.SourceInfo, _buildResult);
                    sourceReplacer.Apply(adder);
                    return(sourceMapBuilder.Content(), null);
                }
            }

            throw new InvalidOperationException("Bundler Read Content unknown type " +
                                                Enum.GetName(typeof(FileCompilationType), fileInfo.Type) + ":" + name);
        }
Esempio n. 2
0
        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);
                }
            }
        }
Esempio n. 3
0
        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;
                    }
                }
            }
        }