Skip to content

Commit

Permalink
refactor: use findNearestPackageData in more places (#12577)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Mar 26, 2023
1 parent 1cc99f8 commit 35faae9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
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
10 changes: 6 additions & 4 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,12 +541,12 @@ export async function resolveConfig(
)

// resolve cache directory
const pkgPath = lookupFile(resolvedRoot, [`package.json`], { pathOnly: true })
const pkgDir = findNearestPackageData(resolvedRoot, packageCache)?.dir
const cacheDir = normalizePath(
config.cacheDir
? path.resolve(resolvedRoot, config.cacheDir)
: pkgPath
? path.join(path.dirname(pkgPath), `node_modules/.vite`)
: pkgDir
? path.join(pkgDir, `node_modules/.vite`)
: path.join(resolvedRoot, `.vite`),
)

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

0 comments on commit 35faae9

Please sign in to comment.