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: compute getPkgName only when used #4729

Merged
merged 4 commits into from Sep 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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