From 2d9008e81d0d835e10437a959fac12529f7823e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Nikoli=C4=87?= Date: Fri, 30 Jun 2023 10:13:48 +0200 Subject: [PATCH] fix(css): use esbuild legalComments config when minifying CSS (#13661) --- packages/vite/src/node/plugins/css.ts | 1 + playground/minify/__tests__/minify.spec.ts | 3 +++ playground/minify/dir/module/index.css | 4 ++++ playground/minify/dir/module/index.js | 2 ++ playground/minify/dir/module/package.json | 6 ++++++ playground/minify/main.js | 3 +++ playground/minify/package.json | 3 +++ playground/minify/test.css | 2 ++ playground/minify/vite.config.js | 1 + pnpm-lock.yaml | 13 ++++++++++++- 10 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 playground/minify/dir/module/index.css create mode 100644 playground/minify/dir/module/index.js create mode 100644 playground/minify/dir/module/package.json diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index b2465d4a1d45ba..9043b9910b6d40 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -1521,6 +1521,7 @@ function resolveMinifyCssEsbuildOptions( logLevel: options.logLevel, logLimit: options.logLimit, logOverride: options.logOverride, + legalComments: options.legalComments, } if ( diff --git a/playground/minify/__tests__/minify.spec.ts b/playground/minify/__tests__/minify.spec.ts index b8d4e458e968b8..7b672d21134257 100644 --- a/playground/minify/__tests__/minify.spec.ts +++ b/playground/minify/__tests__/minify.spec.ts @@ -14,5 +14,8 @@ test.runIf(isBuild)('no minifySyntax', () => { const cssContent = readFile(path.resolve(assetsDir, cssFile)) expect(jsContent).toContain('{console.log("hello world")}') + expect(jsContent).not.toContain('/*! explicit comment */') + expect(cssContent).toContain('color:#ff0000') + expect(cssContent).not.toContain('/*! explicit comment */') }) diff --git a/playground/minify/dir/module/index.css b/playground/minify/dir/module/index.css new file mode 100644 index 00000000000000..16b551d967397c --- /dev/null +++ b/playground/minify/dir/module/index.css @@ -0,0 +1,4 @@ +/*! explicit comment */ +h2 { + color: #ff00ff; +} diff --git a/playground/minify/dir/module/index.js b/playground/minify/dir/module/index.js new file mode 100644 index 00000000000000..e76258a228a47c --- /dev/null +++ b/playground/minify/dir/module/index.js @@ -0,0 +1,2 @@ +/*! explicit comment */ +export const msg = `[success] minified module` diff --git a/playground/minify/dir/module/package.json b/playground/minify/dir/module/package.json new file mode 100644 index 00000000000000..037e7534a446e4 --- /dev/null +++ b/playground/minify/dir/module/package.json @@ -0,0 +1,6 @@ +{ + "name": "@vitejs/test-minify", + "private": true, + "type": "module", + "version": "0.0.0" +} diff --git a/playground/minify/main.js b/playground/minify/main.js index e77c8a997b3314..c732f9cab794ac 100644 --- a/playground/minify/main.js +++ b/playground/minify/main.js @@ -1,4 +1,7 @@ import './test.css' +import { msg } from 'minified-module' + +console.log(msg) if (window) { console.log('hello world') diff --git a/playground/minify/package.json b/playground/minify/package.json index 60048fb4377604..9ff696c8900cd8 100644 --- a/playground/minify/package.json +++ b/playground/minify/package.json @@ -8,5 +8,8 @@ "build": "vite build", "debug": "node --inspect-brk ../../packages/vite/bin/vite", "preview": "vite preview" + }, + "dependencies": { + "minified-module": "file:./dir/module" } } diff --git a/playground/minify/test.css b/playground/minify/test.css index dab6f886b3cf82..23f46306b712bd 100644 --- a/playground/minify/test.css +++ b/playground/minify/test.css @@ -1,3 +1,5 @@ +@import 'minified-module/index.css'; + h1 { /* do not minify as red text */ color: #ff0000; diff --git a/playground/minify/vite.config.js b/playground/minify/vite.config.js index 0f9accb248290a..018cc196e60707 100644 --- a/playground/minify/vite.config.js +++ b/playground/minify/vite.config.js @@ -2,6 +2,7 @@ import { defineConfig } from 'vite' export default defineConfig({ esbuild: { + legalComments: 'none', minifySyntax: false, }, }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b003126baa4730..161c84820f9aae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -709,7 +709,13 @@ importers: specifier: ^2.0.3 version: 2.0.3(patch_hash=z45f224eewh2pgpijxcc3aboqm) - playground/minify: {} + playground/minify: + dependencies: + minified-module: + specifier: file:./dir/module + version: file:playground/minify/dir/module + + playground/minify/dir/module: {} playground/multiple-entrypoints: devDependencies: @@ -10638,6 +10644,11 @@ packages: name: '@vitejs/test-json-module' dev: true + file:playground/minify/dir/module: + resolution: {directory: playground/minify/dir/module, type: directory} + name: '@vitejs/test-minify' + dev: false + file:playground/optimize-deps-no-discovery/dep-no-discovery: resolution: {directory: playground/optimize-deps-no-discovery/dep-no-discovery, type: directory} name: '@vitejs/test-dep-no-discovery'