-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugin-vite): build size to lage
- Loading branch information
1 parent
219bde6
commit 86c0745
Showing
5 changed files
with
351 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import type { PackageJsonManifest } from './packageJson'; | ||
|
||
export interface Dependency { | ||
name: string; | ||
path: SourceAndDestination; | ||
dependencies: Dependency[]; | ||
} | ||
|
||
export interface SourceAndDestination { | ||
src: string; | ||
dest: string; | ||
} | ||
|
||
function isRootDirectory(dir: string) { | ||
// Linux or Windows root path | ||
return dir === '/' || /^[A-Z]:$/i.test(dir); | ||
} | ||
|
||
export async function lookupNodeModulesPaths(root: string, paths: string[] = []): Promise<string[]> { | ||
if (!root) return paths; | ||
if (!path.isAbsolute(root)) return paths; | ||
|
||
const p = path.join(root, 'node_modules'); | ||
|
||
if (fs.existsSync(p) && (await fs.promises.stat(p)).isDirectory()) { | ||
paths = paths.concat(p); | ||
} | ||
root = path.join(root, '..'); | ||
|
||
return isRootDirectory(root) ? paths : await lookupNodeModulesPaths(root, paths); | ||
} | ||
|
||
export async function readPackageJson(root = process.cwd()): Promise<PackageJsonManifest> { | ||
const packageJsonPath = path.join(root, 'package.json'); | ||
try { | ||
const packageJsonStr = await fs.promises.readFile(packageJsonPath, 'utf8'); | ||
try { | ||
return JSON.parse(packageJsonStr); | ||
} catch (error) { | ||
console.error(`parse 'package.json': ${packageJsonPath}`); | ||
throw error; | ||
} | ||
} catch (error) { | ||
console.error(`'package.json' not found: ${packageJsonPath}`); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function resolveDependencies(root: string) { | ||
const rootDependencies = Object.keys((await readPackageJson(root)).dependencies || {}); | ||
const resolve = async (prePath: string, dependencies: string[], collected: Map<string, Dependency> = new Map()) => | ||
await Promise.all( | ||
dependencies.map(async (name) => { | ||
let curPath = prePath, | ||
depPath = null, | ||
packageJson = null; | ||
while (!packageJson && !isRootDirectory(curPath)) { | ||
const allNodeModules = await lookupNodeModulesPaths(curPath); | ||
|
||
for (const nodeModules of allNodeModules) { | ||
depPath = path.join(nodeModules, name); | ||
if (fs.existsSync(depPath)) break; | ||
} | ||
|
||
if (depPath) { | ||
try { | ||
packageJson = await readPackageJson(depPath); | ||
} catch (err) { | ||
// lookup node_modules | ||
curPath = path.join(curPath, '..'); | ||
if (curPath.length < root.length) { | ||
console.error(`not found 'node_modules' in root path: ${root}`); | ||
throw err; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!depPath || !packageJson) { | ||
throw new Error(`find dependencies error in: ${curPath}`); | ||
} | ||
|
||
const result: Dependency = { | ||
name, | ||
path: { | ||
src: depPath, | ||
dest: path.relative(root, depPath), | ||
}, | ||
dependencies: [], | ||
}; | ||
const shouldResolveDeps = !collected.has(depPath); | ||
collected.set(depPath, result); | ||
if (shouldResolveDeps) { | ||
result.dependencies = await resolve(depPath, Object.keys(packageJson.dependencies || {}), collected); | ||
} | ||
return result; | ||
}) | ||
); | ||
return resolve(root, rootDependencies); | ||
} | ||
|
||
export async function getFlatDependencies(root = process.cwd()) { | ||
const dpesTree = await resolveDependencies(root); | ||
const depsFlat = new Map<string, SourceAndDestination>(); | ||
|
||
const flatten = (dep: Dependency) => { | ||
depsFlat.set(dep.path.src, dep.path); // dedup | ||
dep.dependencies.forEach(flatten); | ||
}; | ||
dpesTree.forEach(flatten); | ||
|
||
return [...depsFlat.values()]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export interface Person { | ||
name: string; | ||
url?: string; | ||
email?: string; | ||
} | ||
|
||
export interface PackageJsonManifest { | ||
// mandatory (npm) | ||
name: string; | ||
version: string; | ||
engines: { [name: string]: string }; | ||
|
||
// optional (npm) | ||
author?: string | Person; | ||
displayName?: string; | ||
description?: string; | ||
keywords?: string[]; | ||
categories?: string[]; | ||
homepage?: string; | ||
bugs?: string | { url?: string; email?: string }; | ||
license?: string; | ||
contributors?: string | Person[]; | ||
main?: string; | ||
browser?: string; | ||
repository?: string | { type?: string; url?: string }; | ||
scripts?: { [name: string]: string }; | ||
dependencies?: { [name: string]: string }; | ||
devDependencies?: { [name: string]: string }; | ||
private?: boolean; | ||
pricing?: string; | ||
|
||
// not supported (npm) | ||
// files?: string[]; | ||
// bin | ||
// man | ||
// directories | ||
// config | ||
// peerDependencies | ||
// bundledDependencies | ||
// optionalDependencies | ||
// os?: string[]; | ||
// cpu?: string[]; | ||
// preferGlobal | ||
// publishConfig | ||
} |
Oops, something went wrong.