RequireJsNet.Compressor.AutoDependency.ScriptProcessor.Process C# (CSharp) Method

Process() public method

public Process ( ) : void
return void
        public void Process()
        {
            try
            {
                var parser = new JavaScriptParser();
                var program = parser.Parse(OriginalString);
                var visitor = new RequireVisitor();
                var result = visitor.Visit(program, RelativeFileName);

                var lines = GetScriptLines(OriginalString);

                var flattenedResults = result.GetFlattened();

                var deps = flattenedResults
                    .SelectMany(r => r.Dependencies)
                    .Where(r => !r.Contains("!"))
                    .Except(new List<string> { "require", "module", "exports" });

                //Make existing relative paths relative to the Scripts folder
                var scriptPath = System.IO.Path.GetDirectoryName(RelativeFileName).Replace(@"\", "/") + "/";
                deps = deps.Select(r =>
                {
                    if (r.StartsWith("."))
                    {
                        r = scriptPath + r;
                    }
                    return r;
                });

                //Expand named paths relative to the scripts folder
                Dependencies = deps.Select(r => ExpandPaths(r, configuration)).ToList();

                var shim = this.GetShim(RelativeFileName);
                if (shim != null)
                {
                    Dependencies.AddRange(shim.Dependencies.Select(r => ExpandPaths(r.Dependency, configuration)));
                }

                Dependencies = Dependencies.Distinct().ToList();

                flattenedResults.ForEach(
                    x =>
                    {
                        EnsureHasRange(x.ParentNode.Node, lines);
                        EnsureHasRange(x.DependencyArrayNode, lines);
                        EnsureHasRange(x.ModuleDefinitionNode, lines);
                        EnsureHasRange(x.ModuleIdentifierNode, lines);
                        EnsureHasRange(x.SingleDependencyNode, lines);
                        var arguments = x.ParentNode.Node.As<CallExpression>().Arguments;
                        foreach (var arg in arguments)
                        {
                            EnsureHasRange(arg, lines);
                        }
                    });

                var transformations = this.GetTransformations(result, flattenedResults);
                var text = OriginalString;
                transformations.ExecuteAll(ref text);
                ProcessedString = text;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while processing {0}: {1}", RelativeFileName, ex.Message), ex);
            }
        }

Usage Example

        public override List<Bundle> ParseConfigs()
        {
            var bundles = new List<Bundle>();
            if (!Directory.Exists(ProjectPath))
            {
                throw new DirectoryNotFoundException("Could not find project directory.");
            }

            FindConfigs();

            var loader = new ConfigLoader(
                FilePaths,
                new ExceptionThrowingLogger(),
                new ConfigLoaderOptions { ProcessBundles = true });

            Configuration = loader.Get();
            
            foreach (var bundle in Configuration.AutoBundles.Bundles)
            {
                var files = new List<string>();
                var bundleResult = new Bundle
                                       {
                                           Files = new List<FileSpec>(),
                                           Output = this.GetOutputPath(bundle.OutputPath, bundle.Id),
                                           ContainingConfig = bundle.ContainingConfig,
                                           BundleId = bundle.Id
                                       };
                bundles.Add(bundleResult);

                var tempFileList = new List<RequireFile>();

                foreach (var include in bundle.Includes)
                {
                    // check if the file path is actually an URL
                    if (!string.IsNullOrEmpty(include.File) && !include.File.Contains("?"))
                    {
                        files.Add(this.ResolvePhysicalPath(include.File));
                    }
                    else if(!string.IsNullOrEmpty(include.Directory))
                    {
                        var absDirectory = this.GetAbsoluteDirectory(include.Directory);

                        // not using filter for this since we're going to use the one the user provided in the future
                        var dirFiles = Directory.GetFiles(absDirectory, "*", SearchOption.AllDirectories).Where(r => Path.GetExtension(r) == ".js").ToList();
                        files.AddRange(dirFiles);
                    }
                }

                files = files.Distinct().ToList();

                var fileQueue = new Queue<string>();
                this.EnqueueFileList(tempFileList, fileQueue, files);
                

                while (fileQueue.Any())
                {
                    var file = fileQueue.Dequeue();
                    var fileText = File.ReadAllText(file, encoding);
                    var relativePath = PathHelpers.GetRelativePath(file, EntryPoint + Path.DirectorySeparatorChar);
                    var processor = new ScriptProcessor(relativePath, fileText, Configuration);
                    processor.Process();
                    var result = processor.ProcessedString;
                    var dependencies = processor.Dependencies.Select(r => this.ResolvePhysicalPath(r)).Where(r => r != null).Distinct().ToList();
                    tempFileList.Add(new RequireFile
                                         {
                                             Name = file,
                                             Content = result,
                                             Dependencies = dependencies
                                         });

                    this.EnqueueFileList(tempFileList, fileQueue, dependencies);
                }

                while (tempFileList.Any())
                {
                    var addedFiles = bundleResult.Files.Select(r => r.FileName).ToList();
                    var noDeps = tempFileList.Where(r => !r.Dependencies.Any()
                                                        || r.Dependencies.All(x => addedFiles.Contains(x))).ToList();
                    if (!noDeps.Any())
                    {
                        noDeps = tempFileList.ToList();
                    }

                    foreach (var requireFile in noDeps)
                    {
                        bundleResult.Files.Add(new FileSpec(requireFile.Name, string.Empty) { FileContent = requireFile.Content });
                        tempFileList.Remove(requireFile);
                    }    
                }
            }

            this.WriteOverrideConfigs(bundles);

            return bundles;
        }
All Usage Examples Of RequireJsNet.Compressor.AutoDependency.ScriptProcessor::Process