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

refactor: use findNearestPackageData in more places #12577

Merged
merged 2 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 19 additions & 12 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
copyDir,
emptyDir,
joinUrlSegments,
lookupFile,
normalizePath,
requireResolveFromRootWithFallback,
} from './utils'
Expand All @@ -52,8 +51,8 @@ import {
initDepsOptimizer,
} from './optimizer'
import { loadFallbackPlugin } from './plugins/loadFallback'
import type { PackageData } from './packages'
import { watchPackageDataPlugin } from './packages'
import { findNearestPackageData, watchPackageDataPlugin } from './packages'
import type { PackageCache } from './packages'
import { ensureWatchPlugin } from './plugins/ensureWatch'
import { ESBUILD_MODULES_TARGET, VERSION } from './constants'
import { resolveChokidarOptions } from './watch'
Expand Down Expand Up @@ -578,7 +577,11 @@ export async function build(
const format = output.format || (cjsSsrBuild ? 'cjs' : 'es')
const jsExt =
ssrNodeBuild || libOptions
? resolveOutputJsExtension(format, getPkgJson(config.root)?.type)
? resolveOutputJsExtension(
format,
findNearestPackageData(config.root, config.packageCache)?.data
.type,
)
: 'js'
return {
dir: outDir,
Expand All @@ -595,7 +598,14 @@ export async function build(
? `[name].${jsExt}`
: libOptions
? ({ name }) =>
resolveLibFilename(libOptions, format, name, config.root, jsExt)
resolveLibFilename(
libOptions,
format,
name,
config.root,
jsExt,
config.packageCache,
)
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
chunkFileNames: libOptions
? `[name]-[hash].${jsExt}`
Expand Down Expand Up @@ -742,10 +752,6 @@ function prepareOutDir(
}
}

function getPkgJson(root: string): PackageData['data'] {
return JSON.parse(lookupFile(root, ['package.json']) || `{}`)
}

function getPkgName(name: string) {
return name?.[0] === '@' ? name.split('/')[1] : name
}
Expand All @@ -769,15 +775,16 @@ export function resolveLibFilename(
entryName: string,
root: string,
extension?: JsExt,
packageCache?: PackageCache,
): string {
if (typeof libOptions.fileName === 'function') {
return libOptions.fileName(format, entryName)
}

const packageJson = getPkgJson(root)
const packageJson = findNearestPackageData(root, packageCache)?.data
const name =
libOptions.fileName ||
(typeof libOptions.entry === 'string'
(packageJson && typeof libOptions.entry === 'string'
? getPkgName(packageJson.name)
: entryName)

Expand All @@ -786,7 +793,7 @@ export function resolveLibFilename(
'Name in package.json is required if option "build.lib.fileName" is not provided.',
)

extension ??= resolveOutputJsExtension(format, packageJson.type)
extension ??= resolveOutputJsExtension(format, packageJson?.type)

if (format === 'cjs' || format === 'es') {
return `${name}.${extension}`
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import type { JsonOptions } from './plugins/json'
import type { PluginContainer } from './server/pluginContainer'
import { createPluginContainer } from './server/pluginContainer'
import type { PackageCache } from './packages'
import { findNearestPackageData } from './packages'
import { loadEnv, resolveEnvPrefix } from './env'
import type { ResolvedSSROptions, SSROptions } from './ssr'
import { resolveSSROptions } from './ssr'
Expand Down Expand Up @@ -390,6 +391,7 @@ export async function resolveConfig(
let configFileDependencies: string[] = []
let mode = inlineConfig.mode || defaultMode
const isNodeEnvSet = !!process.env.NODE_ENV
const packageCache: PackageCache = new Map()

// some dependencies e.g. @vue/compiler-* relies on NODE_ENV for getting
// production-specific behavior, so set it early on
Expand Down Expand Up @@ -539,7 +541,7 @@ export async function resolveConfig(
)

// resolve cache directory
const pkgPath = lookupFile(resolvedRoot, [`package.json`], { pathOnly: true })
const pkgPath = findNearestPackageData(resolvedRoot, packageCache)?.dir
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
const cacheDir = normalizePath(
config.cacheDir
? path.resolve(resolvedRoot, config.cacheDir)
Expand Down Expand Up @@ -681,7 +683,7 @@ export async function resolveConfig(
return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file)
},
logger,
packageCache: new Map(),
packageCache,
createResolver,
optimizeDeps: {
disabled: 'build',
Expand Down