perf(externalizeDepsPlugin): use cached package data to improve performance
This commit is contained in:
parent
db1128089a
commit
0c98f33573
|
@ -1,6 +1,5 @@
|
||||||
import path from 'node:path'
|
|
||||||
import { createRequire } from 'node:module'
|
|
||||||
import { type Plugin, mergeConfig } from 'vite'
|
import { type Plugin, mergeConfig } from 'vite'
|
||||||
|
import { loadPackageData } from '../utils'
|
||||||
|
|
||||||
export interface ExternalOptions {
|
export interface ExternalOptions {
|
||||||
exclude?: string[]
|
exclude?: string[]
|
||||||
|
@ -13,9 +12,7 @@ export interface ExternalOptions {
|
||||||
export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | null {
|
export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | null {
|
||||||
const { exclude = [], include = [] } = options
|
const { exclude = [], include = [] } = options
|
||||||
|
|
||||||
const packagePath = path.resolve(process.cwd(), 'package.json')
|
const pkg = loadPackageData() || {}
|
||||||
const require = createRequire(import.meta.url)
|
|
||||||
const pkg = require(packagePath)
|
|
||||||
let deps = Object.keys(pkg.dependencies || {})
|
let deps = Object.keys(pkg.dependencies || {})
|
||||||
|
|
||||||
if (include.length) {
|
if (include.length) {
|
||||||
|
|
|
@ -53,6 +53,7 @@ export function loadEnv(
|
||||||
interface PackageData {
|
interface PackageData {
|
||||||
main?: string
|
main?: string
|
||||||
type?: 'module' | 'commonjs'
|
type?: 'module' | 'commonjs'
|
||||||
|
dependencies?: Record<string, string>
|
||||||
}
|
}
|
||||||
|
|
||||||
let packageCached: PackageData | null = null
|
let packageCached: PackageData | null = null
|
||||||
|
@ -65,7 +66,8 @@ export function loadPackageData(root = process.cwd()): PackageData | null {
|
||||||
const data = _require(pkg)
|
const data = _require(pkg)
|
||||||
packageCached = {
|
packageCached = {
|
||||||
main: data.main,
|
main: data.main,
|
||||||
type: data.type
|
type: data.type,
|
||||||
|
dependencies: data.dependencies
|
||||||
}
|
}
|
||||||
return packageCached
|
return packageCached
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue