Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib): Make libconfig work when minification and css split is enabled #29

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading