Skip to content

Commit

Permalink
refactor: replace read-pkg-up with escalade (#187)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TrySound committed Oct 11, 2020
1 parent bda7369 commit 8f85878
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 57 deletions.
32 changes: 26 additions & 6 deletions lib/plugins/ramdisk.js
Expand Up @@ -10,16 +10,36 @@
*/
/* eslint-disable no-param-reassign */
const crypto = require('crypto');
const { symlinkSync } = require('fs');
const { symlinkSync, readFileSync } = require('fs');
const { basename, join, resolve } = require('path');

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

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

const readPkgName = () => {
const pkgPath = escalade(process.cwd(), (dir, names) => {
if (names.includes('package.json')) {
return 'package.json';
}
return false;
});
// istanbul ignore if
if (pkgPath == null) {
return null;
}
try {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
return pkg.name;
} catch (error) {
// istanbul ignore next
return null;
}
};

module.exports = {
init(compiler, options) {
const hasPlugin = compiler.options.plugins.some(
Expand All @@ -34,7 +54,7 @@ module.exports = {
throw new PluginExistsError('WebpackRamdiskPlugin exists in the specified configuration.');
}

const pkg = readPkgUp.sync() || {};
let pkgName = readPkgName();
const { path } = compiler.options.output;
const lastSegment = basename(path);
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.`;
Expand All @@ -61,14 +81,14 @@ module.exports = {
);
}

if (!pkg.name) {
if (!pkgName) {
// use md5 for a short hash that'll be consistent between wps starts
const md5 = crypto.createHash('md5');
md5.update(path);
pkg.name = md5.digest('hex');
pkgName = md5.digest('hex');
}

const newPath = join(pkg.name, lastSegment);
const newPath = join(pkgName, lastSegment);

// clean up the output path in prep for the ramdisk plugin
compiler.options.output.path = newPath;
Expand Down
66 changes: 51 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"chalk": "^4.0.0",
"connect-history-api-fallback": "^1.5.0",
"escalade": "^3.1.0",
"globby": "^11.0.0",
"http-proxy-middleware": "^1.0.3",
"is-path-cwd": "^2.2.0",
Expand All @@ -54,7 +55,6 @@
"onetime": "^5.1.0",
"open": "^7.0.3",
"p-defer": "^3.0.0",
"read-pkg-up": "^7.0.1",
"rimraf": "^3.0.2",
"strip-ansi": "^6.0.0",
"superstruct": "^0.10.12",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ramdisk-empty-pkg/app.js
@@ -0,0 +1,2 @@
const main = document.querySelector('main');
main.innerHTML = 'main';
1 change: 1 addition & 0 deletions test/fixtures/ramdisk-empty-pkg/package.json
@@ -0,0 +1 @@
{}
29 changes: 29 additions & 0 deletions test/fixtures/ramdisk-empty-pkg/webpack.config.js
@@ -0,0 +1,29 @@
const { resolve } = require('path');

const getPort = require('get-port');

const { WebpackPluginServe: Serve } = require('../../../lib/');

module.exports = {
context: __dirname,
entry: ['./app.js', 'webpack-plugin-serve/client'],
mode: 'development',
output: {
filename: './output.js',
path: resolve(__dirname, './output'),
publicPath: 'output/'
},
plugins: [
new Serve({
host: 'localhost',
port: getPort({ port: 55555 }),
ramdisk: true
})
],
resolve: {
alias: {
'webpack-plugin-serve/client': resolve(__dirname, '../../../client')
}
},
watch: true
};

0 comments on commit 8f85878

Please sign in to comment.