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: avoid packageJson without name in resolveLibCssFilename #19324

Merged
merged 5 commits into from
Feb 4, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
"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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part needs to be fixed as well.

const packageJson = findNearestPackageData(root, packageCache)?.data
const name =
libOptions.fileName ||
(packageJson && typeof libOptions.entry === 'string'
? getPkgName(packageJson.name)
: entryName)
if (!name)
throw new Error(
'Name in package.json is required if option "build.lib.fileName" is not provided.',
)

Copy link
Contributor Author

@Jinjiang Jinjiang Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sapphi-red Just confirm: I saw several test cases particular on packages/noname. Do you think it should be updated to packages/name as well? Thanks.

https://github.com/vitejs/vite/blob/main/packages/vite/src/node/__tests__/build.spec.ts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be packages/noname as-is, but the resolved name should be updated so that we test the fallback behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case updated. I also added an additional packages/vite/src/node/__tests__/packages/package.json to catch the resolver for noname. Please take a look. 🙏

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 @@ -3462,7 +3462,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.