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

Ignore field name mappings in package.json files that are not paths of existing files #46

Merged
merged 8 commits into from
Jun 24, 2018

Conversation

christoffer
Copy link
Contributor

@christoffer christoffer commented Jun 13, 2018

Context

Unfortunately it seems like the the change I introduced in #45 wasn't complete.

The browser field supports objects as well as strings to allow more specific overrides than a single file: unofficial, but most prominent, spec. I wasn't aware of this, and thus didn't account for this behavior in #45. Currently the plugin will blow up when encountering an "advanced" (spec terminology) mapping.

It seems like Webpack supports this spec: https://github.com/christoffer/webpack-resolution-example.

This PR is a suggestion for how to improve the current behavior so that it doesn't blow up when encountering a "bad" mapping, but it's not an implementation of the full spec (see below).

It (silently) ignores mappings that are not a straight file mapping of the main file, i.e. "advanced" mappings. This is obviously far from ideal, but at least it's (arguably) not a digression from the current (and pre-#45) behavior.

With this change, the package.json field names should be accepted only if:

A) they exist
B) they map to an existing file

I'll open an Issue for supporting the full browser spec to get parity with Webpack resolution behavior.

Implementation

While the core of the implementation is as simple as "if

&& is a string && that file exists", the changes required to implement that were a little more involved due to the async file checking. I also took the opportunity to refactor a little bit to get consistent APIs between the sync and async version.

// Not sure why we don't just return the full path? Why strip it?
return doneCallback(
undefined,
Filesystem.removeExtension(mainFieldMappedFile)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As the comment above asks, why are we actually removing the extension?

Copy link
Member

Choose a reason for hiding this comment

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

IIRC I added this comment some time ago when doing the initial work on the async version. @Jontem do you remember why the stripping is done? Or do you also think we can just return the raw filename?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The code has been rewritten many times since i looked at it and i don't recall the reason.

Copy link
Member

Choose a reason for hiding this comment

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

@christoffer I think you can go ahead and remove the stripping of the extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jonaskello Hi Jonas. Sorry for the delay, PTOs and what-not came in the way.

Would you be OK if we remove the extension in a separate diff? I feel that it's unrelated to the change I'm introducing here. And since it might have side effects, it would be nice to keep that change separate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My original comment was probably unclear. I was only asking out of curiosity, not as a way to ask if I should remove it now :)

Copy link
Member

Choose a reason for hiding this comment

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

@christoffer Sure, doing it in a separate diff makes sense.

@coveralls
Copy link

coveralls commented Jun 13, 2018

Coverage Status

Coverage decreased (-0.4%) to 82.069% when pulling a83e0dd on christoffer:handle-non-path-mappings into b84ab5c on dividab:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.1%) to 81.348% when pulling 6792de2 on christoffer:handle-non-path-mappings into b84ab5c on dividab:master.

} else {
// This is async code, we need to return in an else-branch otherwise the code still falls through and keeps recursing.
// While this might work in general, libraries that use neo-async like Webpack will actually not allow you to call the same callback twice.
// An example of where this caused issues: https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/11
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep this comment somewhere or is it no longer relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'll include it at the bottom return statement. Thanks for pointing it out!

);
}

// This is async code, we need to return unconditionally, otherwise the code still falls
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jonaskello Moved it here, let me know what you thing of the formulation. Changed "return in an else-branch otherwise" -> "return unconditionally, otherwise"

@jonaskello jonaskello merged commit ed424a6 into dividab:master Jun 24, 2018
@jonaskello
Copy link
Member

Released in 3.4.1.

@jonaskello
Copy link
Member

@christoffer Since tsconfig-paths-webpack-plugin depends on ^3.4.0 of this package I guess it will pick 3.4.1 now and we won't need to republish the plugin package for you to get these changes?

@christoffer
Copy link
Contributor Author

christoffer commented Jun 24, 2018 via email

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.

4 participants