Skip to content

Commit

Permalink
fix: compute getPkgName only when used (#4729)
Browse files Browse the repository at this point in the history
  • Loading branch information
vwkd committed Sep 11, 2021
1 parent e623b4a commit ce29273
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
43 changes: 39 additions & 4 deletions packages/vite/src/node/__tests__/build.spec.ts
@@ -1,4 +1,5 @@
import { resolveLibFilename } from '../build'
import { resolve } from 'path'

describe('resolveLibFilename', () => {
test('custom filename function', () => {
Expand All @@ -8,25 +9,59 @@ describe('resolveLibFilename', () => {
entry: 'mylib.js'
},
'es',
'mylib'
resolve(__dirname, 'packages/name')
)

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

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

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

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

expect(filename).toBe('mylib.es.js')
})

test('custom filename and no package name', () => {
const filename = resolveLibFilename(
{
fileName: 'custom-filename',
entry: 'mylib.js'
},
'es',
resolve(__dirname, 'packages/noname')
)

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

test('missing filename', () => {
expect(() => {
resolveLibFilename(
{
entry: 'mylib.js'
},
'es',
resolve(__dirname, 'packages/noname')
)
}).toThrow()
})
})
3 changes: 3 additions & 0 deletions packages/vite/src/node/__tests__/packages/name/package.json
@@ -0,0 +1,3 @@
{
"name": "mylib"
}
@@ -0,0 +1 @@
{}
25 changes: 15 additions & 10 deletions packages/vite/src/node/build.ts
Expand Up @@ -422,8 +422,6 @@ async function doBuild(
}

try {
const pkgName = libOptions && getPkgName(config.root)

const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
return {
dir: outDir,
Expand All @@ -434,7 +432,7 @@ async function doBuild(
entryFileNames: ssr
? `[name].js`
: libOptions
? resolveLibFilename(libOptions, output.format || 'es', pkgName)
? resolveLibFilename(libOptions, output.format || 'es', config.root)
: path.posix.join(options.assetsDir, `[name].[hash].js`),
chunkFileNames: libOptions
? `[name].js`
Expand Down Expand Up @@ -576,9 +574,7 @@ function prepareOutDir(
function getPkgName(root: string) {
const { name } = JSON.parse(lookupFile(root, ['package.json']) || `{}`)

if (!name) throw new Error('no name found in package.json')

return name.startsWith('@') ? name.split('/')[1] : name
return name?.startsWith('@') ? name.split('/')[1] : name
}

function createMoveToVendorChunkFn(config: ResolvedConfig): GetManualChunk {
Expand Down Expand Up @@ -633,11 +629,20 @@ function staticImportedByEntry(
export function resolveLibFilename(
libOptions: LibraryOptions,
format: ModuleFormat,
pkgName: string
root: string
): string {
return typeof libOptions.fileName === 'function'
? libOptions.fileName(format)
: `${libOptions.fileName || pkgName}.${format}.js`
if (typeof libOptions.fileName === 'function') {
return libOptions.fileName(format)
}

const name = libOptions.fileName || getPkgName(root)

if (!name)
throw new Error(
'Name in package.json is required if option "build.lib.fileName" is not provided.'
)

return `${name}.${format}.js`
}

function resolveBuildOutputs(
Expand Down

0 comments on commit ce29273

Please sign in to comment.