From 4cc999945beb6e494207fd55b7a43f933a378b6b Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Mon, 19 Dec 2022 15:57:10 -0500 Subject: [PATCH] option to not escape `` and `` --- .../bundler_tests/bundler_default_test.go | 78 +++++++++++++++++ .../snapshots/snapshots_default.txt | 84 +++++++++++++++++++ internal/helpers/comment.go | 3 + internal/linker/linker.go | 12 ++- 4 files changed, 175 insertions(+), 2 deletions(-) diff --git a/internal/bundler_tests/bundler_default_test.go b/internal/bundler_tests/bundler_default_test.go index 713cc4be1c1..e51ad7d3012 100644 --- a/internal/bundler_tests/bundler_default_test.go +++ b/internal/bundler_tests/bundler_default_test.go @@ -3549,6 +3549,84 @@ func TestLegalCommentsManyEndOfFile(t *testing.T) { }) } +func TestLegalCommentsEscapeSlashScriptAndStyleEndOfFile(t *testing.T) { + default_suite.expectBundled(t, bundled{ + files: map[string]string{ + "/project/entry.js": `import "js-pkg"; a /*! */`, + "/project/node_modules/js-pkg/index.js": `x /*! */`, + + "/project/entry.css": `@import "css-pkg"; a { b: c } /*! */`, + "/project/node_modules/css-pkg/index.css": `x { y: z } /*! */`, + }, + entryPaths: []string{"/project/entry.js", "/project/entry.css"}, + options: config.Options{ + Mode: config.ModeBundle, + AbsOutputDir: "/out", + MinifyWhitespace: true, + LegalComments: config.LegalCommentsEndOfFile, + }, + }) +} + +func TestLegalCommentsEscapeSlashScriptAndStyleExternal(t *testing.T) { + default_suite.expectBundled(t, bundled{ + files: map[string]string{ + "/project/entry.js": `import "js-pkg"; a /*! */`, + "/project/node_modules/js-pkg/index.js": `x /*! */`, + + "/project/entry.css": `@import "css-pkg"; a { b: c } /*! */`, + "/project/node_modules/css-pkg/index.css": `x { y: z } /*! */`, + }, + entryPaths: []string{"/project/entry.js", "/project/entry.css"}, + options: config.Options{ + Mode: config.ModeBundle, + AbsOutputDir: "/out", + MinifyWhitespace: true, + LegalComments: config.LegalCommentsExternalWithoutComment, + }, + }) +} + +func TestLegalCommentsNoEscapeSlashScriptEndOfFile(t *testing.T) { + default_suite.expectBundled(t, bundled{ + files: map[string]string{ + "/project/entry.js": `import "js-pkg"; a /*! */`, + "/project/node_modules/js-pkg/index.js": `x /*! */`, + + "/project/entry.css": `@import "css-pkg"; a { b: c } /*! */`, + "/project/node_modules/css-pkg/index.css": `x { y: z } /*! */`, + }, + entryPaths: []string{"/project/entry.js", "/project/entry.css"}, + options: config.Options{ + Mode: config.ModeBundle, + AbsOutputDir: "/out", + MinifyWhitespace: true, + LegalComments: config.LegalCommentsEndOfFile, + UnsupportedJSFeatures: compat.InlineScript, + }, + }) +} + +func TestLegalCommentsNoEscapeSlashStyleEndOfFile(t *testing.T) { + default_suite.expectBundled(t, bundled{ + files: map[string]string{ + "/project/entry.js": `import "js-pkg"; a /*! */`, + "/project/node_modules/js-pkg/index.js": `x /*! */`, + + "/project/entry.css": `@import "css-pkg"; a { b: c } /*! */`, + "/project/node_modules/css-pkg/index.css": `x { y: z } /*! */`, + }, + entryPaths: []string{"/project/entry.js", "/project/entry.css"}, + options: config.Options{ + Mode: config.ModeBundle, + AbsOutputDir: "/out", + MinifyWhitespace: true, + LegalComments: config.LegalCommentsEndOfFile, + UnsupportedCSSFeatures: compat.InlineStyle, + }, + }) +} + func TestLegalCommentsManyLinked(t *testing.T) { default_suite.expectBundled(t, bundled{ files: map[string]string{ diff --git a/internal/bundler_tests/snapshots/snapshots_default.txt b/internal/bundler_tests/snapshots/snapshots_default.txt index bbc56e7e961..befc369da74 100644 --- a/internal/bundler_tests/snapshots/snapshots_default.txt +++ b/internal/bundler_tests/snapshots/snapshots_default.txt @@ -1982,6 +1982,50 @@ c { /*! Copyright notice 1 */ /*! Copyright notice 2 */ +================================================================================ +TestLegalCommentsEscapeSlashScriptAndStyleEndOfFile +---------- /out/entry.js ---------- +x;a; +/*! <\/script> */ +/*! Bundled license information: + +js-pkg/index.js: + (*! <\/script> *) +*/ + +---------- /out/entry.css ---------- +x{y:z}a{b:c} +/*! <\/style> */ +/*! Bundled license information: + +css-pkg/index.css: + (*! <\/style> *) +*/ + +================================================================================ +TestLegalCommentsEscapeSlashScriptAndStyleExternal +---------- /out/entry.js.LEGAL.txt ---------- +/*! */ + +Bundled license information: + +js-pkg/index.js: + /*! */ + +---------- /out/entry.js ---------- +x;a; + +---------- /out/entry.css.LEGAL.txt ---------- +/*! */ + +Bundled license information: + +css-pkg/index.css: + /*! */ + +---------- /out/entry.css ---------- +x{y:z}a{b:c} + ================================================================================ TestLegalCommentsExternal ---------- /out/entry.js.LEGAL.txt ---------- @@ -2208,6 +2252,46 @@ export { } } +================================================================================ +TestLegalCommentsNoEscapeSlashScriptEndOfFile +---------- /out/entry.js ---------- +x;a; +/*! */ +/*! Bundled license information: + +js-pkg/index.js: + (*! *) +*/ + +---------- /out/entry.css ---------- +x{y:z}a{b:c} +/*! <\/style> */ +/*! Bundled license information: + +css-pkg/index.css: + (*! <\/style> *) +*/ + +================================================================================ +TestLegalCommentsNoEscapeSlashStyleEndOfFile +---------- /out/entry.js ---------- +x;a; +/*! <\/script> */ +/*! Bundled license information: + +js-pkg/index.js: + (*! <\/script> *) +*/ + +---------- /out/entry.css ---------- +x{y:z}a{b:c} +/*! */ +/*! Bundled license information: + +css-pkg/index.css: + (*! *) +*/ + ================================================================================ TestLegalCommentsNone ---------- /out/entry.js ---------- diff --git a/internal/helpers/comment.go b/internal/helpers/comment.go index aa3a82fb241..6cb02e620e9 100644 --- a/internal/helpers/comment.go +++ b/internal/helpers/comment.go @@ -68,6 +68,9 @@ seekBackwardToNewline: } func EscapeClosingTag(text string, slashTag string) string { + if slashTag == "" { + return text + } i := strings.Index(text, " 0 { j.AddString(c.options.JSFooter) @@ -5420,7 +5424,11 @@ func (c *linkerContext) generateChunkCSS(chunkIndex int, chunkWaitGroup *sync.Wa // Make sure the file ends with a newline j.EnsureNewlineAtEnd() - c.maybeAppendLegalComments(c.options.LegalComments, legalCommentList, chunk, &j, "/style") + slashTag := "/style" + if c.options.UnsupportedCSSFeatures.Has(compat.InlineStyle) { + slashTag = "" + } + c.maybeAppendLegalComments(c.options.LegalComments, legalCommentList, chunk, &j, slashTag) if len(c.options.CSSFooter) > 0 { j.AddString(c.options.CSSFooter)