|
import fs from 'node:fs/promises' |
|
import fsSync from 'node:fs' |
|
import path from 'node:path' |
|
import { |
|
isDepIncluded, |
|
isDepExcluded, |
|
isDepNoExternaled, |
|
isDepExternaled |
|
} from './sync.cjs' |
|
|
|
|
|
let pnp |
|
if (process.versions.pnp) { |
|
try { |
|
const { createRequire } = (await import('module')).default |
|
pnp = createRequire(import.meta.url)('pnpapi') |
|
} catch {} |
|
} |
|
|
|
export { isDepIncluded, isDepExcluded, isDepNoExternaled, isDepExternaled } |
|
|
|
|
|
export async function crawlFrameworkPkgs(options) { |
|
const pkgJsonPath = await findClosestPkgJsonPath(options.root) |
|
if (!pkgJsonPath) { |
|
|
|
if (typeof Deno !== 'undefined') { |
|
return { |
|
optimizeDeps: { include: [], exclude: [] }, |
|
ssr: { noExternal: [], external: [] } |
|
} |
|
} else { |
|
throw new Error(`Cannot find package.json from ${options.root}`) |
|
} |
|
} |
|
const pkgJson = await readJson(pkgJsonPath).catch((e) => { |
|
throw new Error(`Unable to read ${pkgJsonPath}`, { cause: e }) |
|
}) |
|
|
|
|
|
let optimizeDepsInclude = [] |
|
|
|
let optimizeDepsExclude = [] |
|
|
|
let ssrNoExternal = [] |
|
|
|
let ssrExternal = [] |
|
|
|
await crawl(pkgJsonPath, pkgJson) |
|
|
|
|
|
if (options.viteUserConfig) { |
|
|
|
const _optimizeDepsExclude = options.viteUserConfig?.optimizeDeps?.exclude |
|
if (_optimizeDepsExclude) { |
|
optimizeDepsInclude = optimizeDepsInclude.filter( |
|
(dep) => !isDepExcluded(dep, _optimizeDepsExclude) |
|
) |
|
} |
|
|
|
const _optimizeDepsInclude = options.viteUserConfig?.optimizeDeps?.include |
|
if (_optimizeDepsInclude) { |
|
optimizeDepsExclude = optimizeDepsExclude.filter( |
|
(dep) => !isDepIncluded(dep, _optimizeDepsInclude) |
|
) |
|
} |
|
|
|
const _ssrExternal = options.viteUserConfig?.ssr?.external |
|
if (_ssrExternal) { |
|
ssrNoExternal = ssrNoExternal.filter( |
|
(dep) => !isDepExternaled(dep, _ssrExternal) |
|
) |
|
} |
|
|
|
const _ssrNoExternal = options.viteUserConfig?.ssr?.noExternal |
|
if (_ssrNoExternal) { |
|
ssrExternal = ssrExternal.filter( |
|
(dep) => !isDepNoExternaled(dep, _ssrNoExternal) |
|
) |
|
} |
|
} |
|
|
|
return { |
|
optimizeDeps: { |
|
include: optimizeDepsInclude, |
|
exclude: optimizeDepsExclude |
|
}, |
|
ssr: { |
|
noExternal: ssrNoExternal, |
|
external: ssrExternal |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function crawl(pkgJsonPath, pkgJson, parentDepNames = []) { |
|
const isRoot = parentDepNames.length === 0 |
|
|
|
|
|
let deps = [ |
|
...Object.keys(pkgJson.dependencies || {}), |
|
...(isRoot ? Object.keys(pkgJson.devDependencies || {}) : []) |
|
] |
|
|
|
deps = deps.filter((dep) => { |
|
|
|
if (parentDepNames.includes(dep)) { |
|
return false |
|
} |
|
|
|
const isFrameworkPkg = options.isFrameworkPkgByName?.(dep) |
|
const isSemiFrameworkPkg = options.isSemiFrameworkPkgByName?.(dep) |
|
if (isFrameworkPkg) { |
|
|
|
|
|
optimizeDepsExclude.push(dep) |
|
|
|
|
|
ssrNoExternal.push(dep) |
|
} else if (isSemiFrameworkPkg) { |
|
|
|
|
|
ssrNoExternal.push(dep) |
|
} |
|
|
|
|
|
|
|
if (isFrameworkPkg === false || isSemiFrameworkPkg === false) { |
|
return false |
|
} |
|
|
|
|
|
|
|
else { |
|
return true |
|
} |
|
}) |
|
|
|
const promises = deps.map(async (dep) => { |
|
const depPkgJsonPath = await findDepPkgJsonPath(dep, pkgJsonPath) |
|
if (!depPkgJsonPath) return |
|
const depPkgJson = await readJson(depPkgJsonPath).catch(() => {}) |
|
if (!depPkgJson) return |
|
|
|
|
|
const cachedIsFrameworkPkg = ssrNoExternal.includes(dep) |
|
if (cachedIsFrameworkPkg) { |
|
return crawl(depPkgJsonPath, depPkgJson, parentDepNames.concat(dep)) |
|
} |
|
|
|
|
|
const isFrameworkPkg = options.isFrameworkPkgByJson?.(depPkgJson) |
|
const isSemiFrameworkPkg = options.isSemiFrameworkPkgByJson?.(depPkgJson) |
|
if (isFrameworkPkg || isSemiFrameworkPkg) { |
|
|
|
if (isFrameworkPkg) { |
|
optimizeDepsExclude.push(dep) |
|
ssrNoExternal.push(dep) |
|
} else if (isSemiFrameworkPkg) { |
|
ssrNoExternal.push(dep) |
|
} |
|
return crawl(depPkgJsonPath, depPkgJson, parentDepNames.concat(dep)) |
|
} |
|
|
|
|
|
|
|
|
|
if (!isRoot) { |
|
|
|
if (await pkgNeedsOptimization(depPkgJson, depPkgJsonPath)) { |
|
optimizeDepsInclude.push(parentDepNames.concat(dep).join(' > ')) |
|
} |
|
|
|
|
|
|
|
if (!options.isBuild && !ssrExternal.includes(dep)) { |
|
ssrExternal.push(dep) |
|
} |
|
} |
|
}) |
|
|
|
await Promise.all(promises) |
|
} |
|
} |
|
|
|
|
|
export async function findDepPkgJsonPath(dep, parent) { |
|
if (pnp) { |
|
try { |
|
const depRoot = pnp.resolveToUnqualified(dep, parent) |
|
if (!depRoot) return undefined |
|
return path.join(depRoot, 'package.json') |
|
} catch { |
|
return undefined |
|
} |
|
} |
|
|
|
let root = parent |
|
while (root) { |
|
const pkg = path.join(root, 'node_modules', dep, 'package.json') |
|
try { |
|
await fs.access(pkg) |
|
|
|
|
|
return fsSync.realpathSync(pkg) |
|
} catch {} |
|
const nextRoot = path.dirname(root) |
|
if (nextRoot === root) break |
|
root = nextRoot |
|
} |
|
return undefined |
|
} |
|
|
|
|
|
export async function findClosestPkgJsonPath(dir, predicate = undefined) { |
|
if (dir.endsWith('package.json')) { |
|
dir = path.dirname(dir) |
|
} |
|
while (dir) { |
|
const pkg = path.join(dir, 'package.json') |
|
try { |
|
const stat = await fs.stat(pkg) |
|
if (stat.isFile() && (!predicate || (await predicate(pkg)))) { |
|
return pkg |
|
} |
|
} catch {} |
|
const nextDir = path.dirname(dir) |
|
if (nextDir === dir) break |
|
dir = nextDir |
|
} |
|
return undefined |
|
} |
|
|
|
|
|
export async function pkgNeedsOptimization(pkgJson, pkgJsonPath) { |
|
|
|
|
|
if (pkgJson.module || pkgJson.exports) return false |
|
|
|
|
|
if (pkgJson.main) { |
|
const entryExt = path.extname(pkgJson.main) |
|
return !entryExt || entryExt === '.js' || entryExt === '.cjs' |
|
} |
|
|
|
|
|
|
|
try { |
|
await fs.access(path.join(path.dirname(pkgJsonPath), 'index.js')) |
|
return true |
|
} catch { |
|
return false |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function readJson(findDepPkgJsonPath) { |
|
return JSON.parse(await fs.readFile(findDepPkgJsonPath, 'utf8')) |
|
} |
|
|