public static Dictionary <string, string> BundlerTestCore(BundlerTestData testData) { var output = new Dictionary <string, string>(); var bundler = new BundlerImpl(new BundlerCtx(testData, output, "cbm-")); bundler.Mangle = true; bundler.CompressOptions = CompressOptions.Default; bundler.OutputOptions = new() { Beautify = true }; InitCommonParts(testData); bundler.Run(); bundler = new(new BundlerCtx(testData, output, "cm-")); bundler.Mangle = true; bundler.CompressOptions = CompressOptions.Default; bundler.OutputOptions = new() { Beautify = false }; InitCommonParts(testData); bundler.Run(); bundler = new(new BundlerCtx(testData, output, "cb-")); bundler.Mangle = false; bundler.CompressOptions = CompressOptions.Default; bundler.OutputOptions = new() { Beautify = true }; InitCommonParts(testData); bundler.Run(); bundler = new(new BundlerCtx(testData, output, "b-")); bundler.Mangle = false; bundler.CompressOptions = null; bundler.OutputOptions = new() { Beautify = true }; InitCommonParts(testData); bundler.Run(); return(output); void InitCommonParts(BundlerTestData testData) { var libraryMode = testData.Name.StartsWith("Library"); bundler.LibraryMode = libraryMode; bundler.PartToMainFilesMap = new Dictionary <string, IReadOnlyList <string> > { { "bundle", new[] { "index.js" } } }; bundler.GlobalDefines = new Dictionary <string, object> { { "DEBUG", false } }; if (libraryMode) { bundler.OutputOptions.Ecma = 10; } } } public class BundlerCtx : IBundlerCtx {
public void BasicTest() { var bundler = new BundlerImpl(_tools); bundler.MainFiles = new List <string> { "index.js" }; bundler.Defines = new Dictionary <string, object> { { "DEBUG", false } }; var callbacks = new FakeCallbacks(this); bundler.Callbacks = callbacks; bundler.Bundle(); Assert.Equal("!function(o){\"use strict\";function n(){console.log(\"Hello\")}n()}();", callbacks.Result["bundle.js"]); }
public void Build(bool compress, bool mangle, bool beautify) { var diskCache = Project.Owner.DiskCache; var root = Project.Owner.Owner.FullPath; _jsFilesContent = new Dictionary <string, string>(); var cssLink = ""; var cssToBundle = new List <SourceFromPair>(); foreach (var source in BuildResult.Path2FileInfo) { if (source.Value.Type == FileCompilationType.TypeScript || source.Value.Type == FileCompilationType.JavaScript || source.Value.Type == FileCompilationType.JavaScriptAsset) { if (source.Value.Output == null) { continue; // Skip d.ts } _jsFilesContent[PathUtils.ChangeExtension(source.Key, "js").ToLowerInvariant()] = source.Value.Output; } else if (source.Value.Type == FileCompilationType.Css) { cssToBundle.Add(new SourceFromPair(source.Value.Owner.Utf8Content, source.Value.Owner.FullPath)); } else if (source.Value.Type == FileCompilationType.Resource) { FilesContent[source.Value.OutputUrl] = source.Value.Owner.ByteContent; } } if (cssToBundle.Count > 0) { string cssPath = Project.AllocateName("bundle.css"); var cssProcessor = new CssProcessor(Project.Tools); FilesContent[cssPath] = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) => { var full = PathUtils.Join(from, url); var fullJustName = full.Split('?', '#')[0]; var fileAdditionalInfo = BuildModuleCtx.AutodetectAndAddDependencyCore(Project, fullJustName, diskCache.TryGetItem(from) as IFileCache); FilesContent[fileAdditionalInfo.OutputUrl] = fileAdditionalInfo.Owner.ByteContent; return(PathUtils.SplitDirAndFile(fileAdditionalInfo.OutputUrl).Item2 + full.Substring(fullJustName.Length)); }).Result; cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">"; } if (Project.SpriteGeneration) { _bundlePng = Project.BundlePngUrl; var bundlePngContent = Project.SpriteGenerator.BuildImage(true); if (bundlePngContent != null) { _bundlePngInfo = new List <float>(); foreach (var slice in bundlePngContent) { FilesContent[PathUtils.InjectQuality(_bundlePng, slice.Quality)] = slice.Content; _bundlePngInfo.Add(slice.Quality); } } else { _bundlePng = null; } } var bundler = new BundlerImpl(_tools); bundler.Callbacks = this; if (Project.ExampleSources.Count > 0) { bundler.MainFiles = new[] { PathUtils.ChangeExtension(Project.ExampleSources[0], "js") }; } else { bundler.MainFiles = new[] { PathUtils.ChangeExtension(Project.MainFile, "js") }; } _mainJsBundleUrl = Project.BundleJsUrl; bundler.Compress = compress; bundler.Mangle = mangle; bundler.Beautify = beautify; var defines = new Dictionary <string, object>(); foreach (var p in Project.Defines) { defines.Add(p.Key, p.Value); } bundler.Defines = defines; bundler.Bundle(); if (!Project.NoHtml) { BuildFastBundlerIndexHtml(cssLink); FilesContent["index.html"] = _indexHtml; } }
public void Build(bool compress, bool mangle, bool beautify, bool buildSourceMap, string?sourceMapSourceRoot) { BuildSourceMap = buildSourceMap; SourceMapSourceRoot = sourceMapSourceRoot; var cssLink = ""; var cssToBundle = new List <SourceFromPair>(); foreach (var source in _buildResult.Path2FileInfo.Values.OrderBy(f => f.Owner.FullPath).ToArray()) { if (source.Type == FileCompilationType.Css || source.Type == FileCompilationType.ImportedCss) { cssToBundle.Add(new SourceFromPair(source.Owner.Utf8Content, source.Owner.FullPath)); } else if (source.Type == FileCompilationType.Resource) { _mainBuildResult.FilesContent.GetOrAddValueRef(_buildResult.ToOutputUrl(source)) = source.Owner.ByteContent; } } if (cssToBundle.Count > 0) { string cssPath = _mainBuildResult.AllocateName("bundle.css"); var cssProcessor = new CssProcessor(_project.Tools); var cssContent = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) => { var full = PathUtils.Join(@from, url); var fullJustName = full.Split('?', '#')[0]; _buildResult.Path2FileInfo.TryGetValue(fullJustName, out var fileAdditionalInfo); _mainBuildResult.FilesContent.GetOrAddValueRef(_buildResult.ToOutputUrl(fileAdditionalInfo)) = fileAdditionalInfo.Owner.ByteContent; return(PathUtils.GetFile(fileAdditionalInfo.OutputUrl) + full.Substring(fullJustName.Length)); }).Result; var cssImports = ""; foreach (var match in Regex.Matches(cssContent, "@import .*;")) { cssImports += match.ToString(); cssContent = cssContent.Replace(match.ToString(), ""); } _mainBuildResult.FilesContent.GetOrAddValueRef(cssPath) = cssImports + cssContent; cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">"; } if (_project.SpriteGeneration) { _bundlePng = _project.BundlePngUrl; var bundlePngContent = _project.SpriteGenerator.BuildImage(true); if (bundlePngContent != null) { _bundlePngInfo = new List <float>(); foreach (var slice in bundlePngContent) { _mainBuildResult.FilesContent.GetOrAddValueRef( PathUtils.InjectQuality(_bundlePng, slice.Quality)) = slice.Content; _bundlePngInfo.Add(slice.Quality); } } else { _bundlePng = null; } } _mainJsBundleUrl = _buildResult.BundleJsUrl; var bundler = new BundlerImpl(this); if ((_project.ExampleSources?.Count ?? 0) > 0) { bundler.PartToMainFilesMap = new Dictionary <string, IReadOnlyList <string> > { { "Bundle", new[] { _project.ExampleSources[0] } } }; } else { bundler.PartToMainFilesMap = new Dictionary <string, IReadOnlyList <string> > { { "Bundle", new[] { _project.MainFile } } }; } bundler.CompressOptions = compress ? CompressOptions.FastDefault : null; bundler.Mangle = mangle; bundler.OutputOptions = new OutputOptions { Beautify = beautify, ShortenBooleans = !beautify }; bundler.GenerateSourceMap = BuildSourceMap; bundler.GlobalDefines = _project.BuildDefines(_mainBuildResult); bundler.Run(); if (!_project.NoHtml) { BuildFastBundlerIndexHtml(cssLink); _mainBuildResult.FilesContent.GetOrAddValueRef("index.html") = _indexHtml; } if (_project.SubProjects != null) { var newSubBundlers = new RefDictionary <string, NjsastBundleBundler>(); foreach (var(projPath, subProject) in _project.SubProjects.OrderBy(a => a.Value?.Variant == "serviceworker")) { if (subProject == null) { continue; } if (_subBundlers == null || !_subBundlers.TryGetValue(projPath, out var subBundler)) { subBundler = new NjsastBundleBundler(_tools, _logger, _mainBuildResult, subProject, _buildResult.SubBuildResults.GetOrFakeValueRef(projPath)); } newSubBundlers.GetOrAddValueRef(projPath) = subBundler; subBundler.Build(compress, mangle, beautify, buildSourceMap, sourceMapSourceRoot); } _subBundlers = newSubBundlers; } else { _subBundlers = null; } }
public void Build(bool compress, bool mangle, bool beautify) { var diskCache = Project.Owner.DiskCache; var cssLink = ""; var cssToBundle = new List <SourceFromPair>(); foreach (var source in BuildResult.Path2FileInfo.Values.OrderBy(f => f.Owner.FullPath).ToArray()) { if (source.Type == FileCompilationType.Css || source.Type == FileCompilationType.ImportedCss) { cssToBundle.Add(new SourceFromPair(source.Owner.Utf8Content, source.Owner.FullPath)); } else if (source.Type == FileCompilationType.Resource) { FilesContent.GetOrAddValueRef(BuildResult.ToOutputUrl(source)) = source.Owner.ByteContent; } } if (cssToBundle.Count > 0) { string cssPath = BuildResult.AllocateName("bundle.css"); var cssProcessor = new CssProcessor(Project.Tools); var cssContent = cssProcessor.ConcatenateAndMinifyCss(cssToBundle, (string url, string from) => { var full = PathUtils.Join(from, url); var fullJustName = full.Split('?', '#')[0]; BuildResult.Path2FileInfo.TryGetValue(fullJustName, out var fileAdditionalInfo); FilesContent.GetOrAddValueRef(BuildResult.ToOutputUrl(fileAdditionalInfo)) = fileAdditionalInfo.Owner.ByteContent; return(PathUtils.GetFile(fileAdditionalInfo.OutputUrl) + full.Substring(fullJustName.Length)); }).Result; var cssImports = ""; foreach (var match in Regex.Matches(cssContent, "@import .*;")) { cssImports += match.ToString(); cssContent = cssContent.Replace(match.ToString(), ""); } FilesContent.GetOrAddValueRef(cssPath) = cssImports + cssContent; cssLink += "<link rel=\"stylesheet\" href=\"" + cssPath + "\">"; } if (Project.SpriteGeneration) { _bundlePng = Project.BundlePngUrl; var bundlePngContent = Project.SpriteGenerator.BuildImage(true); if (bundlePngContent != null) { _bundlePngInfo = new List <float>(); foreach (var slice in bundlePngContent) { FilesContent.GetOrAddValueRef(PathUtils.InjectQuality(_bundlePng, slice.Quality)) = slice.Content; _bundlePngInfo.Add(slice.Quality); } } else { _bundlePng = null; } } var bundler = new BundlerImpl(_tools); bundler.Callbacks = this; if (Project.ExampleSources.Count > 0) { bundler.MainFiles = new[] { Project.ExampleSources[0] }; } else { bundler.MainFiles = new[] { Project.MainFile }; } _mainJsBundleUrl = BuildResult.BundleJsUrl; bundler.Compress = compress; bundler.Mangle = mangle; bundler.Beautify = beautify; var defines = new Dictionary <string, object>(); foreach (var p in Project.Defines) { defines.Add(p.Key, p.Value); } bundler.Defines = defines; bundler.Bundle(); if (!Project.NoHtml) { BuildFastBundlerIndexHtml(cssLink); FilesContent.GetOrAddValueRef("index.html") = _indexHtml; } }