Skip to content

Commit f183bdf

Browse files
authored
fix: avoid packageJson without name in resolveLibCssFilename (#19324)
1 parent 132a5dd commit f183bdf

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

packages/vite/src/node/__tests__/build.spec.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,15 @@ describe('resolveLibFilename', () => {
410410
})
411411

412412
test('missing filename', () => {
413-
expect(() => {
414-
resolveLibFilename(
415-
{
416-
entry: 'mylib.js',
417-
},
418-
'es',
419-
'myLib',
420-
resolve(__dirname, 'packages/noname'),
421-
)
422-
}).toThrow()
413+
const filename = resolveLibFilename(
414+
{
415+
entry: 'mylib.js',
416+
},
417+
'es',
418+
'myLib',
419+
resolve(__dirname, 'packages/noname'),
420+
)
421+
expect(filename).toBe('named-testing-package.mjs')
423422
})
424423

425424
test('commonjs package extensions', () => {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"name": "mylib",
23
"type": "module"
34
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "named-testing-package"
3+
}

packages/vite/src/node/build.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { dataURIPlugin } from './plugins/dataUri'
6060
import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild'
6161
import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
6262
import { buildLoadFallbackPlugin } from './plugins/loadFallback'
63-
import { findNearestPackageData } from './packages'
63+
import { findNearestMainPackageData, findNearestPackageData } from './packages'
6464
import type { PackageCache } from './packages'
6565
import {
6666
getResolvedOutDirs,
@@ -919,7 +919,7 @@ export function resolveLibFilename(
919919
return libOptions.fileName(format, entryName)
920920
}
921921

922-
const packageJson = findNearestPackageData(root, packageCache)?.data
922+
const packageJson = findNearestMainPackageData(root, packageCache)?.data
923923
const name =
924924
libOptions.fileName ||
925925
(packageJson && typeof libOptions.entry === 'string'

packages/vite/src/node/plugins/css.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ import type { TransformPluginContext } from '../server/pluginContainer'
9191
import { searchForWorkspaceRoot } from '../server/searchRoot'
9292
import { type DevEnvironment } from '..'
9393
import type { PackageCache } from '../packages'
94-
import { findNearestPackageData } from '../packages'
94+
import { findNearestMainPackageData } from '../packages'
9595
import { addToHTMLProxyTransformResult } from './html'
9696
import {
9797
assetUrlRE,
@@ -3474,7 +3474,7 @@ export function resolveLibCssFilename(
34743474
return `${libOptions.fileName}.css`
34753475
}
34763476

3477-
const packageJson = findNearestPackageData(root, packageCache)?.data
3477+
const packageJson = findNearestMainPackageData(root, packageCache)?.data
34783478
const name = packageJson ? getPkgName(packageJson.name) : undefined
34793479

34803480
if (!name)

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)