Skip to content

Commit 8f85878

Browse files
authored
refactor: replace read-pkg-up with escalade (#187)
Ref https://packagephobia.com/result?p=read-pkg-up read-pkg-up is quite big package with many dependencies. It solves cases not necessary for this project like pretty json errors and normalisation of package.json data. In this diff I replaced it with dependency-free escalade and parsed json with native JSON.parse. This will let users to have faster installation and less bloated node_modules.
1 parent bda7369 commit 8f85878

File tree

9 files changed

+164
-57
lines changed

9 files changed

+164
-57
lines changed

lib/plugins/ramdisk.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,36 @@
1010
*/
1111
/* eslint-disable no-param-reassign */
1212
const crypto = require('crypto');
13-
const { symlinkSync } = require('fs');
13+
const { symlinkSync, readFileSync } = require('fs');
1414
const { basename, join, resolve } = require('path');
1515

1616
const isCwd = require('is-path-cwd');
17-
const readPkgUp = require('read-pkg-up');
17+
const escalade = require('escalade/sync');
1818
const rm = require('rimraf');
1919
const { WebpackPluginRamdisk } = require('webpack-plugin-ramdisk');
2020

2121
const { PluginExistsError, RamdiskPathError } = require('../errors');
2222

23+
const readPkgName = () => {
24+
const pkgPath = escalade(process.cwd(), (dir, names) => {
25+
if (names.includes('package.json')) {
26+
return 'package.json';
27+
}
28+
return false;
29+
});
30+
// istanbul ignore if
31+
if (pkgPath == null) {
32+
return null;
33+
}
34+
try {
35+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
36+
return pkg.name;
37+
} catch (error) {
38+
// istanbul ignore next
39+
return null;
40+
}
41+
};
42+
2343
module.exports = {
2444
init(compiler, options) {
2545
const hasPlugin = compiler.options.plugins.some(
@@ -34,7 +54,7 @@ module.exports = {
3454
throw new PluginExistsError('WebpackRamdiskPlugin exists in the specified configuration.');
3555
}
3656

37-
const pkg = readPkgUp.sync() || {};
57+
let pkgName = readPkgName();
3858
const { path } = compiler.options.output;
3959
const lastSegment = basename(path);
4060
const errorInfo = `The ramdisk option creates a symlink from \`output.path\`, to the ramdisk build output path, and must remove any existing \`output.path\` to do so.`;
@@ -61,14 +81,14 @@ module.exports = {
6181
);
6282
}
6383

64-
if (!pkg.name) {
84+
if (!pkgName) {
6585
// use md5 for a short hash that'll be consistent between wps starts
6686
const md5 = crypto.createHash('md5');
6787
md5.update(path);
68-
pkg.name = md5.digest('hex');
88+
pkgName = md5.digest('hex');
6989
}
7090

71-
const newPath = join(pkg.name, lastSegment);
91+
const newPath = join(pkgName, lastSegment);
7292

7393
// clean up the output path in prep for the ramdisk plugin
7494
compiler.options.output.path = newPath;

package-lock.json

Lines changed: 51 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"dependencies": {
4141
"chalk": "^4.0.0",
4242
"connect-history-api-fallback": "^1.5.0",
43+
"escalade": "^3.1.0",
4344
"globby": "^11.0.0",
4445
"http-proxy-middleware": "^1.0.3",
4546
"is-path-cwd": "^2.2.0",
@@ -54,7 +55,6 @@
5455
"onetime": "^5.1.0",
5556
"open": "^7.0.3",
5657
"p-defer": "^3.0.0",
57-
"read-pkg-up": "^7.0.1",
5858
"rimraf": "^3.0.2",
5959
"strip-ansi": "^6.0.0",
6060
"superstruct": "^0.10.12",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const main = document.querySelector('main');
2+
main.innerHTML = 'main';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { resolve } = require('path');
2+
3+
const getPort = require('get-port');
4+
5+
const { WebpackPluginServe: Serve } = require('../../../lib/');
6+
7+
module.exports = {
8+
context: __dirname,
9+
entry: ['./app.js', 'webpack-plugin-serve/client'],
10+
mode: 'development',
11+
output: {
12+
filename: './output.js',
13+
path: resolve(__dirname, './output'),
14+
publicPath: 'output/'
15+
},
16+
plugins: [
17+
new Serve({
18+
host: 'localhost',
19+
port: getPort({ port: 55555 }),
20+
ramdisk: true
21+
})
22+
],
23+
resolve: {
24+
alias: {
25+
'webpack-plugin-serve/client': resolve(__dirname, '../../../client')
26+
}
27+
},
28+
watch: true
29+
};

0 commit comments

Comments
 (0)