Skip to content

Commit

Permalink
fix: avoid packageJson without name in resolveLibCssFilename (#19324)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinjiang authored Feb 4, 2025
1 parent 132a5dd commit f183bdf
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
19 changes: 9 additions & 10 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,15 @@ describe('resolveLibFilename', () => {
})

test('missing filename', () => {
expect(() => {
resolveLibFilename(
{
entry: 'mylib.js',
},
'es',
'myLib',
resolve(__dirname, 'packages/noname'),
)
}).toThrow()
const filename = resolveLibFilename(
{
entry: 'mylib.js',
},
'es',
'myLib',
resolve(__dirname, 'packages/noname'),
)
expect(filename).toBe('named-testing-package.mjs')
})

test('commonjs package extensions', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"name": "mylib",
"type": "module"
}
3 changes: 3 additions & 0 deletions packages/vite/src/node/__tests__/packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "named-testing-package"
}
4 changes: 2 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { dataURIPlugin } from './plugins/dataUri'
import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild'
import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
import { buildLoadFallbackPlugin } from './plugins/loadFallback'
import { findNearestPackageData } from './packages'
import { findNearestMainPackageData, findNearestPackageData } from './packages'
import type { PackageCache } from './packages'
import {
getResolvedOutDirs,
Expand Down Expand Up @@ -919,7 +919,7 @@ export function resolveLibFilename(
return libOptions.fileName(format, entryName)
}

const packageJson = findNearestPackageData(root, packageCache)?.data
const packageJson = findNearestMainPackageData(root, packageCache)?.data
const name =
libOptions.fileName ||
(packageJson && typeof libOptions.entry === 'string'
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import type { TransformPluginContext } from '../server/pluginContainer'
import { searchForWorkspaceRoot } from '../server/searchRoot'
import { type DevEnvironment } from '..'
import type { PackageCache } from '../packages'
import { findNearestPackageData } from '../packages'
import { findNearestMainPackageData } from '../packages'
import { addToHTMLProxyTransformResult } from './html'
import {
assetUrlRE,
Expand Down Expand Up @@ -3474,7 +3474,7 @@ export function resolveLibCssFilename(
return `${libOptions.fileName}.css`
}

const packageJson = findNearestPackageData(root, packageCache)?.data
const packageJson = findNearestMainPackageData(root, packageCache)?.data
const name = packageJson ? getPkgName(packageJson.name) : undefined

if (!name)
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f183bdf

Please sign in to comment.