Skip to content

Commit c252dee

Browse files
authored
fix(lib): keep annotation comments for es output (#337)
1 parent f16e598 commit c252dee

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/vite/src/node/build.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,15 @@ async function buildEnvironment(
789789
(typeof input === 'string' || Object.keys(input).length === 1)),
790790
minify:
791791
options.minify === 'oxc'
792-
? true
792+
? libOptions && (format === 'es' || format === 'esm')
793+
? {
794+
compress: true,
795+
mangle: true,
796+
// Do not minify whitespace for ES lib output since that would remove
797+
// pure annotations and break tree-shaking
798+
removeWhitespace: false,
799+
}
800+
: true
793801
: options.minify === false
794802
? 'dce-only'
795803
: false,

playground/lib/__tests__/lib.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ describe.runIf(isBuild)('build', () => {
5353
expect(code).not.toMatch('__vitePreload')
5454

5555
// Test that library chunks are hashed
56-
expect(code).toMatch(/await import\(`\.\/message-[-\w]{8}.js`\)/)
56+
expect(code).toMatch(/await import\("\.\/message-[-\w]{8}.js"\)/)
5757
})
5858

5959
test('Library mode does not have any reference to pure CSS chunks', async () => {
6060
const code = readFile('dist/lib/dynamic-import-message.es.mjs')
6161

6262
// Does not import pure CSS chunks and replaced by `Promise.resolve({})` instead
63-
expect(code).not.toMatch(/await import\(`\.\/dynamic-[-\w]{8}.js`\)/)
63+
expect(code).not.toMatch(
64+
/await import\(['"`]\.\/dynamic-[-\w]{8}.js['"`]\)/,
65+
)
6466
expect(code).toMatch(/await Promise.resolve\(\{.*\}\)/)
6567
})
6668

0 commit comments

Comments
 (0)