From eba56eea71dfabb4370af470a4aad313813afd93 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Wed, 2 Feb 2022 05:57:33 +0000 Subject: [PATCH] internal minify flags now match external flags --- internal/bundler/bundler.go | 10 +- internal/bundler/bundler_dce_test.go | 22 ++-- internal/bundler/bundler_default_test.go | 38 +++--- internal/bundler/bundler_importstar_test.go | 6 +- .../bundler/bundler_importstar_ts_test.go | 6 +- internal/bundler/bundler_splitting_test.go | 2 +- internal/bundler/bundler_ts_test.go | 38 +++--- internal/bundler/linker.go | 30 ++--- internal/config/config.go | 4 +- internal/css_parser/css_decls.go | 100 ++++++++-------- .../css_parser/css_decls_border_radius.go | 16 +-- internal/css_parser/css_decls_box.go | 12 +- internal/css_parser/css_decls_box_shadow.go | 2 +- internal/css_parser/css_decls_font.go | 2 +- internal/css_parser/css_decls_font_family.go | 2 +- internal/css_parser/css_parser.go | 32 +++--- internal/css_parser/css_parser_selector.go | 2 +- internal/css_parser/css_parser_test.go | 16 +-- internal/css_parser/css_reduce_calc.go | 2 +- internal/css_printer/css_printer.go | 52 ++++----- internal/css_printer/css_printer_test.go | 4 +- internal/js_parser/js_parser.go | 108 +++++++++--------- internal/js_parser/js_parser_lower.go | 4 +- internal/js_parser/js_parser_test.go | 4 +- internal/js_parser/json_parser_test.go | 2 +- internal/js_parser/ts_parser.go | 12 +- internal/js_printer/js_printer.go | 62 +++++----- internal/js_printer/js_printer_test.go | 20 ++-- pkg/api/api_impl.go | 8 +- 29 files changed, 309 insertions(+), 309 deletions(-) diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go index 89a862fab03..2cdc3e99120 100644 --- a/internal/bundler/bundler.go +++ b/internal/bundler/bundler.go @@ -221,8 +221,8 @@ func parseFile(args parseArgs) { case config.LoaderCSS: ast := args.caches.CSSCache.Parse(args.log, source, css_parser.Options{ - MangleSyntax: args.options.MangleSyntax, - RemoveWhitespace: args.options.RemoveWhitespace, + MinifySyntax: args.options.MinifySyntax, + MinifyWhitespace: args.options.MinifyWhitespace, UnsupportedCSSFeatures: args.options.UnsupportedCSSFeatures, OriginalTargetEnv: args.options.OriginalTargetEnv, }) @@ -2326,7 +2326,7 @@ func (b *Bundle) generateMetadataJSON(results []graph.OutputFile, allReachableFi } type runtimeCacheKey struct { - MangleSyntax bool + MinifySyntax bool MinifyIdentifiers bool ES6 bool } @@ -2341,7 +2341,7 @@ var globalRuntimeCache runtimeCache func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.Source, runtimeAST js_ast.AST, ok bool) { key := runtimeCacheKey{ // All configuration options that the runtime code depends on must go here - MangleSyntax: options.MangleSyntax, + MinifySyntax: options.MinifySyntax, MinifyIdentifiers: options.MinifyIdentifiers, ES6: runtime.CanUseES6(options.UnsupportedJSFeatures), } @@ -2375,7 +2375,7 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger. log := logger.NewDeferLog(logger.DeferLogAll) runtimeAST, ok = js_parser.Parse(log, source, js_parser.OptionsFromConfig(&config.Options{ // These configuration options must only depend on the key - MangleSyntax: key.MangleSyntax, + MinifySyntax: key.MinifySyntax, MinifyIdentifiers: key.MinifyIdentifiers, UnsupportedJSFeatures: compat.UnsupportedJSFeatures( map[compat.Engine][]int{compat.ES: {constraint}}), diff --git a/internal/bundler/bundler_dce_test.go b/internal/bundler/bundler_dce_test.go index cfa4d922866..c4258e7910f 100644 --- a/internal/bundler/bundler_dce_test.go +++ b/internal/bundler/bundler_dce_test.go @@ -1384,7 +1384,7 @@ func TestDeadCodeFollowingJump(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputFile: "/out.js", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -1427,7 +1427,7 @@ func TestRemoveTrailingReturn(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputFile: "/out.js", - MangleSyntax: true, + MinifySyntax: true, OutputFormat: config.FormatESModule, }, }) @@ -1719,7 +1719,7 @@ func TestDCETypeOfEqualsStringMangle(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, OutputFormat: config.FormatIIFE, - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -1843,7 +1843,7 @@ func TestRemoveUnusedImports(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModePassThrough, - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -1864,7 +1864,7 @@ func TestRemoveUnusedImportsEval(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModePassThrough, - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -1888,7 +1888,7 @@ func TestRemoveUnusedImportsEvalTS(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModePassThrough, - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -2048,7 +2048,7 @@ func TestTreeShakingLoweredClassStaticFieldMinified(t *testing.T) { Mode: config.ModeBundle, AbsOutputDir: "/out", UnsupportedJSFeatures: compat.ClassField, - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -2255,7 +2255,7 @@ func TestInlineIdentityFunctionCalls(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputDir: "/out", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -2390,7 +2390,7 @@ func TestInlineEmptyFunctionCalls(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputDir: "/out", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -2461,7 +2461,7 @@ func TestInlineFunctionCallBehaviorChanges(t *testing.T) { options: config.Options{ Mode: config.ModePassThrough, AbsOutputDir: "/out", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -2481,7 +2481,7 @@ func TestInlineFunctionCallForInitDecl(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputDir: "/out", - MangleSyntax: true, + MinifySyntax: true, }, }) } diff --git a/internal/bundler/bundler_default_test.go b/internal/bundler/bundler_default_test.go index 57aad316b20..8da5b24abf2 100644 --- a/internal/bundler/bundler_default_test.go +++ b/internal/bundler/bundler_default_test.go @@ -893,7 +893,7 @@ func TestMinifiedDynamicImportWithExpressionCJS(t *testing.T) { Mode: config.ModeConvertFormat, OutputFormat: config.FormatCommonJS, AbsOutputFile: "/out.js", - RemoveWhitespace: true, + MinifyWhitespace: true, }, }) } @@ -1358,7 +1358,7 @@ func TestRequireFSNodeMinify(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModeBundle, - RemoveWhitespace: true, + MinifyWhitespace: true, OutputFormat: config.FormatCommonJS, AbsOutputFile: "/out.js", Platform: config.PlatformNode, @@ -1560,8 +1560,8 @@ func TestMinifiedBundleES6(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModeBundle, - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputFile: "/out.js", }, @@ -1587,8 +1587,8 @@ func TestMinifiedBundleCommonJS(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModeBundle, - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputFile: "/out.js", }, @@ -1605,7 +1605,7 @@ func TestMinifiedBundleEndingWithImportantSemicolon(t *testing.T) { entryPaths: []string{"/entry.js"}, options: config.Options{ Mode: config.ModeBundle, - RemoveWhitespace: true, + MinifyWhitespace: true, OutputFormat: config.FormatIIFE, AbsOutputFile: "/out.js", }, @@ -2658,9 +2658,9 @@ func TestMinifyNestedLabelsNoBundle(t *testing.T) { }, entryPaths: []string{"/entry.js"}, options: config.Options{ - RemoveWhitespace: true, + MinifyWhitespace: true, MinifyIdentifiers: true, - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -2755,8 +2755,8 @@ func TestUseStrictDirectiveMinifyNoBundle(t *testing.T) { }, entryPaths: []string{"/entry.js"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, AbsOutputFile: "/out.js", }, }) @@ -4384,7 +4384,7 @@ func TestKeepNamesTreeShaking(t *testing.T) { Mode: config.ModeBundle, AbsOutputFile: "/out.js", KeepNames: true, - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -4474,7 +4474,7 @@ func TestConstWithLet(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputFile: "/out.js", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -4495,7 +4495,7 @@ func TestConstWithLetNoBundle(t *testing.T) { options: config.Options{ Mode: config.ModePassThrough, AbsOutputFile: "/out.js", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -4874,7 +4874,7 @@ func TestQuotedPropertyMangle(t *testing.T) { Mode: config.ModeBundle, OutputFormat: config.FormatCommonJS, AbsOutputDir: "/out", - MangleSyntax: true, + MinifySyntax: true, ExternalSettings: config.ExternalSettings{ PreResolve: config.ExternalMatchers{Exact: map[string]bool{ "ext": true, @@ -4976,7 +4976,7 @@ func TestStrictModeNestedFnDeclKeepNamesVariableInliningIssue1552(t *testing.T) Mode: config.ModePassThrough, AbsOutputDir: "/out", KeepNames: true, - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -5114,7 +5114,7 @@ func TestMinifyIdentifiersImportPathFrequencyAnalysis(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, AbsOutputDir: "/out", - RemoveWhitespace: true, + MinifyWhitespace: true, MinifyIdentifiers: true, }, }) @@ -5186,7 +5186,7 @@ func TestNamedFunctionExpressionArgumentCollision(t *testing.T) { options: config.Options{ Mode: config.ModePassThrough, AbsOutputDir: "/out", - MangleSyntax: true, + MinifySyntax: true, }, }) } @@ -5414,7 +5414,7 @@ func TestManglePropsMinify(t *testing.T) { AbsOutputDir: "/out", MangleProps: regexp.MustCompile("_$"), MinifyIdentifiers: true, - MangleSyntax: true, + MinifySyntax: true, }, }) } diff --git a/internal/bundler/bundler_importstar_test.go b/internal/bundler/bundler_importstar_test.go index bb39499c3f7..981e1016829 100644 --- a/internal/bundler/bundler_importstar_test.go +++ b/internal/bundler/bundler_importstar_test.go @@ -419,7 +419,7 @@ func TestImportStarMangleNoBundleUnused(t *testing.T) { }, entryPaths: []string{"/entry.js"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -436,7 +436,7 @@ func TestImportStarMangleNoBundleCapture(t *testing.T) { }, entryPaths: []string{"/entry.js"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -453,7 +453,7 @@ func TestImportStarMangleNoBundleNoCapture(t *testing.T) { }, entryPaths: []string{"/entry.js"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) diff --git a/internal/bundler/bundler_importstar_ts_test.go b/internal/bundler/bundler_importstar_ts_test.go index e3c70f2b228..6ca55dab282 100644 --- a/internal/bundler/bundler_importstar_ts_test.go +++ b/internal/bundler/bundler_importstar_ts_test.go @@ -419,7 +419,7 @@ func TestTSImportStarMangleNoBundleUnused(t *testing.T) { }, entryPaths: []string{"/entry.ts"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -436,7 +436,7 @@ func TestTSImportStarMangleNoBundleCapture(t *testing.T) { }, entryPaths: []string{"/entry.ts"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) @@ -453,7 +453,7 @@ func TestTSImportStarMangleNoBundleNoCapture(t *testing.T) { }, entryPaths: []string{"/entry.ts"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, AbsOutputFile: "/out.js", }, }) diff --git a/internal/bundler/bundler_splitting_test.go b/internal/bundler/bundler_splitting_test.go index 999f025190e..21c74e74f36 100644 --- a/internal/bundler/bundler_splitting_test.go +++ b/internal/bundler/bundler_splitting_test.go @@ -449,7 +449,7 @@ func TestSplittingDuplicateChunkCollision(t *testing.T) { options: config.Options{ Mode: config.ModeBundle, CodeSplitting: true, - RemoveWhitespace: true, + MinifyWhitespace: true, OutputFormat: config.FormatESModule, AbsOutputDir: "/out", }, diff --git a/internal/bundler/bundler_ts_test.go b/internal/bundler/bundler_ts_test.go index a7ffd938764..e014aa7f459 100644 --- a/internal/bundler/bundler_ts_test.go +++ b/internal/bundler/bundler_ts_test.go @@ -400,8 +400,8 @@ func TestTSMinifyEnum(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", }, @@ -420,8 +420,8 @@ func TestTSMinifyNestedEnum(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", }, @@ -440,8 +440,8 @@ func TestTSMinifyNestedEnumNoLogicalAssignment(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", UnsupportedJSFeatures: compat.LogicalAssignment, @@ -461,8 +461,8 @@ func TestTSMinifyNestedEnumNoArrow(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", UnsupportedJSFeatures: compat.Arrow, @@ -490,8 +490,8 @@ func TestTSMinifyNamespace(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", }, @@ -518,8 +518,8 @@ func TestTSMinifyNamespaceNoLogicalAssignment(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", UnsupportedJSFeatures: compat.LogicalAssignment, @@ -547,8 +547,8 @@ func TestTSMinifyNamespaceNoArrow(t *testing.T) { }, entryPaths: []string{"/a.ts", "/b.ts"}, options: config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputDir: "/", UnsupportedJSFeatures: compat.Arrow, @@ -573,7 +573,7 @@ func TestTSMinifyDerivedClass(t *testing.T) { }, entryPaths: []string{"/entry.ts"}, options: config.Options{ - MangleSyntax: true, + MinifySyntax: true, UnsupportedJSFeatures: es(2015), AbsOutputFile: "/out.js", }, @@ -666,8 +666,8 @@ func TestTSMinifiedBundleES6(t *testing.T) { entryPaths: []string{"/entry.ts"}, options: config.Options{ Mode: config.ModeBundle, - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputFile: "/out.js", }, @@ -693,8 +693,8 @@ func TestTSMinifiedBundleCommonJS(t *testing.T) { entryPaths: []string{"/entry.ts"}, options: config.Options{ Mode: config.ModeBundle, - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, MinifyIdentifiers: true, AbsOutputFile: "/out.js", }, diff --git a/internal/bundler/linker.go b/internal/bundler/linker.go index 2a0033003ea..92a2c08af28 100644 --- a/internal/bundler/linker.go +++ b/internal/bundler/linker.go @@ -3810,7 +3810,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS( if len(stmtList.insideWrapperPrefix) > 0 { stmts = append(stmtList.insideWrapperPrefix, stmts...) } - if c.options.MangleSyntax { + if c.options.MinifySyntax { stmts = mergeAdjacentLocalStmts(stmts) } @@ -3916,7 +3916,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS( }} // "var foo, bar;" - if !c.options.MangleSyntax && len(decls) > 0 { + if !c.options.MinifySyntax && len(decls) > 0 { stmtList.outsideWrapperPrefix = append(stmtList.outsideWrapperPrefix, js_ast.Stmt{Data: &js_ast.SLocal{ Decls: decls, }}) @@ -3953,8 +3953,8 @@ func (c *linkerContext) generateCodeForFileInChunkJS( printOptions := js_printer.Options{ Indent: indent, OutputFormat: c.options.OutputFormat, - RemoveWhitespace: c.options.RemoveWhitespace, - MangleSyntax: c.options.MangleSyntax, + MinifyWhitespace: c.options.MinifyWhitespace, + MinifySyntax: c.options.MinifySyntax, ASCIIOnly: c.options.ASCIIOnly, ToCommonJSRef: toCommonJSRef, ToESMRef: toESMRef, @@ -4095,7 +4095,7 @@ func (c *linkerContext) generateEntryPointTailJS( // instead of "__export" but support for that would need to be added to // "cjs-module-lexer" and then we would need to be ok with not supporting // older versions of node that don't have that newly-added support. - if !c.options.RemoveWhitespace { + if !c.options.MinifyWhitespace { stmts = append(stmts, js_ast.Stmt{Data: &js_ast.SComment{Text: `// Annotate the CommonJS export names for ESM import in node:`}}, ) @@ -4290,8 +4290,8 @@ func (c *linkerContext) generateEntryPointTailJS( printOptions := js_printer.Options{ Indent: indent, OutputFormat: c.options.OutputFormat, - RemoveWhitespace: c.options.RemoveWhitespace, - MangleSyntax: c.options.MangleSyntax, + MinifyWhitespace: c.options.MinifyWhitespace, + MinifySyntax: c.options.MinifySyntax, ASCIIOnly: c.options.ASCIIOnly, ToCommonJSRef: toCommonJSRef, ToESMRef: toESMRef, @@ -4608,8 +4608,8 @@ func (c *linkerContext) generateChunkJS(chunks []chunkInfo, chunkIndex int, chun printOptions := js_printer.Options{ Indent: indent, OutputFormat: c.options.OutputFormat, - RemoveWhitespace: c.options.RemoveWhitespace, - MangleSyntax: c.options.MangleSyntax, + MinifyWhitespace: c.options.MinifyWhitespace, + MinifySyntax: c.options.MinifySyntax, } crossChunkImportRecords := make([]ast.ImportRecord, len(chunk.crossChunkImports)) for i, chunkImport := range chunk.crossChunkImports { @@ -4649,7 +4649,7 @@ func (c *linkerContext) generateChunkJS(chunks []chunkInfo, chunkIndex int, chun indent := "" space := " " newline := "\n" - if c.options.RemoveWhitespace { + if c.options.MinifyWhitespace { space = "" newline = "" } @@ -4792,7 +4792,7 @@ func (c *linkerContext) generateChunkJS(chunks []chunkInfo, chunkIndex int, chun } // Add a comment with the file path before the file contents - if c.options.Mode == config.ModeBundle && !c.options.RemoveWhitespace && + if c.options.Mode == config.ModeBundle && !c.options.MinifyWhitespace && prevFileNameComment != compileResult.sourceIndex && len(compileResult.JS) > 0 { if newlineBeforeComment { prevOffset.AdvanceString("\n") @@ -4932,7 +4932,7 @@ func (c *linkerContext) generateGlobalNamePrefix() string { space := " " join := ";\n" - if c.options.RemoveWhitespace { + if c.options.MinifyWhitespace { space = "" join = ";" } @@ -5038,7 +5038,7 @@ func (c *linkerContext) generateChunkCSS(chunks []chunkInfo, chunkIndex int, chu } cssOptions := css_printer.Options{ - RemoveWhitespace: c.options.RemoveWhitespace, + MinifyWhitespace: c.options.MinifyWhitespace, ASCIIOnly: c.options.ASCIIOnly, LegalComments: c.options.LegalComments, AddSourceMappings: addSourceMappings, @@ -5098,7 +5098,7 @@ func (c *linkerContext) generateChunkCSS(chunks []chunkInfo, chunkIndex int, chu if len(tree.Rules) > 0 { result := css_printer.Print(tree, css_printer.Options{ - RemoveWhitespace: c.options.RemoveWhitespace, + MinifyWhitespace: c.options.MinifyWhitespace, ASCIIOnly: c.options.ASCIIOnly, }) if len(result.CSS) > 0 { @@ -5157,7 +5157,7 @@ func (c *linkerContext) generateChunkCSS(chunks []chunkInfo, chunkIndex int, chu } } - if c.options.Mode == config.ModeBundle && !c.options.RemoveWhitespace { + if c.options.Mode == config.ModeBundle && !c.options.MinifyWhitespace { var newline string if newlineBeforeComment { newline = "\n" diff --git a/internal/config/config.go b/internal/config/config.go index 2fbdf238c4a..56e9a606ace 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -246,9 +246,9 @@ type Options struct { TS TSOptions Mode Mode PreserveSymlinks bool - RemoveWhitespace bool + MinifyWhitespace bool MinifyIdentifiers bool - MangleSyntax bool + MinifySyntax bool ProfilerNames bool CodeSplitting bool WatchMode bool diff --git a/internal/css_parser/css_decls.go b/internal/css_parser/css_decls.go index 561ed37c410..ed17137780c 100644 --- a/internal/css_parser/css_decls.go +++ b/internal/css_parser/css_decls.go @@ -11,7 +11,7 @@ func (p *parser) commaToken() css_ast.Token { Kind: css_lexer.TComma, Text: ",", } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { t.Whitespace = css_ast.WhitespaceAfter } return t @@ -51,7 +51,7 @@ func expandTokenQuad(tokens []css_ast.Token, allowedIdent string) (result [4]css return } -func compactTokenQuad(a css_ast.Token, b css_ast.Token, c css_ast.Token, d css_ast.Token, removeWhitespace bool) []css_ast.Token { +func compactTokenQuad(a css_ast.Token, b css_ast.Token, c css_ast.Token, d css_ast.Token, minifyWhitespace bool) []css_ast.Token { tokens := []css_ast.Token{a, b, c, d} if tokens[3].EqualIgnoringWhitespace(tokens[1]) { if tokens[2].EqualIgnoringWhitespace(tokens[0]) { @@ -66,7 +66,7 @@ func compactTokenQuad(a css_ast.Token, b css_ast.Token, c css_ast.Token, d css_a } for i := range tokens { var whitespace css_ast.WhitespaceFlags - if !removeWhitespace || i > 0 { + if !minifyWhitespace || i > 0 { whitespace |= css_ast.WhitespaceBefore } if i+1 < len(tokens) { @@ -115,7 +115,7 @@ func (p *parser) processDeclarations(rules []css_ast.Rule) []css_ast.Rule { if len(decl.Value) == 1 { decl.Value[0] = p.lowerColor(decl.Value[0]) - if p.options.MangleSyntax { + if p.options.MinifySyntax { t := decl.Value[0] if hex, ok := parseColor(t); ok { decl.Value[0] = p.mangleColor(t, hex) @@ -124,124 +124,124 @@ func (p *parser) processDeclarations(rules []css_ast.Rule) []css_ast.Rule { } case css_ast.DFont: - if p.options.MangleSyntax { + if p.options.MinifySyntax { decl.Value = p.mangleFont(decl.Value) } case css_ast.DFontFamily: - if p.options.MangleSyntax { + if p.options.MinifySyntax { if value, ok := p.mangleFontFamily(decl.Value); ok { decl.Value = value } } case css_ast.DFontWeight: - if len(decl.Value) == 1 && p.options.MangleSyntax { + if len(decl.Value) == 1 && p.options.MinifySyntax { decl.Value[0] = p.mangleFontWeight(decl.Value[0]) } case css_ast.DTransform: - if p.options.MangleSyntax { + if p.options.MinifySyntax { decl.Value = p.mangleTransforms(decl.Value) } case css_ast.DBoxShadow: - if p.options.MangleSyntax { + if p.options.MinifySyntax { decl.Value = p.mangleBoxShadows(decl.Value) } // Margin case css_ast.DMargin: - if p.options.MangleSyntax { - margin.mangleSides(rules, decl, i, p.options.RemoveWhitespace) + if p.options.MinifySyntax { + margin.mangleSides(rules, decl, i, p.options.MinifyWhitespace) } case css_ast.DMarginTop: - if p.options.MangleSyntax { - margin.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxTop) + if p.options.MinifySyntax { + margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxTop) } case css_ast.DMarginRight: - if p.options.MangleSyntax { - margin.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxRight) + if p.options.MinifySyntax { + margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxRight) } case css_ast.DMarginBottom: - if p.options.MangleSyntax { - margin.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxBottom) + if p.options.MinifySyntax { + margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxBottom) } case css_ast.DMarginLeft: - if p.options.MangleSyntax { - margin.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxLeft) + if p.options.MinifySyntax { + margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxLeft) } // Padding case css_ast.DPadding: - if p.options.MangleSyntax { - padding.mangleSides(rules, decl, i, p.options.RemoveWhitespace) + if p.options.MinifySyntax { + padding.mangleSides(rules, decl, i, p.options.MinifyWhitespace) } case css_ast.DPaddingTop: - if p.options.MangleSyntax { - padding.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxTop) + if p.options.MinifySyntax { + padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxTop) } case css_ast.DPaddingRight: - if p.options.MangleSyntax { - padding.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxRight) + if p.options.MinifySyntax { + padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxRight) } case css_ast.DPaddingBottom: - if p.options.MangleSyntax { - padding.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxBottom) + if p.options.MinifySyntax { + padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxBottom) } case css_ast.DPaddingLeft: - if p.options.MangleSyntax { - padding.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxLeft) + if p.options.MinifySyntax { + padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxLeft) } // Inset case css_ast.DInset: - if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MangleSyntax { - inset.mangleSides(rules, decl, i, p.options.RemoveWhitespace) + if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax { + inset.mangleSides(rules, decl, i, p.options.MinifyWhitespace) } case css_ast.DTop: - if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MangleSyntax { - inset.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxTop) + if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax { + inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxTop) } case css_ast.DRight: - if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MangleSyntax { - inset.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxRight) + if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax { + inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxRight) } case css_ast.DBottom: - if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MangleSyntax { - inset.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxBottom) + if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax { + inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxBottom) } case css_ast.DLeft: - if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MangleSyntax { - inset.mangleSide(rules, decl, i, p.options.RemoveWhitespace, boxLeft) + if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax { + inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxLeft) } // Border radius case css_ast.DBorderRadius: - if p.options.MangleSyntax { - borderRadius.mangleCorners(rules, decl, i, p.options.RemoveWhitespace) + if p.options.MinifySyntax { + borderRadius.mangleCorners(rules, decl, i, p.options.MinifyWhitespace) } case css_ast.DBorderTopLeftRadius: - if p.options.MangleSyntax { - borderRadius.mangleCorner(rules, decl, i, p.options.RemoveWhitespace, borderRadiusTopLeft) + if p.options.MinifySyntax { + borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusTopLeft) } case css_ast.DBorderTopRightRadius: - if p.options.MangleSyntax { - borderRadius.mangleCorner(rules, decl, i, p.options.RemoveWhitespace, borderRadiusTopRight) + if p.options.MinifySyntax { + borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusTopRight) } case css_ast.DBorderBottomRightRadius: - if p.options.MangleSyntax { - borderRadius.mangleCorner(rules, decl, i, p.options.RemoveWhitespace, borderRadiusBottomRight) + if p.options.MinifySyntax { + borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusBottomRight) } case css_ast.DBorderBottomLeftRadius: - if p.options.MangleSyntax { - borderRadius.mangleCorner(rules, decl, i, p.options.RemoveWhitespace, borderRadiusBottomLeft) + if p.options.MinifySyntax { + borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusBottomLeft) } } } // Compact removed rules - if p.options.MangleSyntax { + if p.options.MinifySyntax { end := 0 for _, rule := range rules { if rule.Data != nil { diff --git a/internal/css_parser/css_decls_border_radius.go b/internal/css_parser/css_decls_border_radius.go index 7f776dc60db..bba664a62d9 100644 --- a/internal/css_parser/css_decls_border_radius.go +++ b/internal/css_parser/css_decls_border_radius.go @@ -35,7 +35,7 @@ func (borderRadius *borderRadiusTracker) updateCorner(rules []css_ast.Rule, corn borderRadius.corners[corner] = new } -func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, removeWhitespace bool) { +func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool) { // Reset if we see a change in the "!important" flag if borderRadius.important != decl.Important { borderRadius.corners = [4]borderRadiusCorner{} @@ -102,10 +102,10 @@ func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, dec } // Success - borderRadius.compactRules(rules, decl.KeyRange, removeWhitespace) + borderRadius.compactRules(rules, decl.KeyRange, minifyWhitespace) } -func (borderRadius *borderRadiusTracker) mangleCorner(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, removeWhitespace bool, corner int) { +func (borderRadius *borderRadiusTracker) mangleCorner(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool, corner int) { // Reset if we see a change in the "!important" flag if borderRadius.important != decl.Important { borderRadius.corners = [4]borderRadiusCorner{} @@ -148,13 +148,13 @@ func (borderRadius *borderRadiusTracker) mangleCorner(rules []css_ast.Rule, decl ruleIndex: uint32(index), wasSingleRule: true, }) - borderRadius.compactRules(rules, decl.KeyRange, removeWhitespace) + borderRadius.compactRules(rules, decl.KeyRange, minifyWhitespace) } else { borderRadius.corners = [4]borderRadiusCorner{} } } -func (borderRadius *borderRadiusTracker) compactRules(rules []css_ast.Rule, keyRange logger.Range, removeWhitespace bool) { +func (borderRadius *borderRadiusTracker) compactRules(rules []css_ast.Rule, keyRange logger.Range, minifyWhitespace bool) { // All tokens must be present if eof := css_lexer.TEndOfFile; borderRadius.corners[0].firstToken.Kind == eof || borderRadius.corners[1].firstToken.Kind == eof || borderRadius.corners[2].firstToken.Kind == eof || borderRadius.corners[3].firstToken.Kind == eof { @@ -174,18 +174,18 @@ func (borderRadius *borderRadiusTracker) compactRules(rules []css_ast.Rule, keyR borderRadius.corners[1].firstToken, borderRadius.corners[2].firstToken, borderRadius.corners[3].firstToken, - removeWhitespace, + minifyWhitespace, ) secondTokens := compactTokenQuad( borderRadius.corners[0].secondToken, borderRadius.corners[1].secondToken, borderRadius.corners[2].secondToken, borderRadius.corners[3].secondToken, - removeWhitespace, + minifyWhitespace, ) if !css_ast.TokensEqualIgnoringWhitespace(tokens, secondTokens) { var whitespace css_ast.WhitespaceFlags - if !removeWhitespace { + if !minifyWhitespace { whitespace = css_ast.WhitespaceBefore | css_ast.WhitespaceAfter } tokens = append(tokens, css_ast.Token{ diff --git a/internal/css_parser/css_decls_box.go b/internal/css_parser/css_decls_box.go index a905f091b3a..52587fdff36 100644 --- a/internal/css_parser/css_decls_box.go +++ b/internal/css_parser/css_decls_box.go @@ -92,7 +92,7 @@ func (box *boxTracker) updateSide(rules []css_ast.Rule, side int, new boxSide) { box.sides[side] = new } -func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, removeWhitespace bool) { +func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool) { // Reset if we see a change in the "!important" flag if box.important != decl.Important { box.sides = [4]boxSide{} @@ -121,13 +121,13 @@ func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclarat unitSafety: unitSafety, }) } - box.compactRules(rules, decl.KeyRange, removeWhitespace) + box.compactRules(rules, decl.KeyRange, minifyWhitespace) } else { box.sides = [4]boxSide{} } } -func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, removeWhitespace bool, side int) { +func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool, side int) { // Reset if we see a change in the "!important" flag if box.important != decl.Important { box.sides = [4]boxSide{} @@ -149,7 +149,7 @@ func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclarati wasSingleRule: true, unitSafety: unitSafety, }) - box.compactRules(rules, decl.KeyRange, removeWhitespace) + box.compactRules(rules, decl.KeyRange, minifyWhitespace) return } } @@ -157,7 +157,7 @@ func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclarati box.sides = [4]boxSide{} } -func (box *boxTracker) compactRules(rules []css_ast.Rule, keyRange logger.Range, removeWhitespace bool) { +func (box *boxTracker) compactRules(rules []css_ast.Rule, keyRange logger.Range, minifyWhitespace bool) { // All tokens must be present if eof := css_lexer.TEndOfFile; box.sides[0].token.Kind == eof || box.sides[1].token.Kind == eof || box.sides[2].token.Kind == eof || box.sides[3].token.Kind == eof { @@ -177,7 +177,7 @@ func (box *boxTracker) compactRules(rules []css_ast.Rule, keyRange logger.Range, box.sides[1].token, box.sides[2].token, box.sides[3].token, - removeWhitespace, + minifyWhitespace, ) // Remove all of the existing declarations diff --git a/internal/css_parser/css_decls_box_shadow.go b/internal/css_parser/css_decls_box_shadow.go index de13bf8bb6c..338f9a21a23 100644 --- a/internal/css_parser/css_decls_box_shadow.go +++ b/internal/css_parser/css_decls_box_shadow.go @@ -64,7 +64,7 @@ func (p *parser) mangleBoxShadow(tokens []css_ast.Token) []css_ast.Token { // Set the whitespace flags for i := range tokens { var whitespace css_ast.WhitespaceFlags - if i > 0 || !p.options.RemoveWhitespace { + if i > 0 || !p.options.MinifyWhitespace { whitespace |= css_ast.WhitespaceBefore } if i+1 < len(tokens) { diff --git a/internal/css_parser/css_decls_font.go b/internal/css_parser/css_decls_font.go index e08e211b706..3ae96908009 100644 --- a/internal/css_parser/css_decls_font.go +++ b/internal/css_parser/css_decls_font.go @@ -88,7 +88,7 @@ func (p *parser) mangleFont(tokens []css_ast.Token) []css_ast.Token { pos += 2 // Remove the whitespace around the "/" character - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { result[len(result)-3].Whitespace &= ^css_ast.WhitespaceAfter result[len(result)-2].Whitespace = 0 result[len(result)-1].Whitespace &= ^css_ast.WhitespaceBefore diff --git a/internal/css_parser/css_decls_font_family.go b/internal/css_parser/css_decls_font_family.go index 96d48150556..0ba8c665215 100644 --- a/internal/css_parser/css_decls_font_family.go +++ b/internal/css_parser/css_decls_font_family.go @@ -76,7 +76,7 @@ func (p *parser) mangleFamilyNameOrGenericName(result []css_ast.Token, tokens [] } for i, name := range names { var whitespace css_ast.WhitespaceFlags - if i != 0 || !p.options.RemoveWhitespace { + if i != 0 || !p.options.MinifyWhitespace { whitespace = css_ast.WhitespaceBefore } result = append(result, css_ast.Token{ diff --git a/internal/css_parser/css_parser.go b/internal/css_parser/css_parser.go index 9f4f458b779..6831e3e63b3 100644 --- a/internal/css_parser/css_parser.go +++ b/internal/css_parser/css_parser.go @@ -32,8 +32,8 @@ type parser struct { type Options struct { OriginalTargetEnv string UnsupportedCSSFeatures compat.CSSFeature - MangleSyntax bool - RemoveWhitespace bool + MinifySyntax bool + MinifyWhitespace bool } func Parse(log logger.Log, source logger.Source, options Options) css_ast.AST { @@ -259,7 +259,7 @@ loop: } } - if p.options.MangleSyntax { + if p.options.MinifySyntax { rules = mangleRules(rules) } return rules @@ -273,7 +273,7 @@ func (p *parser) parseListOfDeclarations() (list []css_ast.Rule) { case css_lexer.TEndOfFile, css_lexer.TCloseBrace: list = p.processDeclarations(list) - if p.options.MangleSyntax { + if p.options.MinifySyntax { list = mangleRules(list) } return @@ -706,7 +706,7 @@ func (p *parser) parseAtRule(context atRuleContext) css_ast.Rule { // Insert or remove whitespace before the first token if len(importConditions) > 0 { kind = ast.ImportAtConditional - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { importConditions[0].Whitespace &= ^css_ast.WhitespaceBefore } else { importConditions[0].Whitespace |= css_ast.WhitespaceBefore @@ -792,13 +792,13 @@ func (p *parser) parseAtRule(context atRuleContext) css_ast.Rule { text := p.decoded() if t.Kind == css_lexer.TIdent { if text == "from" { - if p.options.MangleSyntax { + if p.options.MinifySyntax { text = "0%" // "0%" is equivalent to but shorter than "from" } } else if text != "to" { p.expect(css_lexer.TPercentage) } - } else if p.options.MangleSyntax && text == "100%" { + } else if p.options.MinifySyntax && text == "100%" { text = "to" // "to" is equivalent to but shorter than "100%" } selectors = append(selectors, text) @@ -827,7 +827,7 @@ func (p *parser) parseAtRule(context atRuleContext) css_ast.Rule { p.expect(css_lexer.TCloseBrace) // "@keyframes { from {} to { color: red } }" => "@keyframes { to { color: red } }" - if !p.options.MangleSyntax || len(rules) > 0 { + if !p.options.MinifySyntax || len(rules) > 0 { blocks = append(blocks, css_ast.KeyframeBlock{ Selectors: selectors, Rules: rules, @@ -1001,14 +1001,14 @@ loop: } case css_lexer.TNumber: - if p.options.MangleSyntax { + if p.options.MinifySyntax { if text, ok := mangleNumber(token.Text); ok { token.Text = text } } case css_lexer.TPercentage: - if p.options.MangleSyntax { + if p.options.MinifySyntax { if text, ok := mangleNumber(token.PercentageValue()); ok { token.Text = text + "%" } @@ -1017,7 +1017,7 @@ loop: case css_lexer.TDimension: token.UnitOffset = t.UnitOffset - if p.options.MangleSyntax { + if p.options.MinifySyntax { if text, ok := mangleNumber(token.DimensionValue()); ok { token.Text = text + token.DimensionUnit() token.UnitOffset = uint16(len(text)) @@ -1058,7 +1058,7 @@ loop: token.Children = &nested // Apply "calc" simplification rules when minifying - if p.options.MangleSyntax && token.Text == "calc" { + if p.options.MinifySyntax && token.Text == "calc" { token = p.tryToReduceCalcExpression(token) } @@ -1090,7 +1090,7 @@ loop: nested, tokens = p.convertTokensHelper(tokens, css_lexer.TCloseBrace, opts) // Pretty-printing: insert leading and trailing whitespace when not minifying - if !opts.verbatimWhitespace && !p.options.RemoveWhitespace && len(nested) > 0 { + if !opts.verbatimWhitespace && !p.options.MinifyWhitespace && len(nested) > 0 { nested[0].Whitespace |= css_ast.WhitespaceBefore nested[len(nested)-1].Whitespace |= css_ast.WhitespaceAfter } @@ -1127,7 +1127,7 @@ loop: } // Assume whitespace can always be added after a comma - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { token.Whitespace &= ^css_ast.WhitespaceAfter if i+1 < len(result) { result[i+1].Whitespace &= ^css_ast.WhitespaceBefore @@ -1268,7 +1268,7 @@ func (p *parser) parseSelectorRuleFrom(preludeStart int, opts parseSelectorOpts) p.expect(css_lexer.TCloseBrace) // Minify "@nest" when possible - if p.options.MangleSyntax && selector.HasAtNest { + if p.options.MinifySyntax && selector.HasAtNest { allHaveNestPrefix := true for _, complex := range selector.Selectors { if len(complex.Selectors) == 0 || complex.Selectors[0].NestingSelector != css_ast.NestingSelectorPrefix { @@ -1380,7 +1380,7 @@ stop: // Insert or remove whitespace before the first token if !verbatimWhitespace && len(result) > 0 { - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { result[0].Whitespace &= ^css_ast.WhitespaceBefore } else { result[0].Whitespace |= css_ast.WhitespaceBefore diff --git a/internal/css_parser/css_parser_selector.go b/internal/css_parser/css_parser_selector.go index 073bb92d113..43b14f1cad7 100644 --- a/internal/css_parser/css_parser_selector.go +++ b/internal/css_parser/css_parser_selector.go @@ -190,7 +190,7 @@ subclassSelectors: // and ::first-letter) may, for legacy reasons, be represented using // the grammar, with only a single ":" // character at their start. - if p.options.MangleSyntax && isElement && len(pseudo.Args) == 0 { + if p.options.MinifySyntax && isElement && len(pseudo.Args) == 0 { switch pseudo.Name { case "before", "after", "first-line", "first-letter": isElement = false diff --git a/internal/css_parser/css_parser_test.go b/internal/css_parser/css_parser_test.go index 4d09c630a55..bd24104ff22 100644 --- a/internal/css_parser/css_parser_test.go +++ b/internal/css_parser/css_parser_test.go @@ -32,8 +32,8 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st t.Helper() log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug) tree := Parse(log, test.SourceForTest(contents), Options{ - MangleSyntax: options.MangleSyntax, - RemoveWhitespace: options.RemoveWhitespace, + MinifySyntax: options.MinifySyntax, + MinifyWhitespace: options.MinifyWhitespace, UnsupportedCSSFeatures: options.UnsupportedCSSFeatures, }) msgs := log.Done() @@ -45,7 +45,7 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st } test.AssertEqualWithDiff(t, text, "") result := css_printer.Print(tree, css_printer.Options{ - RemoveWhitespace: options.RemoveWhitespace, + MinifyWhitespace: options.MinifyWhitespace, }) test.AssertEqualWithDiff(t, string(result.CSS), expected) }) @@ -66,14 +66,14 @@ func expectPrintedLower(t *testing.T, contents string, expected string) { func expectPrintedMinify(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [minify]", contents, expected, config.Options{ - RemoveWhitespace: true, + MinifyWhitespace: true, }) } func expectPrintedMangle(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [mangle]", contents, expected, config.Options{ - MangleSyntax: true, + MinifySyntax: true, }) } @@ -81,15 +81,15 @@ func expectPrintedLowerMangle(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [mangle]", contents, expected, config.Options{ UnsupportedCSSFeatures: ^compat.CSSFeature(0), - MangleSyntax: true, + MinifySyntax: true, }) } func expectPrintedMangleMinify(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [mangle, minify]", contents, expected, config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, }) } diff --git a/internal/css_parser/css_reduce_calc.go b/internal/css_parser/css_reduce_calc.go index d3a32cb5fea..b28ff56e309 100644 --- a/internal/css_parser/css_reduce_calc.go +++ b/internal/css_parser/css_reduce_calc.go @@ -13,7 +13,7 @@ import ( func (p *parser) tryToReduceCalcExpression(token css_ast.Token) css_ast.Token { if term := tryToParseCalcTerm(*token.Children); term != nil { whitespace := css_ast.WhitespaceBefore | css_ast.WhitespaceAfter - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { whitespace = 0 } term = term.partiallySimplify() diff --git a/internal/css_printer/css_printer.go b/internal/css_printer/css_printer.go index 37b765ec426..bf7eb276d58 100644 --- a/internal/css_printer/css_printer.go +++ b/internal/css_printer/css_printer.go @@ -32,7 +32,7 @@ type Options struct { // us do binary search on to figure out what line a given AST node came from LineOffsetTables []sourcemap.LineOffsetTable - RemoveWhitespace bool + MinifyWhitespace bool ASCIIOnly bool AddSourceMappings bool LegalComments config.LegalComments @@ -81,7 +81,7 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol p.builder.AddSourceMapping(rule.Loc, p.css) } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.printIndent(indent) } @@ -95,7 +95,7 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol p.print(";") case *css_ast.RAtImport: - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print("@import") } else { p.print("@import ") @@ -113,22 +113,22 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol } else { p.printIdent(r.Name, identNormal, canDiscardWhitespaceAfter) } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" ") } - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print("{") } else { p.print("{\n") } indent++ for _, block := range r.Blocks { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.printIndent(indent) } for i, sel := range block.Selectors { if i > 0 { - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print(",") } else { p.print(", ") @@ -136,16 +136,16 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol } p.print(sel) } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" ") } p.printRuleBlock(block.Rules, indent) - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print("\n") } } indent-- - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.printIndent(indent) } p.print("}") @@ -157,11 +157,11 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol whitespace = canDiscardWhitespaceAfter } p.printIdent(r.AtToken, identNormal, whitespace) - if !p.options.RemoveWhitespace || len(r.Prelude) > 0 { + if !p.options.MinifyWhitespace || len(r.Prelude) > 0 { p.print(" ") } p.printTokens(r.Prelude, printTokensOpts{}) - if !p.options.RemoveWhitespace && len(r.Prelude) > 0 { + if !p.options.MinifyWhitespace && len(r.Prelude) > 0 { p.print(" ") } p.printRuleBlock(r.Rules, indent) @@ -173,11 +173,11 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol whitespace = canDiscardWhitespaceAfter } p.printIdent(r.AtToken, identNormal, whitespace) - if (!p.options.RemoveWhitespace && r.Block != nil) || len(r.Prelude) > 0 { + if (!p.options.MinifyWhitespace && r.Block != nil) || len(r.Prelude) > 0 { p.print(" ") } p.printTokens(r.Prelude, printTokensOpts{}) - if !p.options.RemoveWhitespace && r.Block != nil && len(r.Prelude) > 0 { + if !p.options.MinifyWhitespace && r.Block != nil && len(r.Prelude) > 0 { p.print(" ") } if r.Block == nil { @@ -191,14 +191,14 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol p.print("@nest") } p.printComplexSelectors(r.Selectors, indent, r.HasAtNest) - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" ") } p.printRuleBlock(r.Rules, indent) case *css_ast.RQualified: hasWhitespaceAfter := p.printTokens(r.Prelude, printTokensOpts{}) - if !hasWhitespaceAfter && !p.options.RemoveWhitespace { + if !hasWhitespaceAfter && !p.options.MinifyWhitespace { p.print(" ") } p.printRuleBlock(r.Rules, indent) @@ -211,7 +211,7 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol isDeclaration: true, }) if r.Important { - if !hasWhitespaceAfter && !p.options.RemoveWhitespace && len(r.Value) > 0 { + if !hasWhitespaceAfter && !p.options.MinifyWhitespace && len(r.Value) > 0 { p.print(" ") } p.print("!important") @@ -233,7 +233,7 @@ func (p *printer) printRule(rule css_ast.Rule, indent int32, omitTrailingSemicol panic("Internal error") } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print("\n") } } @@ -249,7 +249,7 @@ func (p *printer) printIndentedComment(indent int32, text string) { break } p.print(text[:newline+1]) - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.printIndent(indent) } text = text[newline+1:] @@ -258,18 +258,18 @@ func (p *printer) printIndentedComment(indent int32, text string) { } func (p *printer) printRuleBlock(rules []css_ast.Rule, indent int32) { - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print("{") } else { p.print("{\n") } for i, decl := range rules { - omitTrailingSemicolon := p.options.RemoveWhitespace && i+1 == len(rules) + omitTrailingSemicolon := p.options.MinifyWhitespace && i+1 == len(rules) p.printRule(decl, indent+1, omitTrailingSemicolon) } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.printIndent(indent) } p.print("}") @@ -278,7 +278,7 @@ func (p *printer) printRuleBlock(rules []css_ast.Rule, indent int32) { func (p *printer) printComplexSelectors(selectors []css_ast.ComplexSelector, indent int32, hasAtNest bool) { for i, complex := range selectors { if i > 0 { - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print(",") } else { p.print(",\n") @@ -305,11 +305,11 @@ func (p *printer) printCompoundSelector(sel css_ast.CompoundSelector, isFirst bo } if sel.Combinator != "" { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" ") } p.print(sel.Combinator) - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" ") } } @@ -645,7 +645,7 @@ func (p *printer) printTokens(tokens []css_ast.Token, opts printTokensOpts) bool // Pretty-print long comma-separated declarations of 3 or more items isMultiLineValue := false - if !p.options.RemoveWhitespace && opts.isDeclaration { + if !p.options.MinifyWhitespace && opts.isDeclaration { commaCount := 0 for _, t := range tokens { if t.Kind == css_lexer.TComma { diff --git a/internal/css_printer/css_printer_test.go b/internal/css_printer/css_printer_test.go index 4fff797e56e..b177395c779 100644 --- a/internal/css_printer/css_printer_test.go +++ b/internal/css_printer/css_printer_test.go @@ -21,7 +21,7 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st t.Helper() log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug) tree := css_parser.Parse(log, test.SourceForTest(contents), css_parser.Options{ - RemoveWhitespace: options.RemoveWhitespace, + MinifyWhitespace: options.MinifyWhitespace, }) msgs := log.Done() text := "" @@ -44,7 +44,7 @@ func expectPrinted(t *testing.T, contents string, expected string) { func expectPrintedMinify(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [minified]", contents, expected, Options{ - RemoveWhitespace: true, + MinifyWhitespace: true, }) } diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 0958411e6cd..f032e966d26 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -384,7 +384,7 @@ type optionsThatSupportStructuralEquality struct { targetFromAPI config.TargetFromAPI asciiOnly bool keepNames bool - mangleSyntax bool + minifySyntax bool minifyIdentifiers bool omitRuntimeForTests bool ignoreDCEAnnotations bool @@ -414,7 +414,7 @@ func OptionsFromConfig(options *config.Options) Options { targetFromAPI: options.TargetFromAPI, asciiOnly: options.ASCIIOnly, keepNames: options.KeepNames, - mangleSyntax: options.MangleSyntax, + minifySyntax: options.MinifySyntax, minifyIdentifiers: options.MinifyIdentifiers, omitRuntimeForTests: options.OmitRuntimeForTests, ignoreDCEAnnotations: options.IgnoreDCEAnnotations, @@ -1163,7 +1163,7 @@ func (p *parser) selectLocalKind(kind js_ast.LocalKind) js_ast.LocalKind { // Optimization: use "let" instead of "const" because it's shorter. This is // only done when bundling because assigning to "const" is only an error when // bundling. - if p.options.mode == config.ModeBundle && kind == js_ast.LocalConst && p.options.mangleSyntax { + if p.options.mode == config.ModeBundle && kind == js_ast.LocalConst && p.options.minifySyntax { return js_ast.LocalLet } @@ -1483,7 +1483,7 @@ func (p *parser) declareSymbol(kind js_ast.SymbolKind, loc logger.Loc, name stri symbol.Link = ref // If these are both functions, remove the overwritten declaration - if p.options.mangleSyntax && kind.IsFunction() && symbol.Kind.IsFunction() { + if p.options.minifySyntax && kind.IsFunction() && symbol.Kind.IsFunction() { symbol.Flags |= js_ast.RemoveOverwrittenFunctionDeclaration } @@ -1954,7 +1954,7 @@ func (p *parser) parseProperty(kind js_ast.PropertyKind, opts propertyOpts, erro case js_lexer.TStringLiteral: key = p.parseStringLiteral() - preferQuotedKey = !p.options.mangleSyntax + preferQuotedKey = !p.options.minifySyntax case js_lexer.TBigIntegerLiteral: key = js_ast.Expr{Loc: p.lexer.Loc(), Data: &js_ast.EBigInt{Value: p.lexer.Identifier}} @@ -2418,7 +2418,7 @@ func (p *parser) parsePropertyBinding() js_ast.PropertyBinding { case js_lexer.TStringLiteral: key = p.parseStringLiteral() - preferQuotedKey = !p.options.mangleSyntax + preferQuotedKey = !p.options.minifySyntax case js_lexer.TBigIntegerLiteral: key = js_ast.Expr{Loc: p.lexer.Loc(), Data: &js_ast.EBigInt{Value: p.lexer.Identifier}} @@ -3852,7 +3852,7 @@ func (p *parser) parseSuffix(left js_ast.Expr, level js_ast.L, errors *deferredE optionalStart := js_ast.OptionalChainStart // Remove unnecessary optional chains - if p.options.mangleSyntax { + if p.options.minifySyntax { if isNullOrUndefined, _, ok := toNullOrUndefinedWithSideEffects(left.Data); ok && !isNullOrUndefined { optionalStart = js_ast.OptionalChainNone } @@ -5728,7 +5728,7 @@ func (p *parser) parsePath() (logger.Loc, string, *[]ast.AssertEntry) { } else if p.lexer.Token == js_lexer.TStringLiteral { key = p.lexer.StringLiteral() keyText = js_lexer.UTF16ToString(key) - preferQuotedKey = !p.options.mangleSyntax + preferQuotedKey = !p.options.minifySyntax } else { p.lexer.Expect(js_lexer.TIdentifier) } @@ -7579,7 +7579,7 @@ func (p *parser) visitStmts(stmts []js_ast.Stmt, kind stmtsKind) []js_ast.Stmt { p.isControlFlowDead = oldIsControlFlowDead // Stop now if we're not mangling - if !p.options.mangleSyntax { + if !p.options.minifySyntax { return visited } @@ -9230,7 +9230,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ // Bad (a behavior change): // "a = 123; var a = undefined;" => "a = 123; var a;" // - if p.options.mangleSyntax && s.Kind == js_ast.LocalLet { + if p.options.minifySyntax && s.Kind == js_ast.LocalLet { if _, ok := d.Binding.Data.(*js_ast.BIdentifier); ok { if _, ok := d.ValueOrNil.Data.(*js_ast.EUndefined); ok { d.ValueOrNil = js_ast.Expr{} @@ -9279,7 +9279,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ case *js_ast.SExpr: shouldTrimUndefined := false - if !p.options.mangleSyntax { + if !p.options.minifySyntax { if _, ok := s.Value.Data.(*js_ast.ECall); ok { shouldTrimUndefined = true } @@ -9298,7 +9298,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ } // Trim expressions without side effects - if p.options.mangleSyntax { + if p.options.minifySyntax { s.Value = js_ast.SimplifyUnusedExpr(s.Value, p.isUnbound) if s.Value.Data == nil { return stmts @@ -9325,7 +9325,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ // Returning undefined is implicit except when inside an async generator // function, where "return undefined" behaves like "return await undefined" // but just "return" has no "await". - if p.options.mangleSyntax && (!p.fnOrArrowDataVisit.isAsync || !p.fnOrArrowDataVisit.isGenerator) { + if p.options.minifySyntax && (!p.fnOrArrowDataVisit.isAsync || !p.fnOrArrowDataVisit.isGenerator) { if _, ok := s.ValueOrNil.Data.(*js_ast.EUndefined); ok { s.ValueOrNil = js_ast.Expr{} } @@ -9346,7 +9346,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ p.popScope() - if p.options.mangleSyntax { + if p.options.minifySyntax { if len(s.Stmts) == 1 && !statementCaresAboutScope(s.Stmts[0]) { // Unwrap blocks containing a single statement stmt = s.Stmts[0] @@ -9367,7 +9367,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ s.Test = p.visitExpr(s.Test) s.Body = p.visitLoopBody(s.Body) - if p.options.mangleSyntax { + if p.options.minifySyntax { s.Test = p.simplifyBooleanExpr(s.Test) // A true value is implied @@ -9386,14 +9386,14 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ s.Body = p.visitLoopBody(s.Body) s.Test = p.visitExpr(s.Test) - if p.options.mangleSyntax { + if p.options.minifySyntax { s.Test = p.simplifyBooleanExpr(s.Test) } case *js_ast.SIf: s.Test = p.visitExpr(s.Test) - if p.options.mangleSyntax { + if p.options.minifySyntax { s.Test = p.simplifyBooleanExpr(s.Test) } @@ -9423,14 +9423,14 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ } // Trim unnecessary "else" clauses - if p.options.mangleSyntax { + if p.options.minifySyntax { if _, ok := s.NoOrNil.Data.(*js_ast.SEmpty); ok { s.NoOrNil = js_ast.Stmt{} } } } - if p.options.mangleSyntax { + if p.options.minifySyntax { return p.mangleIf(stmts, stmt.Loc, s) } @@ -9443,7 +9443,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ if s.TestOrNil.Data != nil { s.TestOrNil = p.visitExpr(s.TestOrNil) - if p.options.mangleSyntax { + if p.options.minifySyntax { s.TestOrNil = p.simplifyBooleanExpr(s.TestOrNil) // A true value is implied @@ -9474,7 +9474,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ p.popScope() - if p.options.mangleSyntax { + if p.options.minifySyntax { mangleFor(s) } @@ -9591,7 +9591,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ return stmts } - if p.options.mangleSyntax && !s.Fn.IsGenerator && !s.Fn.IsAsync && !s.Fn.HasRestArg && s.Fn.Name != nil { + if p.options.minifySyntax && !s.Fn.IsGenerator && !s.Fn.IsAsync && !s.Fn.HasRestArg && s.Fn.Name != nil { if len(s.Fn.Body.Stmts) == 0 { // Mark if this function is an empty function hasSideEffectFreeArguments := true @@ -9755,7 +9755,7 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ value.ValueOrNil = js_ast.Expr{Loc: value.Loc, Data: js_ast.EUndefinedShared} } - if p.options.mangleSyntax && js_lexer.IsIdentifier(name) { + if p.options.minifySyntax && js_lexer.IsIdentifier(name) { // "Enum.Name = value" assignTarget = js_ast.Assign( js_ast.Expr{Loc: value.Loc, Data: &js_ast.EDot{ @@ -10226,7 +10226,7 @@ func (p *parser) visitClass(nameScopeLoc logger.Loc, class *js_ast.Class) js_ast p.fnOnlyDataVisit = oldFnOnlyDataVisit // "class { static {} }" => "class {}" - if p.options.mangleSyntax && len(property.ClassStaticBlock.Stmts) == 0 { + if p.options.minifySyntax && len(property.ClassStaticBlock.Stmts) == 0 { continue } @@ -10251,7 +10251,7 @@ func (p *parser) visitClass(nameScopeLoc logger.Loc, class *js_ast.Class) js_ast property.Key = key // "class {['x'] = y}" => "class {x = y}" - if p.options.mangleSyntax && property.IsComputed { + if p.options.minifySyntax && property.IsComputed { if str, ok := key.Data.(*js_ast.EString); ok && js_lexer.IsIdentifierUTF16(str.Value) { isInvalidConstructor := false if js_lexer.UTF16EqualsString(str.Value, "constructor") { @@ -10757,7 +10757,7 @@ func (p *parser) maybeRewritePropertyAccess( } // Attempt to simplify statically-determined object literal property accesses - if !isCallTarget && p.options.mangleSyntax && assignTarget == js_ast.AssignTargetNone { + if !isCallTarget && p.options.minifySyntax && assignTarget == js_ast.AssignTargetNone { if object, ok := target.Data.(*js_ast.EObject); ok { var replace js_ast.Expr hasProtoNull := false @@ -11594,7 +11594,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO // When mangling, inline string values into the template literal. Note that // it may no longer be a template literal after this point (it may turn into // a plain string literal instead). - if p.options.mangleSyntax { + if p.options.minifySyntax { expr = p.mangleTemplate(expr.Loc, e) } @@ -11704,7 +11704,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO case js_ast.BinOpComma: // "(1, 2)" => "2" // "(sideEffects(), 2)" => "(sideEffects(), 2)" - if p.options.mangleSyntax { + if p.options.minifySyntax { e.Left = js_ast.SimplifyUnusedExpr(e.Left, p.isUnbound) if e.Left.Data == nil { // "(1, fn)()" => "fn()" @@ -11727,7 +11727,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } p.warnAboutTypeofAndString(e.Left, e.Right) - if p.options.mangleSyntax { + if p.options.minifySyntax { // "x == void 0" => "x == null" if _, ok := e.Right.Data.(*js_ast.EUndefined); ok { e.Right.Data = js_ast.ENullShared @@ -11748,7 +11748,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } p.warnAboutTypeofAndString(e.Left, e.Right) - if p.options.mangleSyntax { + if p.options.minifySyntax { // "typeof x === 'undefined'" => "typeof x == 'undefined'" if canChangeStrictToLoose(e.Left, e.Right) { e.Op = js_ast.BinOpLooseEq @@ -11769,7 +11769,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } p.warnAboutTypeofAndString(e.Left, e.Right) - if p.options.mangleSyntax { + if p.options.minifySyntax { // "x != void 0" => "x != null" if _, ok := e.Right.Data.(*js_ast.EUndefined); ok { e.Right.Data = js_ast.ENullShared @@ -11790,7 +11790,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } p.warnAboutTypeofAndString(e.Left, e.Right) - if p.options.mangleSyntax { + if p.options.minifySyntax { // "typeof x !== 'undefined'" => "typeof x != 'undefined'" if canChangeStrictToLoose(e.Left, e.Right) { e.Op = js_ast.BinOpLooseNe @@ -11817,7 +11817,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } } - if p.options.mangleSyntax { + if p.options.minifySyntax { // "a ?? (b ?? c)" => "a ?? b ?? c" if right, ok := e.Right.Data.(*js_ast.EBinary); ok && right.Op == js_ast.BinOpNullishCoalescing { e.Left = js_ast.JoinWithLeftAssociativeOp(js_ast.BinOpNullishCoalescing, e.Left, right.Left) @@ -11844,7 +11844,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } } - if p.options.mangleSyntax { + if p.options.minifySyntax { // "a || (b || c)" => "a || b || c" if right, ok := e.Right.Data.(*js_ast.EBinary); ok && right.Op == js_ast.BinOpLogicalOr { e.Left = js_ast.JoinWithLeftAssociativeOp(js_ast.BinOpLogicalOr, e.Left, right.Left) @@ -11874,7 +11874,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } } - if p.options.mangleSyntax { + if p.options.minifySyntax { // "a && (b && c)" => "a && b && c" if right, ok := e.Right.Data.(*js_ast.EBinary); ok && right.Op == js_ast.BinOpLogicalAnd { e.Left = js_ast.JoinWithLeftAssociativeOp(js_ast.BinOpLogicalAnd, e.Left, right.Left) @@ -12157,7 +12157,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } // "(a, b) + c" => "a, b + c" - if p.options.mangleSyntax && e.Op != js_ast.BinOpComma { + if p.options.minifySyntax && e.Op != js_ast.BinOpComma { if comma, ok := e.Left.Data.(*js_ast.EBinary); ok && comma.Op == js_ast.BinOpComma { return js_ast.JoinWithComma(comma.Left, js_ast.Expr{ Loc: comma.Right.Loc, @@ -12176,7 +12176,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO isDeleteTarget := e == p.deleteTarget // "a['b']" => "a.b" - if p.options.mangleSyntax { + if p.options.minifySyntax { if str, ok := e.Index.Data.(*js_ast.EString); ok && js_lexer.IsIdentifierUTF16(str.Value) { dot := &js_ast.EDot{ Target: e.Target, @@ -12274,7 +12274,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO out.thisArgWrapFunc = nil } if str, ok := e.Index.Data.(*js_ast.EString); ok && e.OptionalChain == js_ast.OptionalChainNone { - preferQuotedKey := !p.options.mangleSyntax + preferQuotedKey := !p.options.minifySyntax if value, ok := p.maybeRewritePropertyAccess(expr.Loc, in.assignTarget, isDeleteTarget, e.Target, js_lexer.UTF16ToString(str.Value), e.Index.Loc, isCallTarget, preferQuotedKey); ok { return value, out @@ -12379,7 +12379,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO // Post-process the unary expression switch e.Op { case js_ast.UnOpNot: - if p.options.mangleSyntax { + if p.options.minifySyntax { e.Value = p.simplifyBooleanExpr(e.Value) } @@ -12387,7 +12387,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.EBoolean{Value: !boolean}}, exprOut{} } - if p.options.mangleSyntax { + if p.options.minifySyntax { if result, ok := js_ast.MaybeSimplifyNot(e.Value); ok { return result, exprOut{} } @@ -12429,7 +12429,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } // "-(a, b)" => "a, -b" - if p.options.mangleSyntax && e.Op != js_ast.UnOpDelete && e.Op != js_ast.UnOpTypeof { + if p.options.minifySyntax && e.Op != js_ast.UnOpDelete && e.Op != js_ast.UnOpTypeof { if comma, ok := e.Value.Data.(*js_ast.EBinary); ok && comma.Op == js_ast.BinOpComma { return js_ast.JoinWithComma(comma.Left, js_ast.Expr{ Loc: comma.Right.Loc, @@ -12527,7 +12527,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO isCallTarget := e == p.callTarget e.Test = p.visitExpr(e.Test) - if p.options.mangleSyntax { + if p.options.minifySyntax { e.Test = p.simplifyBooleanExpr(e.Test) } @@ -12545,7 +12545,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO e.No = p.visitExpr(e.No) p.isControlFlowDead = old - if p.options.mangleSyntax { + if p.options.minifySyntax { // "(a, true) ? b : c" => "a, b" if sideEffects == couldHaveSideEffects { return js_ast.JoinWithComma(js_ast.SimplifyUnusedExpr(e.Test, p.isUnbound), e.Yes), exprOut{} @@ -12568,7 +12568,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO p.isControlFlowDead = old e.No = p.visitExpr(e.No) - if p.options.mangleSyntax { + if p.options.minifySyntax { // "(a, false) ? b : c" => "a, c" if sideEffects == couldHaveSideEffects { return js_ast.JoinWithComma(js_ast.SimplifyUnusedExpr(e.Test, p.isUnbound), e.No), exprOut{} @@ -12586,7 +12586,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } } - if p.options.mangleSyntax { + if p.options.minifySyntax { return p.mangleIfExpr(expr.Loc, e), exprOut{} } @@ -12639,7 +12639,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } // "[1, ...[2, 3], 4]" => "[1, 2, 3, 4]" - if p.options.mangleSyntax && hasSpread && in.assignTarget == js_ast.AssignTargetNone { + if p.options.minifySyntax && hasSpread && in.assignTarget == js_ast.AssignTargetNone { e.Items = inlineSpreadsOfArrayLiterals(e.Items) } @@ -12679,7 +12679,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } // "{['x']: y}" => "{x: y}" - if p.options.mangleSyntax && property.IsComputed { + if p.options.minifySyntax && property.IsComputed { if str, ok := key.Data.(*js_ast.EString); ok && js_lexer.IsIdentifierUTF16(str.Value) && !js_lexer.UTF16EqualsString(str.Value, "__proto__") { property.IsComputed = false } @@ -12757,7 +12757,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO if in.assignTarget == js_ast.AssignTargetNone { // "{a, ...{b, c}, d}" => "{a, b, c, d}" - if p.options.mangleSyntax && hasSpread { + if p.options.minifySyntax && hasSpread { var properties []js_ast.Property for _, property := range e.Properties { if property.Kind == js_ast.PropertySpread { @@ -13115,7 +13115,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } // "foo(1, ...[2, 3], 4)" => "foo(1, 2, 3, 4)" - if p.options.mangleSyntax && hasSpread && in.assignTarget == js_ast.AssignTargetNone { + if p.options.minifySyntax && hasSpread && in.assignTarget == js_ast.AssignTargetNone { e.Args = inlineSpreadsOfArrayLiterals(e.Args) } @@ -13162,7 +13162,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO switch t := target.Data.(type) { case *js_ast.EImportIdentifier: // If this function is inlined, allow it to be tree-shaken - if p.options.mangleSyntax && !p.isControlFlowDead { + if p.options.minifySyntax && !p.isControlFlowDead { p.convertSymbolUseToCall(t.Ref, len(e.Args) == 1) } @@ -13173,7 +13173,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } // If this function is inlined, allow it to be tree-shaken - if p.options.mangleSyntax && !p.isControlFlowDead { + if p.options.minifySyntax && !p.isControlFlowDead { p.convertSymbolUseToCall(t.Ref, len(e.Args) == 1) } @@ -13348,7 +13348,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO p.fnOnlyDataVisit.superHelpers = nil } - if p.options.mangleSyntax && len(e.Body.Stmts) == 1 { + if p.options.minifySyntax && len(e.Body.Stmts) == 1 { if s, ok := e.Body.Stmts[0].Data.(*js_ast.SReturn); ok { if s.ValueOrNil.Data == nil { // "() => { return }" => "() => {}" @@ -13379,7 +13379,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO name := e.Fn.Name // Remove unused function names when minifying - if p.options.mangleSyntax && !p.currentScope.ContainsDirectEval && + if p.options.minifySyntax && !p.currentScope.ContainsDirectEval && name != nil && p.symbols[name.Ref.InnerIndex].UseCountEstimate == 0 { e.Fn.Name = nil } @@ -13931,7 +13931,7 @@ func (p *parser) scanForImportsAndExports(stmts []js_ast.Stmt) (result importsEx // TypeScript always trims unused imports. This is important for // correctness since some imports might be fake (only in the type // system and used for type-only imports). - if (p.options.mangleSyntax || p.options.ts.Parse) && !keepUnusedImports { + if (p.options.minifySyntax || p.options.ts.Parse) && !keepUnusedImports { foundImports := false isUnusedInTypeScript := true diff --git a/internal/js_parser/js_parser_lower.go b/internal/js_parser/js_parser_lower.go index f043b9ddc81..9bf8a40ec83 100644 --- a/internal/js_parser/js_parser_lower.go +++ b/internal/js_parser/js_parser_lower.go @@ -582,7 +582,7 @@ flatten: // Stop now if we can strip the whole chain as dead code. Since the chain is // lazily evaluated, it's safe to just drop the code entirely. - if p.options.mangleSyntax { + if p.options.minifySyntax { if isNullOrUndefined, sideEffects, ok := toNullOrUndefinedWithSideEffects(expr.Data); ok && isNullOrUndefined { if sideEffects == couldHaveSideEffects { return js_ast.JoinWithComma(js_ast.SimplifyUnusedExpr(expr, p.isUnbound), valueWhenUndefined), exprOut{} @@ -2018,7 +2018,7 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast // Remove unused class names when minifying. Check this after we merge in // the shadowing name above since that will adjust the use count. - if p.options.mangleSyntax && symbol.UseCountEstimate == 0 { + if p.options.minifySyntax && symbol.UseCountEstimate == 0 { class.Name = nil } } diff --git a/internal/js_parser/js_parser_test.go b/internal/js_parser/js_parser_test.go index 49d0c9c7a0b..d09038169b7 100644 --- a/internal/js_parser/js_parser_test.go +++ b/internal/js_parser/js_parser_test.go @@ -81,7 +81,7 @@ func expectPrinted(t *testing.T, contents string, expected string) { func expectPrintedMangle(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents, expected, config.Options{ - MangleSyntax: true, + MinifySyntax: true, }) } @@ -100,7 +100,7 @@ func expectPrintedMangleTarget(t *testing.T, esVersion int, contents string, exp UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine][]int{ compat.ES: {esVersion}, }), - MangleSyntax: true, + MinifySyntax: true, }) } diff --git a/internal/js_parser/json_parser_test.go b/internal/js_parser/json_parser_test.go index 577e2200ba2..a8fba538f21 100644 --- a/internal/js_parser/json_parser_test.go +++ b/internal/js_parser/json_parser_test.go @@ -55,7 +55,7 @@ func expectPrintedJSONWithWarning(t *testing.T, contents string, warning string, } js := js_printer.Print(tree, js_ast.SymbolMap{}, nil, js_printer.Options{ - RemoveWhitespace: true, + MinifyWhitespace: true, }).JS // Remove the trailing semicolon diff --git a/internal/js_parser/ts_parser.go b/internal/js_parser/ts_parser.go index 4fdee68f4ed..616cc599a69 100644 --- a/internal/js_parser/ts_parser.go +++ b/internal/js_parser/ts_parser.go @@ -1412,7 +1412,7 @@ func (p *parser) generateClosureForTypeScriptNamespaceOrEnum( } var argExpr js_ast.Expr - if p.options.mangleSyntax && !p.options.unsupportedJSFeatures.Has(compat.LogicalAssignment) { + if p.options.minifySyntax && !p.options.unsupportedJSFeatures.Has(compat.LogicalAssignment) { // If the "||=" operator is supported, our minified output can be slightly smaller if isExport && p.enclosingNamespaceArgRef != nil { // "name = (enclosing.name ||= {})" @@ -1490,7 +1490,7 @@ func (p *parser) generateClosureForTypeScriptNamespaceOrEnum( }}} } else { // "(() => { foo() })()" => "(() => foo())()" - if p.options.mangleSyntax && len(stmtsInsideClosure) == 1 { + if p.options.minifySyntax && len(stmtsInsideClosure) == 1 { if expr, ok := stmtsInsideClosure[0].Data.(*js_ast.SExpr); ok { stmtsInsideClosure[0].Data = &js_ast.SReturn{ValueOrNil: expr.Value} } @@ -1524,7 +1524,7 @@ func (p *parser) generateClosureForTypeScriptEnum( if p.currentScope != p.moduleScope { stmtsInsideClosure := []js_ast.Stmt{} if len(exprsInsideClosure) > 0 { - if p.options.mangleSyntax { + if p.options.minifySyntax { // "a; b; c;" => "a, b, c;" joined := js_ast.JoinAllWithComma(exprsInsideClosure) stmtsInsideClosure = append(stmtsInsideClosure, js_ast.Stmt{Loc: joined.Loc, Data: &js_ast.SExpr{Value: joined}}) @@ -1579,7 +1579,7 @@ func (p *parser) generateClosureForTypeScriptEnum( stmtsInsideClosure := []js_ast.Stmt{} if len(exprsInsideClosure) > 0 { argExpr := js_ast.Expr{Loc: nameLoc, Data: &js_ast.EIdentifier{Ref: argRef}} - if p.options.mangleSyntax { + if p.options.minifySyntax { // "a; b; return c;" => "return a, b, c;" joined := js_ast.JoinAllWithComma(exprsInsideClosure) joined = js_ast.JoinWithComma(joined, argExpr) @@ -1604,7 +1604,7 @@ func (p *parser) generateClosureForTypeScriptEnum( targetExpr = js_ast.Expr{Loc: stmtLoc, Data: &js_ast.EArrow{ Args: args, Body: js_ast.FnBody{Loc: stmtLoc, Stmts: stmtsInsideClosure}, - PreferExpr: p.options.mangleSyntax, + PreferExpr: p.options.minifySyntax, }} } @@ -1635,7 +1635,7 @@ func (p *parser) generateClosureForTypeScriptEnum( } func (p *parser) wrapInlinedEnum(value js_ast.Expr, comment string) js_ast.Expr { - if p.shouldFoldNumericConstants || p.options.mangleSyntax || strings.Contains(comment, "*/") { + if p.shouldFoldNumericConstants || p.options.minifySyntax || strings.Contains(comment, "*/") { // Don't wrap with a comment return value } diff --git a/internal/js_printer/js_printer.go b/internal/js_printer/js_printer.go index 4361751e80a..abc970b3697 100644 --- a/internal/js_printer/js_printer.go +++ b/internal/js_printer/js_printer.go @@ -477,7 +477,7 @@ func (p *printer) addSourceMapping(loc logger.Loc) { } func (p *printer) printIndent() { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { for i := 0; i < p.options.Indent; i++ { p.print(" ") } @@ -580,7 +580,7 @@ func (p *printer) printNumber(value float64, level js_ast.L) { p.printSpaceBeforeIdentifier() p.print("NaN") } else if value == positiveInfinity || value == negativeInfinity { - wrap := (p.options.MangleSyntax && level >= js_ast.LMultiply) || + wrap := (p.options.MinifySyntax && level >= js_ast.LMultiply) || (value == negativeInfinity && level >= js_ast.LPrefix) if wrap { p.print("(") @@ -591,9 +591,9 @@ func (p *printer) printNumber(value float64, level js_ast.L) { } else { p.printSpaceBeforeIdentifier() } - if !p.options.MangleSyntax { + if !p.options.MinifySyntax { p.print("Infinity") - } else if p.options.RemoveWhitespace { + } else if p.options.MinifyWhitespace { p.print("1/0") } else { p.print("1 / 0") @@ -782,13 +782,13 @@ func (p *printer) printBinding(binding js_ast.Binding) { } func (p *printer) printSpace() { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" ") } } func (p *printer) printNewline() { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print("\n") } } @@ -814,7 +814,7 @@ func (p *printer) printSpaceBeforeOperator(next js_ast.OpCode) { } func (p *printer) printSemicolonAfterStatement() { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(";\n") } else { p.needsSemicolon = true @@ -840,7 +840,7 @@ func (p *printer) printFnArgs(args []js_ast.Arg, hasRestArg bool, isArrow bool) wrap := true // Minify "(a) => {}" as "a=>{}" - if p.options.RemoveWhitespace && !hasRestArg && isArrow && len(args) == 1 { + if p.options.MinifyWhitespace && !hasRestArg && isArrow && len(args) == 1 { if _, ok := args[0].Binding.Data.(*js_ast.BIdentifier); ok && args[0].DefaultOrNil.Data == nil { wrap = false } @@ -1105,7 +1105,7 @@ func (p *printer) printQuotedUTF16(data []uint16, allowBacktick bool) { for i, c := range data { switch c { case '\n': - if p.options.MangleSyntax { + if p.options.MinifySyntax { // The backslash for the newline costs an extra character for old-style // string literals when compared to a template literal backtickCost-- @@ -1339,7 +1339,7 @@ func (p *printer) printDotThenPrefix() js_ast.L { func (p *printer) printDotThenSuffix() { if p.options.UnsupportedFeatures.Has(compat.Arrow) { - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(";") } p.printNewline() @@ -1568,7 +1568,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla p.print(">") isSingleLine := true - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { isSingleLine = len(e.Children) < 2 if len(e.Children) == 1 { if _, ok := e.Children[0].Data.(*js_ast.EJSXElement); !ok { @@ -1612,7 +1612,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla case *js_ast.ENew: wrap := level >= js_ast.LCall - hasPureComment := !p.options.RemoveWhitespace && e.CanBeUnwrappedIfUnused + hasPureComment := !p.options.MinifyWhitespace && e.CanBeUnwrappedIfUnused if hasPureComment && level >= js_ast.LPostfix { wrap = true } @@ -1631,7 +1631,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla p.printExpr(e.Target, js_ast.LNew, forbidCall) // Omit the "()" when minifying, but only when safe to do so - if !p.options.RemoveWhitespace || len(e.Args) > 0 || level >= js_ast.LPostfix { + if !p.options.MinifyWhitespace || len(e.Args) > 0 || level >= js_ast.LPostfix { p.print("(") for i, arg := range e.Args { if i != 0 { @@ -1648,7 +1648,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla } case *js_ast.ECall: - if p.options.MangleSyntax { + if p.options.MinifySyntax { var symbolFlags js_ast.SymbolFlags switch target := e.Target.Data.(type) { case *js_ast.EIdentifier: @@ -1690,7 +1690,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla wrap = true } - hasPureComment := !p.options.RemoveWhitespace && e.CanBeUnwrappedIfUnused + hasPureComment := !p.options.MinifyWhitespace && e.CanBeUnwrappedIfUnused if hasPureComment && level >= js_ast.LPostfix { wrap = true } @@ -1710,7 +1710,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla // We don't ever want to accidentally generate a direct eval expression here p.callTarget = e.Target.Data if !e.IsDirectEval && p.isUnboundEvalIdentifier(e.Target) { - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print("(0,") } else { p.print("(0, ") @@ -1755,14 +1755,14 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla case *js_ast.EImportString: var leadingInteriorComments []js_ast.Comment - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { leadingInteriorComments = e.LeadingInteriorComments } p.printRequireOrImportExpr(e.ImportRecordIndex, leadingInteriorComments, level, flags) case *js_ast.EImportCall: var leadingInteriorComments []js_ast.Comment - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { leadingInteriorComments = e.LeadingInteriorComments } wrap := level >= js_ast.LNew || (flags&forbidCall) != 0 @@ -1811,7 +1811,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla } else { p.printNumber(value.Number, level) } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" /* ") p.print(e.Name) p.print(" */") @@ -1872,7 +1872,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla } else { p.printNumber(value.Number, level) } - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" /* ") p.print(name) p.print(" */") @@ -2088,7 +2088,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla } case *js_ast.EBoolean: - if p.options.MangleSyntax { + if p.options.MinifySyntax { if level >= js_ast.LPrefix { if e.Value { p.print("(!0)") @@ -2113,7 +2113,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla case *js_ast.EString: // If this was originally a template literal, print it as one as long as we're not minifying - if e.PreferTemplate && !p.options.MangleSyntax && !p.options.UnsupportedFeatures.Has(compat.TemplateLiteral) { + if e.PreferTemplate && !p.options.MinifySyntax && !p.options.UnsupportedFeatures.Has(compat.TemplateLiteral) { p.print("`") p.printUnquotedUTF16(e.Value, '`') p.print("`") @@ -2124,7 +2124,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla case *js_ast.ETemplate: // Convert no-substitution template literals into strings if it's smaller - if p.options.MangleSyntax && e.TagOrNil.Data == nil && len(e.Parts) == 0 { + if p.options.MinifySyntax && e.TagOrNil.Data == nil && len(e.Parts) == 0 { p.printQuotedUTF16(e.HeadCooked, true /* allowBacktick */) return } @@ -2175,7 +2175,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla case *js_ast.EInlinedEnum: p.printExpr(e.Value, level, flags) - if !p.options.RemoveWhitespace { + if !p.options.MinifyWhitespace { p.print(" /* ") p.print(e.Comment) p.print(" */") @@ -2215,7 +2215,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla } else if symbol.NamespaceAlias != nil { wrap := p.callTarget == e && e.WasOriginallyIdentifier if wrap { - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { p.print("(0,") } else { p.print("(0, ") @@ -2380,7 +2380,7 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla } else if _, ok := e.Left.Data.(*js_ast.ENumber); ok { // Negative numbers are printed using a unary operator leftLevel = js_ast.LCall - } else if p.options.MangleSyntax { + } else if p.options.MinifySyntax { // When minifying, booleans are printed as "!0 and "!1" if _, ok := e.Left.Data.(*js_ast.EBoolean); ok { leftLevel = js_ast.LCall @@ -2539,7 +2539,7 @@ func (p *printer) printNonNegativeFloat(absValue float64) { // Strip off the leading zero when minifying // "0.5" => ".5" - if p.options.RemoveWhitespace { + if p.options.MinifyWhitespace { result = result[1:] afterDot-- } @@ -3234,7 +3234,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) { update := s.UpdateOrNil // Omit calls to empty functions from the output completely - if p.options.MangleSyntax { + if p.options.MinifySyntax { if expr, ok := init.Data.(*js_ast.SExpr); ok { if value := p.simplifyUnusedExpr(expr.Value); value.Data == nil { init.Data = nil @@ -3458,7 +3458,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) { value := s.Value // Omit calls to empty functions from the output completely - if p.options.MangleSyntax { + if p.options.MinifySyntax { value = p.simplifyUnusedExpr(value) if value.Data == nil { // If this statement is not in a block, then we still need to emit something @@ -3504,8 +3504,8 @@ type Options struct { UnsupportedFeatures compat.JSFeature Indent int OutputFormat config.Format - RemoveWhitespace bool - MangleSyntax bool + MinifyWhitespace bool + MinifySyntax bool ASCIIOnly bool LegalComments config.LegalComments AddSourceMappings bool diff --git a/internal/js_printer/js_printer_test.go b/internal/js_printer/js_printer_test.go index 740b246fdbf..f2032cae504 100644 --- a/internal/js_printer/js_printer_test.go +++ b/internal/js_printer/js_printer_test.go @@ -39,8 +39,8 @@ func expectPrintedCommon(t *testing.T, name string, contents string, expected st r := renamer.NewNoOpRenamer(symbols) js := Print(tree, symbols, r, Options{ ASCIIOnly: options.ASCIIOnly, - MangleSyntax: options.MangleSyntax, - RemoveWhitespace: options.RemoveWhitespace, + MinifySyntax: options.MinifySyntax, + MinifyWhitespace: options.MinifyWhitespace, UnsupportedFeatures: options.UnsupportedJSFeatures, }).JS test.AssertEqualWithDiff(t, string(js), expected) @@ -55,22 +55,22 @@ func expectPrinted(t *testing.T, contents string, expected string) { func expectPrintedMinify(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [minified]", contents, expected, config.Options{ - RemoveWhitespace: true, + MinifyWhitespace: true, }) } func expectPrintedMangle(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [mangled]", contents, expected, config.Options{ - MangleSyntax: true, + MinifySyntax: true, }) } func expectPrintedMangleMinify(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [mangled, minified]", contents, expected, config.Options{ - MangleSyntax: true, - RemoveWhitespace: true, + MinifySyntax: true, + MinifyWhitespace: true, }) } @@ -84,7 +84,7 @@ func expectPrintedASCII(t *testing.T, contents string, expected string) { func expectPrintedMinifyASCII(t *testing.T, contents string, expected string) { t.Helper() expectPrintedCommon(t, contents+" [ascii]", contents, expected, config.Options{ - RemoveWhitespace: true, + MinifyWhitespace: true, ASCIIOnly: true, }) } @@ -104,7 +104,7 @@ func expectPrintedTargetMinify(t *testing.T, esVersion int, contents string, exp UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine][]int{ compat.ES: {esVersion}, }), - RemoveWhitespace: true, + MinifyWhitespace: true, }) } @@ -114,7 +114,7 @@ func expectPrintedTargetMangle(t *testing.T, esVersion int, contents string, exp UnsupportedJSFeatures: compat.UnsupportedJSFeatures(map[compat.Engine][]int{ compat.ES: {esVersion}, }), - MangleSyntax: true, + MinifySyntax: true, }) } @@ -156,7 +156,7 @@ func expectPrintedJSXMinify(t *testing.T, contents string, expected string) { Parse: true, Preserve: true, }, - RemoveWhitespace: true, + MinifyWhitespace: true, }) } diff --git a/pkg/api/api_impl.go b/pkg/api/api_impl.go index 78a88df2d6e..c1c44759264 100644 --- a/pkg/api/api_impl.go +++ b/pkg/api/api_impl.go @@ -892,8 +892,8 @@ func rebuildImpl( LegalComments: validateLegalComments(buildOpts.LegalComments, buildOpts.Bundle), SourceRoot: buildOpts.SourceRoot, ExcludeSourcesContent: buildOpts.SourcesContent == SourcesContentExclude, - MangleSyntax: buildOpts.MinifySyntax, - RemoveWhitespace: buildOpts.MinifyWhitespace, + MinifySyntax: buildOpts.MinifySyntax, + MinifyWhitespace: buildOpts.MinifyWhitespace, MinifyIdentifiers: buildOpts.MinifyIdentifiers, MangleProps: validateRegex(log, "mangle props", buildOpts.MangleProps), ReserveProps: validateRegex(log, "reserve props", buildOpts.ReserveProps), @@ -1386,8 +1386,8 @@ func transformImpl(input string, transformOpts TransformOptions) TransformResult ExcludeSourcesContent: transformOpts.SourcesContent == SourcesContentExclude, OutputFormat: validateFormat(transformOpts.Format), GlobalName: validateGlobalName(log, transformOpts.GlobalName), - MangleSyntax: transformOpts.MinifySyntax, - RemoveWhitespace: transformOpts.MinifyWhitespace, + MinifySyntax: transformOpts.MinifySyntax, + MinifyWhitespace: transformOpts.MinifyWhitespace, MinifyIdentifiers: transformOpts.MinifyIdentifiers, MangleProps: validateRegex(log, "mangle props", transformOpts.MangleProps), ReserveProps: validateRegex(log, "reserve props", transformOpts.ReserveProps),