예제 #1
0
        public void Run()
        {
            var stopwatch = Stopwatch.StartNew();

            foreach (var(splitName, mainFileList) in PartToMainFilesMap)
            {
                foreach (var mainFile in mainFileList)
                {
                    if (_cache.TryGetValue(mainFile, out var sourceFile))
                    {
                        MarkRequiredAs(sourceFile, splitName);
                        continue;
                    }

                    Check(mainFile, splitName);
                }

                _splitMap[splitName] = new SplitInfo(splitName)
                {
                    ShortName = _ctx.GenerateBundleName(splitName), PropName = "ERROR", IsMainSplit = true
                };
            }

            stopwatch.Stop();
            _ctx.ReportTime("Parse", stopwatch.Elapsed);
            foreach (var(_, sourceFile) in _cache)
            {
                foreach (var(file, path) in sourceFile.NeedsImports)
                {
                    _cache.TryGetValue(file, out var targetFile);
                    targetFile !.CreateWholeExport(path);
                }
            }

            var lazySplitCounter = 0;

            foreach (var f in _order)
            {
                var fullBundleName = f.PartOfBundle !;
                if (!_splitMap.TryGetValue(fullBundleName, out var split))
                {
                    var shortenedBundleName = _ctx.GenerateBundleName(fullBundleName);
                    split = new SplitInfo(fullBundleName)
                    {
                        ShortName = shortenedBundleName,
                        PropName  = BundlerHelpers.NumberToIdent(lazySplitCounter++)
                    };
                    _splitMap[fullBundleName] = split;
                }

                foreach (var dependency in f.PlainJsDependencies)
                {
                    split.PlainJsDependencies.Add(dependency);
                }
            }

            if (lazySplitCounter > 0)
            {
                DetectBundleExportsImports(ref lazySplitCounter);
            }

            foreach (var(splitName, splitInfo) in _splitMap)
            {
                var topLevelAst = Parser.Parse(_ctx.JsHeaders(splitName, lazySplitCounter > 0));
                if (GlobalDefines != null && GlobalDefines.Count > 0)
                {
                    topLevelAst.Body.Add(Helpers.EmitVarDefines(GlobalDefines));
                }
                topLevelAst.FigureOutScope();
                foreach (var jsDependency in splitInfo.PlainJsDependencies)
                {
                    var content = _ctx.ReadContent(jsDependency);
                    var jsAst   = Parser.Parse(content.Item1 !);
                    content.Item2?.ResolveInAst(jsAst);
                    jsAst.FigureOutScope();
                    BundlerHelpers.SimplifyJavaScriptDependency(jsAst);
                    _currentFileIdent = BundlerHelpers.FileNameToIdent(jsDependency);
                    BundlerHelpers.AppendToplevelWithRename(topLevelAst, jsAst, _currentFileIdent);
                }

                foreach (var sourceFile in _order)
                {
                    if (sourceFile.PartOfBundle != splitName)
                    {
                        continue;
                    }
                    foreach (var keyValuePair in sourceFile.Ast.Globals !)
                    {
                        topLevelAst.Globals !.TryAdd(keyValuePair.Key, keyValuePair.Value);
                        topLevelAst.NonRootSymbolNames?.Add(keyValuePair.Key);
                    }
                }

                AddExternallyImportedFromOtherBundles(topLevelAst, splitInfo);
                foreach (var sourceFile in _order)
                {
                    if (sourceFile.PartOfBundle != splitName)
                    {
                        continue;
                    }
                    _currentSourceFile = sourceFile;
                    _currentFileIdent  = BundlerHelpers.FileNameToIdent(sourceFile.Name);
                    BundlerHelpers.AppendToplevelWithRename(topLevelAst, sourceFile.Ast, _currentFileIdent
                                                            , BeforeAdd);
                }

                IfNeededPolyfillGlobal(topLevelAst);

                AddExportsFromLazyBundle(splitInfo, topLevelAst);

                BundlerHelpers.WrapByIIFE(topLevelAst);
                if (lazySplitCounter > 0 && PartToMainFilesMap.ContainsKey(splitName))
                {
                    var astVar = new AstVar(topLevelAst);
                    astVar.Definitions.Add(new AstVarDef(new AstSymbolVar("__bbb"), new AstObject()));
                    topLevelAst.Body.Insert(0) = astVar;
                }

                if (CompressOptions != null)
                {
                    stopwatch = Stopwatch.StartNew();
                    topLevelAst.FigureOutScope();
                    topLevelAst = topLevelAst.Compress(CompressOptions, new ScopeOptions
                    {
                        TopLevel       = false,
                        BeforeMangling = IgnoreEvalInTwoScopes
                    });
                    stopwatch.Stop();
                    _ctx.ReportTime("Compress", stopwatch.Elapsed);
                }

                if (Mangle)
                {
                    stopwatch = Stopwatch.StartNew();
                    topLevelAst.Mangle(new ScopeOptions
                    {
                        FrequencyCounting = MangleWithFrequencyCounting,
                        TopLevel          = false,
                        BeforeMangling    = IgnoreEvalInTwoScopes
                    }, OutputOptions);
                    stopwatch.Stop();
                    _ctx.ReportTime("Mangle", stopwatch.Elapsed);
                }

                _ctx.ModifyBundle(splitInfo.ShortName !, topLevelAst);

                if (GenerateSourceMap)
                {
                    stopwatch = Stopwatch.StartNew();
                    var builder = new SourceMapBuilder();
                    topLevelAst.PrintToBuilder(builder, OutputOptions);
                    stopwatch.Stop();
                    _ctx.ReportTime("Print", stopwatch.Elapsed);
                    _ctx.WriteBundle(splitInfo.ShortName !, builder);
                }
                else
                {
                    stopwatch = Stopwatch.StartNew();
                    var content = topLevelAst.PrintToString(OutputOptions);
                    stopwatch.Stop();
                    _ctx.ReportTime("Print", stopwatch.Elapsed);
                    _ctx.WriteBundle(splitInfo.ShortName !, content);
                }
            }
        }
예제 #2
0
        public void Run()
        {
            foreach (var(splitName, mainFileList) in PartToMainFilesMap)
            {
                foreach (var mainFile in mainFileList)
                {
                    if (_cache.TryGetValue(mainFile, out var sourceFile))
                    {
                        MarkRequiredAs(sourceFile, splitName);
                        continue;
                    }

                    Check(mainFile, splitName);
                }

                _splitMap[splitName] = new SplitInfo(splitName)
                {
                    ShortName = _ctx.GenerateBundleName(splitName), PropName = "ERROR", IsMainSplit = true
                };
            }

            foreach (var(_, sourceFile) in _cache)
            {
                if (sourceFile.NeedsWholeExport && sourceFile.WholeExport == null)
                {
                    sourceFile.CreateWholeExport();
                }
            }

            var lazySplitCounter = 0;

            foreach (var f in _order)
            {
                var fullBundleName = f.PartOfBundle !;
                if (!_splitMap.TryGetValue(fullBundleName, out var split))
                {
                    var shortenedBundleName = _ctx.GenerateBundleName(fullBundleName);
                    split = new SplitInfo(fullBundleName)
                    {
                        ShortName = shortenedBundleName,
                        PropName  = BundlerHelpers.NumberToIdent(lazySplitCounter++)
                    };
                    _splitMap[fullBundleName] = split;
                }

                foreach (var dependency in f.PlainJsDependencies)
                {
                    split.PlainJsDependencies.Add(dependency);
                }
            }

            if (lazySplitCounter > 0)
            {
                DetectBundleExportsImports(ref lazySplitCounter);
            }

            foreach (var(splitName, splitInfo) in _splitMap)
            {
                var topLevelAst = new Parser(new Options(), _ctx.JsHeaders(splitName, lazySplitCounter > 0)).Parse();
                if (GlobalDefines != null && GlobalDefines.Count > 0)
                {
                    topLevelAst.Body.Add(Helpers.EmitVarDefines(GlobalDefines));
                }
                topLevelAst.FigureOutScope();
                foreach (var jsDependency in splitInfo.PlainJsDependencies)
                {
                    var jsAst = new Parser(new Options(), _ctx.ReadContent(jsDependency) !).Parse();
                    jsAst.FigureOutScope();
                    _currentFileIdent = BundlerHelpers.FileNameToIdent(jsDependency);
                    BundlerHelpers.AppendToplevelWithRename(topLevelAst, jsAst, _currentFileIdent);
                }

                AddExternallyImportedFromOtherBundles(topLevelAst, splitInfo);
                foreach (var sourceFile in _order)
                {
                    if (sourceFile.PartOfBundle != splitName)
                    {
                        continue;
                    }
                    _currentSourceFile = sourceFile;
                    _currentFileIdent  = BundlerHelpers.FileNameToIdent(sourceFile.Name);
                    BundlerHelpers.AppendToplevelWithRename(topLevelAst, sourceFile.Ast, _currentFileIdent
                                                            , BeforeAdd);
                }

                AddExportsFromLazyBundle(splitInfo, topLevelAst);

                BundlerHelpers.WrapByIIFE(topLevelAst);
                if (lazySplitCounter > 0 && PartToMainFilesMap.ContainsKey(splitName))
                {
                    var astVar = new AstVar(topLevelAst);
                    astVar.Definitions.Add(new AstVarDef(new AstSymbolVar("__bbb"), new AstObject()));
                    topLevelAst.Body.Insert(0) = astVar;
                }

                if (CompressOptions != null)
                {
                    topLevelAst.FigureOutScope();
                    topLevelAst = topLevelAst.Compress(CompressOptions);
                }

                if (Mangle)
                {
                    topLevelAst.Mangle(new ScopeOptions
                    {
                        FrequencyCounting = MangleWithFrequencyCounting, TopLevel = false,
                        BeforeMangling    = IgnoreEvalInTwoScopes
                    });
                }

                _ctx.WriteBundle(splitInfo.ShortName !, topLevelAst.PrintToString(OutputOptions));
            }
        }