webpack-dependency-size
Advanced tools
Comparing version 0.0.3 to 0.1.0
85
index.js
@@ -11,6 +11,6 @@ const fs = require('fs'); | ||
// Gets the dependency name | ||
const ptrn = /^(.*node_modules\/(@[^\/]+\/)?[^\/]+)/; | ||
const ptrn = /^(.*node_modules\/(@[^/]+\/)?[^/]+)/; | ||
const getDepName = filepath => _.head(filepath.match(ptrn)); | ||
const statsOpts = { | ||
const statsOptions = { | ||
assets: false, | ||
@@ -44,3 +44,2 @@ builtAt: false, | ||
class DependencySizePlugin { | ||
constructor({ | ||
@@ -69,12 +68,18 @@ outputPath = 'dependency-size.json', | ||
return modules | ||
.map((m) => { | ||
const filepath = getFilepath(m.name); | ||
if (!ptrn.test(filepath)) { return; } | ||
.map((_module) => { | ||
const filepath = getFilepath(_module.name); | ||
if (!ptrn.test(filepath)) { | ||
return; | ||
} | ||
let { size } = m; | ||
let { size } = _module; | ||
if (this.gzip) { | ||
if (typeof m.source === 'string') { | ||
size = gzipSize.sync(m.source); | ||
} else if (Array.isArray(m.modules)) { | ||
size = m.modules.reduce((s, m) => (s + gzipSize.sync(m.source)), 0); | ||
if (typeof _module.source === 'string') { | ||
size = gzipSize.sync(_module.source); | ||
} else if (Array.isArray(_module.modules)) { | ||
// eslint-disable-next-line unicorn/no-reduce | ||
size = _module.modules.reduce( | ||
(totalSize, { source }) => totalSize + gzipSize.sync(source), | ||
0, | ||
); | ||
} else { | ||
@@ -84,3 +89,3 @@ try { | ||
size = gzipSize.sync(fsSource); | ||
} catch (err) { | ||
} catch { | ||
console.warn(`Failed to calculate gzip size for "${filepath}". Using original size ${byteSize(size)}.`); | ||
@@ -94,37 +99,35 @@ } | ||
size, | ||
reasons: _.uniq(m.reasons.map(r => getFilepath(r.moduleName))).sort(), | ||
reasons: _.uniq(_module.reasons.map(r => getFilepath(r.moduleName))).sort(), | ||
}; | ||
}) | ||
.filter((m) => m); | ||
.filter(Boolean); | ||
} | ||
analyzeStats(stats, cb = _.noop) { | ||
analyzeStats(stats, callback = _.noop) { | ||
const statsJson = stats.toJson({ | ||
...statsOpts, | ||
...statsOptions, | ||
source: this.gzip, | ||
}); | ||
let deps = this.getDependencyModules(statsJson) | ||
.reduce((deps, m) => { | ||
const depName = getDepName(m.filepath); | ||
const dependencyModules = this.getDependencyModules(statsJson); | ||
const groupedDependencyModules = {}; | ||
dependencyModules.forEach((m) => { | ||
const depName = getDepName(m.filepath); | ||
if (!deps[depName]) { | ||
deps[depName] = { | ||
size: 0, | ||
files: [], | ||
}; | ||
} | ||
if (!groupedDependencyModules[depName]) { | ||
groupedDependencyModules[depName] = { | ||
size: 0, | ||
files: [], | ||
}; | ||
} | ||
deps[depName].size += m.size; | ||
deps[depName].files.push(m); | ||
groupedDependencyModules[depName].size += m.size; | ||
groupedDependencyModules[depName].files.push(m); | ||
}); | ||
return deps; | ||
}, {}); | ||
deps = _(deps) | ||
const dependencyReport = _(groupedDependencyModules) | ||
.toPairs() | ||
.orderBy(['1.size'], ['desc']) | ||
.map((dep) => { | ||
dep[1].size = byteSize(dep[1].size).toString(); | ||
dep[1].files | ||
.map(([dependencyPath, depData]) => { | ||
depData.files | ||
.sort((a, b) => b.size - a.size) | ||
@@ -135,15 +138,19 @@ .forEach((f) => { | ||
return dep; | ||
return { | ||
dependencyPath, | ||
size: byteSize(depData.size).toString(), | ||
files: depData.files, | ||
}; | ||
}) | ||
.fromPairs() | ||
.value(); | ||
this.writeData(deps, cb); | ||
this.writeData(dependencyReport, callback); | ||
} | ||
writeData(data, cb) { | ||
writeData(data, callback) { | ||
// eslint-disable-next-line node/prefer-promises/fs | ||
fs.writeFile( | ||
path.resolve(this.compiler.outputPath, this.outputPath), | ||
JSON.stringify(data, null, this.indent), | ||
cb | ||
callback, | ||
); | ||
@@ -150,0 +157,0 @@ } |
{ | ||
"name": "webpack-dependency-size", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Track/evaluate dependencies bundled into a Webpack build", | ||
"keywords": [ | ||
"webpack", | ||
"analyzer", | ||
"dependency", | ||
"size" | ||
"size", | ||
"analysis", | ||
"analyze", | ||
"report" | ||
], | ||
"license": "MIT", | ||
"repository": "privatenumber/webpack-dependency-size", | ||
"funding": "https://github.com/privatenumber/webpack-dependency-size?sponsor=1", | ||
"author": { | ||
@@ -21,2 +24,13 @@ "name": "Hiroki Osame", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint ." | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.js": "eslint" | ||
}, | ||
"dependencies": { | ||
@@ -26,3 +40,12 @@ "byte-size": "^7.0.0", | ||
"lodash": "^4.17.20" | ||
}, | ||
"devDependencies": { | ||
"@pvtnbr/eslint-config-base": "^0.1.9", | ||
"eslint": "^7.19.0", | ||
"husky": "^4.3.8", | ||
"lint-staged": "^10.5.3" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@pvtnbr/eslint-config-base" | ||
} | ||
} |
@@ -52,9 +52,16 @@ <h1> | ||
### Schema | ||
- `[package path]` bundled-in package (sorted by `size`) | ||
- `size` human-readable net import size from package | ||
- `files` specific files imported from the package (sorted by `size`) | ||
- `filepath` bundled-in file | ||
- `size` human-readable size | ||
- `reasons` request sources | ||
```js | ||
type File = { | ||
filepath: string; // bundled-in file | ||
size: string; // human-readable size | ||
reasons: string[]; // request sources | ||
}; | ||
type Report = { | ||
dependencyPath: string; // bundled-in package (sorted by `size`) | ||
size: string; // human-readable net import size from package | ||
files: File[]; // specific files imported from the package (sorted by `size`) | ||
}[]; | ||
``` | ||
### Example | ||
@@ -65,4 +72,5 @@ | ||
```json5 | ||
{ | ||
"./node_modules/axios": { | ||
[ | ||
{ | ||
"dependencyPath": "./node_modules/axios", | ||
"size": "40.15 KB", | ||
@@ -81,3 +89,4 @@ "files": [ | ||
}, | ||
"./node_modules/lodash": { | ||
{ | ||
"dependencyPath": "./node_modules/lodash", | ||
"size": "25.37 KB", | ||
@@ -96,6 +105,12 @@ "files": [ | ||
... | ||
} | ||
] | ||
``` | ||
## 👨👩👦👦 Related | ||
- [webpack-distsize](https://github.com/privatenumber/webpack-distsize) - Track Webpack output size via version control | ||
- [webpack-analyze-duplication-plugin](https://github.com/privatenumber/webpack-analyze-duplication-plugin) - Webpack plugin to detect duplicated modules | ||
## 💼 License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8647
136
113
4