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

Support scoped modules containing hyphens #744

Merged
merged 1 commit into from
Jul 25, 2017

Conversation

rosswarren
Copy link
Contributor

I was having problems with the order rule and scoped modules. It was returning NaN for the rank. I discovered the regex was not matching our scope name which contained hyphens.

This regex should match the full range of scoped module names I think. Please check it though.

@coveralls
Copy link

coveralls commented Feb 13, 2017

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling fc2569c on rosswarren:master into 0800951 on benmosher:master.

@@ -27,7 +27,7 @@ function isExternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
}

const scopedRegExp = /^@\w+\/\w+/
Copy link
Member

Choose a reason for hiding this comment

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

would this be simpler as /^@[^\/]+\/[^\/]+?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, just tested this on our codebase and that works 👍 I have updated the pull request.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling 2fb246d on rosswarren:master into 0800951 on benmosher:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling 2fb246d on rosswarren:master into 0800951 on benmosher:master.

@coveralls
Copy link

coveralls commented Feb 14, 2017

Coverage Status

Coverage increased (+0.003%) to 94.915% when pulling 2fb246d on rosswarren:master into 0800951 on benmosher:master.

@ljharb
Copy link
Member

ljharb commented Feb 14, 2017

Instead of regexes, should we use the official https://github.com/npm/validate-npm-package-name ? It'll be a scoped package if it's an invalid old one but a valid new one, and we won't have to maintain our own logic.

@rosswarren
Copy link
Contributor Author

@ljharb I am not sure if that will help us with detection of scoped modules 🤔 . It will validate but not tell us what type it is.

@ljharb
Copy link
Member

ljharb commented Feb 14, 2017

It returns two booleans, "old" and "new" - new meaning, including scoped. If they're both true it's scoped, if just "old" is true it's unscoped, if both are false, it's invalid.

@rosswarren
Copy link
Contributor Author

@ljharb unfortunately this isn't how it works

capture

@ljharb
Copy link
Member

ljharb commented Feb 14, 2017

@rosswarren what about checking "new", and if it starts with an "@"?

That's an official npm package, and it'd really be nice to use that instead of messing around with regexps.

@rosswarren
Copy link
Contributor Author

@ljharb OK, good idea. I will get it done!

@rosswarren
Copy link
Contributor Author

@ljharb hmm, I tried to implement this but it seems that the isScoped function is passed the whole import for example @scope/donthaveit/lib/foo rather than just the package which would be @scope/donthaveit so you would still need to try and parse out the package name with some code or a regex.

@rosswarren
Copy link
Contributor Author

@ljharb would it be a problem to only check if the first character is an "@"?. From NPM docs "If a package's name begins with @, then it is a scoped package". Not sure we really need to check anything else.

@ljharb
Copy link
Member

ljharb commented Feb 15, 2017

@rosswarren Probably - but @foo isn't a scoped package, and could be a webpack alias - same with @.

@rosswarren
Copy link
Contributor Author

@ljharb do you have any further suggestion given my previous comment? Not sure how to proceed here.

Copy link
Collaborator

@jfmengels jfmengels left a comment

Choose a reason for hiding this comment

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

I'd be fine with sticking to a regex in this case. If someone can make it work using validate-npm-package-name, even better. But as @rosswarren said, we pass the whole path (which can be like @foo/bar/folder/file.js), which the package doesn't validate, and you'd pretty much need a regex anyway to extract the data.

@@ -31,6 +31,7 @@ describe('importType(name)', function () {
it("should return 'external' for scopes packages", function() {
expect(importType('@cycle/core', context)).to.equal('external')
expect(importType('@cycle/dom', context)).to.equal('external')
expect(importType('@some-thing/something', context)).to.equal('external')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add additional tests for the following cases? (and more if you think of some, better safe than sorry)

  • @something/some-thing
  • @something/something/something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

have added 👍

@coveralls
Copy link

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling 1fe3b4a on rosswarren:master into b5a962f on benmosher:master.

1 similar comment
@coveralls
Copy link

coveralls commented Feb 17, 2017

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling 1fe3b4a on rosswarren:master into b5a962f on benmosher:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling bd243b2 on rosswarren:master into 98e7048 on benmosher:master.

@ljharb
Copy link
Member

ljharb commented Mar 4, 2017

You could use fs methods to split up the path, without using a regex, but that's probably fine to leave for a future PR.

@rosswarren would you mind rebasing, so as to remove all merge commits?

@coveralls
Copy link

coveralls commented Mar 4, 2017

Coverage Status

Coverage increased (+0.009%) to 94.924% when pulling 4d03274 on rosswarren:master into 98e7048 on benmosher:master.

@rosswarren
Copy link
Contributor Author

@ljharb yeah that would require some more thinking, there is probably a better solution for this. However, right now of our code importing scoped modules is full of extra line breaks so I am anxious to see this fixed 😄

And yeah no problem I rebased the branch 👍

@afairb
Copy link

afairb commented Apr 12, 2017

Hi. I see this PR is approved but not merged quite yet. Is there any chance it could get in soon as it's causing issues in my project when importing scoped modules? Thanks!

@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

The Windows build is still failing.

@coveralls
Copy link

coveralls commented Jun 5, 2017

Coverage Status

Coverage increased (+0.008%) to 95.165% when pulling 0ad9659 on rosswarren:master into 3c46d30 on benmosher:master.

@ljharb ljharb requested review from benmosher and jfmengels June 5, 2017 16:12
@danny-andrews
Copy link
Contributor

danny-andrews commented Jul 25, 2017

Can we get this merged? I'm having this same issue. Is the Windows build still failing?

@danny-andrews
Copy link
Contributor

danny-andrews commented Jul 25, 2017

You could use fs methods to split up the path, without using a regex, but that's probably fine to leave for a future PR.

Just out of curiosity, how would you do this? @ljharb

@ljharb
Copy link
Member

ljharb commented Jul 25, 2017

@danny-andrews a combo of path.parse, splitting on path.delimiter, etc.

@ljharb ljharb merged commit 40f5635 into import-js:master Jul 25, 2017
@danny-andrews
Copy link
Contributor

Siiiiick. Thanks for merging!

@danny-andrews
Copy link
Contributor

This looks like a good solution to this: https://github.com/mattdesl/require-package-name.

@ljharb
Copy link
Member

ljharb commented Aug 9, 2017

It doesn't seem to work for relative paths tho.

@danny-andrews
Copy link
Contributor

Brutal. Could use it in combination with https://www.npmjs.com/package/relative-require-regex?
Maybe not worth it for solving such a simple problem doe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants