Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace read-pkg-up with escalade #187

Merged
merged 1 commit into from Oct 11, 2020
Merged

refactor: replace read-pkg-up with escalade #187

merged 1 commit into from Oct 11, 2020

Conversation

TrySound
Copy link
Collaborator

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.

This PR contains:

  • bugfix
  • feature
  • refactor
  • tests
  • documentation
  • metadata

Breaking Changes?

  • yes
  • no

Copy link
Collaborator

@matheus1lva matheus1lva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less deps is always a good thing

const rm = require('rimraf');
const { WebpackPluginRamdisk } = require('webpack-plugin-ramdisk');

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

function readPkgName() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function readPkgName() {
const readPkgName = () => {

We should export this so it can be tested

Copy link
Owner

@shellscape shellscape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good overall with the change, but we need to get test coverage around this now. The one downside to removing dependencies that take care of this type of thing for you is that we now have to own more code, and that means tests.

@TrySound
Copy link
Collaborator Author

TrySound commented Sep 29, 2020

Heh, I just found currently pkg.name is always null so we always get ramdom directory name. pkg.name is incorrect. Should've been pkg.packageJson.name. And tests are wrong as well.

@TrySound
Copy link
Collaborator Author

Current tests covers logic around escalade. Though the code with generating id cannot be reached. Not sure how to cover it with tests as well.

Comment on lines 23 to 42
const readPkgName = () => {
const pkgPath = escalade(process.cwd(), (dir, names) => {
if (names.includes('package.json')) {
return 'package.json';
}
return false;
});
if (pkgPath == null) {
return null;
}
try {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
return pkg.name;
} catch (error) {
return null;
}
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're going to need test coverage of this method before we can merge

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned above. This is covered by existing tests. See https://github.com/shellscape/webpack-plugin-serve/pull/187/files#diff-817a4e640cdcf9964df2158c8020f627R33-R54

Though I can add test with broken package.json. This way random name can be tested too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, broken package.json will not work because webpack-nano failed as well for some reason.

@bebraw
Copy link
Collaborator

bebraw commented Oct 11, 2020

Looks good. Should we merge (squash please)?

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.
@TrySound
Copy link
Collaborator Author

@bebraw Done

@bebraw bebraw merged commit 8f85878 into master Oct 11, 2020
@bebraw
Copy link
Collaborator

bebraw commented Oct 11, 2020

Ok, merged.

We can even squash on merge without any extra work (one of the options) so maybe we'll use that in the future. 👍

@TrySound
Copy link
Collaborator Author

TrySound commented Oct 11, 2020

I enforced only squash commits at my work. Can be configured in settings.

@bebraw
Copy link
Collaborator

bebraw commented Oct 11, 2020

I enforced only squash commits at my work. Can be configured in settings.

That's a good idea.

@shellscape Do you want to set that up?

@TrySound TrySound deleted the escalade branch January 31, 2021 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants