Skip to content

Commit

Permalink
feat(vite): refactor and add unit test for resolveLibFilename
Browse files Browse the repository at this point in the history
  • Loading branch information
gobeli committed Jun 2, 2021
1 parent 244835d commit 97d1f98
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
32 changes: 32 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
@@ -0,0 +1,32 @@
import { resolveLibFilename } from '../build'

describe('resolveLibFilename', () => {
test('custom filename function', () => {
const filename = resolveLibFilename(
{
fileName: (format) => `custom-filename-function.${format}.js`,
entry: 'mylib.js'
},
'es',
'mylib'
)

expect(filename).toBe('custom-filename-function.es.js')
})

test('custom filename string', () => {
const filename = resolveLibFilename(
{ fileName: 'custom-filename', entry: 'mylib.js' },
'es',
'mylib'
)

expect(filename).toBe('custom-filename.es.js')
})

test('package name as filename', () => {
const filename = resolveLibFilename({ entry: 'mylib.js' }, 'es', 'mylib')

expect(filename).toBe('mylib.es.js')
})
})
14 changes: 11 additions & 3 deletions packages/vite/src/node/build.ts
Expand Up @@ -415,9 +415,7 @@ async function doBuild(
entryFileNames: ssr
? `[name].js`
: libOptions
? typeof libOptions.fileName === 'function'
? libOptions.fileName(output.format)
: `${libOptions.fileName || pkgName}.${output.format || `es`}.js`
? resolveLibFilename(libOptions, output.format, pkgName)
: path.posix.join(options.assetsDir, `[name].[hash].js`),
chunkFileNames: libOptions
? `[name].js`
Expand Down Expand Up @@ -613,6 +611,16 @@ function staticImportedByEntry(
return someImporterIs
}

export function resolveLibFilename(
libOptions: LibraryOptions,
format: ModuleFormat | undefined,
pkgName: string
): string {
return typeof libOptions.fileName === 'function'
? libOptions.fileName(format)
: `${libOptions.fileName || pkgName}.${format || `es`}.js`
}

function resolveBuildOutputs(
outputs: OutputOptions | OutputOptions[] | undefined,
libOptions: LibraryOptions | false,
Expand Down

0 comments on commit 97d1f98

Please sign in to comment.