Skip to content

Commit

Permalink
fix(lib): Libraries do not need to be minified by default as this is …
Browse files Browse the repository at this point in the history
…done on app level

Co-authored-by: Ferdinand Thiessen <opensource@fthiessen.de>
Co-authored-by: Grigorii K. Shartsev <me@shgk.me>
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux and ShGKme committed Apr 22, 2024
1 parent e9be750 commit ccd2ea0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 14 additions & 4 deletions __tests__/libconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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')
const resolved = await createConfig('build', 'production', { minify: true })

// there is no minify plugin
expect(resolved.plugins.filter((plugin) => plugin.name === 'esbuild-minify').length).toBe(0)
Expand Down Expand Up @@ -72,7 +72,17 @@ describe('library config', () => {
})
})

const createConfig = async (command: 'build' | 'serve' = 'build', mode: 'development' | 'production' = 'production', options?: LibraryOptions) => await resolveConfig(await createLibConfig({
main: 'src/main.js',
}, options)({ command, mode, isSsrBuild: false }), command)
const createConfig = async (
command: 'build' | 'serve' = 'build',
mode: 'development' | 'production' = 'production',
options?: LibraryOptions,
) => {
return await resolveConfig(
await createLibConfig(
{
main: 'src/main.js',
},
options,
)({ command, mode, isSsrBuild: false }), command)
}
})
16 changes: 14 additions & 2 deletions lib/libConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import { ImportCSSPlugin } from './plugins/ImportCSS.js'
type OutputOptions = BuildOptions['rollupOptions']['output']

export interface LibraryOptions extends BaseOptions {
/**
* Whether to minify the output
* @default false For libraries the code is not minified by default for better DX. Usually it is not needed: a library will be minified as a part of an app bundling.
*/
minify?: boolean

/**
* Options for the rollup node externals plugin
*
Expand Down Expand Up @@ -52,7 +58,13 @@ export interface LibraryOptions extends BaseOptions {
*/
export const createLibConfig = (entries: { [entryAlias: string]: string }, options: LibraryOptions = {}): UserConfigFn => {
// Add default values for options
options = { config: {}, nodeExternalsOptions: {}, libraryFormats: ['es'], ...options }
options = {
config: {},
minify: false,
nodeExternalsOptions: {},
libraryFormats: ['es'],
...options,
}

const node = nodeExternals({
builtins: true, // Mark all node core modules, like `path` as external
Expand Down Expand Up @@ -137,7 +149,7 @@ export const createLibConfig = (entries: { [entryAlias: string]: string }, optio
formats: options.libraryFormats,
},
// workaround, see above
minify: options.minify ?? env.mode === 'production' ? 'esbuild' : false,
minify: (options.minify ?? env.mode === 'production') ? 'esbuild' : false,
cssCodeSplit: true,
outDir: 'dist',
rollupOptions: {
Expand Down

0 comments on commit ccd2ea0

Please sign in to comment.