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

env.NODE_PATH support in Storyshots #2873

Merged
merged 8 commits into from
Feb 2, 2018
Merged

Conversation

igor-dv
Copy link
Member

@igor-dv igor-dv commented Jan 30, 2018

Issue: #2838

What I did

I've added NODE_PATH support in the mocked "require.context"

How to test

I have no idea how to add a working example into the monorepo... I've tested it with the repo from the issue.

@igor-dv igor-dv requested a review from tmeasday January 30, 2018 19:16
@codecov
Copy link

codecov bot commented Jan 30, 2018

Codecov Report

Merging #2873 into master will increase coverage by <.01%.
The diff coverage is 75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2873      +/-   ##
==========================================
+ Coverage   35.91%   35.92%   +<.01%     
==========================================
  Files         428      428              
  Lines        9483     9486       +3     
  Branches      973      993      +20     
==========================================
+ Hits         3406     3408       +2     
+ Misses       5417     5382      -35     
- Partials      660      696      +36
Impacted Files Coverage Δ
addons/storyshots/src/require_context.js 91.66% <75%> (-2.28%) ⬇️
...-native/src/preview/components/OnDeviceUI/index.js 0% <0%> (ø) ⬆️
lib/ui/src/modules/ui/index.js 0% <0%> (ø) ⬆️
addons/knobs/src/components/types/Date/index.js 23.52% <0%> (ø) ⬆️
lib/components/src/table/cell.js 65.21% <0%> (ø) ⬆️
lib/codemod/src/transforms/update-addon-info.js 50% <0%> (ø) ⬆️
addons/a11y/src/components/Report/Tags.js 0% <0%> (ø) ⬆️
lib/ui/src/modules/ui/containers/search_box.js 23.52% <0%> (ø) ⬆️
addons/knobs/src/react/WrapStory.js 49.42% <0%> (ø) ⬆️
addons/actions/src/lib/reviver.js 58.33% <0%> (ø) ⬆️
... and 47 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 09d03ec...5d3e6eb. Read the comment docs.

@tmeasday
Copy link
Member

tmeasday commented Jan 30, 2018

@igor-dv would it not make more sense to not call path.resolve() if the root require path (e.g. "components" in the case of #2838) isn't relative? So essentially doing what this code does in getFullPath?

While we are modifying this file, I would suggest we also fix the glaring bug that it calls require immediately, rather than later when req is called. This is completely inconsistent with what webpack does.

@igor-dv
Copy link
Member Author

igor-dv commented Jan 31, 2018

would it not make more sense to not call path.resolve() if the root require path (e.g. "components" in the case of #2838) isn't relative? So essentially doing what this code does in getFullPath?

Probably you are right... Will try to test it with a repo from the issue.

While we are modifying this file, I would suggest we also fix the glaring bug that it calls require immediately, rather than later when req is called. This is completely inconsistent with what webpack does.

Why is it a bug? it's a mock of what webpack does, so you can decide how to take care of it =)

@tmeasday
Copy link
Member

Why is it a bug?

Because the require happens at a different time -- in this code (direct from the docs):

// In storyshots, the require happens here:
const req = require.context('../src/components', true, /\.stories\.js$/)

function loadStories() {
  // in webpack it happens here:
  req.keys().forEach((filename) => req(filename))
}

configure(loadStories, module);

This has caused bugs in the past, for instance if you add a decorator between the two..

@tmeasday
Copy link
Member

[We don't have to fix this issue in this PR]

@igor-dv
Copy link
Member Author

igor-dv commented Jan 31, 2018

Is there any open bug on it?

@tmeasday
Copy link
Member

Only in my head for some reason I think..

@igor-dv
Copy link
Member Author

igor-dv commented Jan 31, 2018

@tmeasday, just checked with an absolute path, and path.resolve didn't do anything - left the absolute path as is and everything worked. So I think it probably checks internally for the relativity.

@tmeasday
Copy link
Member

tmeasday commented Jan 31, 2018

@igor-dv I created an issue here: #2894. Note that the original issue (#2838) will also have this problem ;)

@tmeasday, just checked with an absolute path, and path.resolve didn't do anything - left the absolute path as is and everything worked. So I think it probably checks internally for the relativity.

Yes, but not so much with "components", it treats this equivalently to "./components". i.e.:

> path.resolve('/a', '/b') # 1.
'/b'
> path.resolve('/a', 'b') # 2.
'/a/b'
> path.resolve('/a', './b') # 3.
'/a/b'

The idea of using path.resolve in this file is so relative paths (#3) are relative to the .storyshots/config.js NOT the cwd. We don't need to touch {whatever you call paths without a slash} (ie #2)

@igor-dv
Copy link
Member Author

igor-dv commented Feb 1, 2018

Aha ! Now I understand what you are talking about 😈 I'll do it

@@ -40,6 +41,14 @@ function isRelativeRequest(request) {
);
}

function getFullPath(dirname, request) {
if (isRelativeRequest(request) || !process.env.NODE_PATH) {
Copy link
Member

@tmeasday tmeasday Feb 2, 2018

Choose a reason for hiding this comment

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

I don't think it needs to be this complex; process.env.NODE_PATH will automatically apply if you require something non-relative, won't it?

So it can just be:

return isRelativeRequest(request) ? path.resolve(dirname, request) : dirname;

Copy link
Member Author

Choose a reason for hiding this comment

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

Checked it now, and it's not applied. It's just resolved according to the cwd. 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

Oooh, because we need to a fs.readdirSync in the directory. Sorry I am bit slow 😊

Do we not need to worry about other require rules in this case? For instance if I do require.context('foo') and I have node_modules/foo or ../node_modules/foo or .. etc

I guess this is better than nothing though!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah... I would say let's fix on request =)
Also, why would anyone require.context('foo'), when foo is a node module 🤔 what should we expect to happen

@tmeasday
Copy link
Member

tmeasday commented Feb 2, 2018 via email

@igor-dv igor-dv merged commit 968209d into master Feb 2, 2018
@igor-dv igor-dv deleted the storyshots-support-NODE_PATH branch February 2, 2018 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants