Skip to content

Commit

Permalink
fix(lib): Make libconfig work when minification and css split is enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Oct 2, 2023
1 parent d04e4a6 commit 58aa8cc
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 19 deletions.
54 changes: 54 additions & 0 deletions __tests__/libconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* SPDX-FileCopyrightText: 2023 Ferdinand Thiessen <opensource@fthiessen.de>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { ESBuildOptions, resolveConfig } from 'vite'
import { describe, it, expect } from 'vitest'
import { LibraryOptions, createLibConfig } from '../lib/libConfig'

describe('library config', () => {
describe('workaround vite#14515 minify bug', () => {
it('minifies using esbuild by default', async () => {
const resolved = await createConfig('build')

// there is no minify plugin
expect(resolved.plugins.filter((plugin) => plugin.name === 'esbuild-minify').length).toBe(0)
// but minify enabled
expect(resolved.build.minify).toBe('esbuild')
expect(typeof resolved.esbuild).toBe('object')
expect((resolved.esbuild as ESBuildOptions).minifyIdentifiers).toBe(true)
expect((resolved.esbuild as ESBuildOptions).minifySyntax).toBe(true)
// but no whitespace minify
expect((resolved.esbuild as ESBuildOptions).minifyWhitespace).toBe(false)
})

it('minifies using esbuild on development when configured', async () => {
const resolved = await createConfig('build', 'development', { minify: true })

// there is no minify plugin
expect(resolved.plugins.filter((plugin) => plugin.name === 'esbuild-minify').length).toBe(0)
// but minify enabled
expect(resolved.build.minify).toBe('esbuild')
expect(typeof resolved.esbuild).toBe('object')
expect((resolved.esbuild as ESBuildOptions).minifyIdentifiers).toBe(true)
expect((resolved.esbuild as ESBuildOptions).minifySyntax).toBe(true)
// but no whitespace minify
expect((resolved.esbuild as ESBuildOptions).minifyWhitespace).toBe(false)
})

it('does not minify on development by default', async () => {
const resolved = await createConfig('build', 'development')

// there is no minify plugin
expect(resolved.plugins.filter((plugin) => plugin.name === 'esbuild-minify').length).toBe(0)
// and minify disabled
expect(resolved.build.minify).toBe(false)
})
})

const createConfig = async (command: 'build' | 'serve' = 'build', mode: 'development' | 'production' = 'production', options?: LibraryOptions) => await resolveConfig(await createLibConfig({
main: 'src/main.js',
}, options)({ command, mode, ssrBuild: false }), command)
})
12 changes: 12 additions & 0 deletions lib/libConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const createLibConfig = (entries: { [entryAlias: string]: string }, optio

return createBaseConfig({
...options,
// Workaround https://github.com/vitejs/vite/issues/14515
minify: false,
config: async (env) => {
// This config is used to extend or override our base config
// Make sure we get a user config and not a promise or a user config function
Expand Down Expand Up @@ -110,12 +112,22 @@ export const createLibConfig = (entries: { [entryAlias: string]: string }, optio

return mergeConfig({
plugins,
// workaround, see above (https://github.com/vitejs/vite/issues/14515)
esbuild: {
minifyIdentifiers: true,
minifySyntax: true,
minifyWhitespace: false,
legalComments: options.minify ? 'none' : 'inline',
target: undefined,
},
build: {
lib: {
entry: {
...entries,
},
},
// workaround, see above
minify: options.minify ?? env.mode === 'production' ? 'esbuild' : false,
cssCodeSplit: true,
outDir: 'dist',
rollupOptions: {
Expand Down
Loading

0 comments on commit 58aa8cc

Please sign in to comment.