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 for webpack resolution of modules. #382

Closed
meirish opened this issue Apr 13, 2015 · 19 comments
Closed

Support for webpack resolution of modules. #382

meirish opened this issue Apr 13, 2015 · 19 comments

Comments

@meirish
Copy link

meirish commented Apr 13, 2015

When I tried to get Flow working in our project last week, I was saddened that it wouldn't work because of the custom resolution we're using via webpack's modulesDirectories.

You can read more about our folder structure here: https://gist.github.com/ryanflorence/daafb1e3cb8ad740b346#shared-module-resolution

And webpack's modulesDirectories here: http://webpack.github.io/docs/resolving.html#resolving-a-module-path

Is there any chance that Flow will support this?

@lencioni
Copy link

I am also interested in this. I asked a similar question in IRC in Feb: https://botbot.me/freenode/flowtype/2015-02-10/?msg=31705409&page=2

At the time, @gabelevi mentioned that @jeffmo and @mroch were already talking about how to build it.

This seems somewhat related to #101.

@jeffmo
Copy link
Contributor

jeffmo commented Apr 25, 2015

I have a feature that I'm working on (that should be ready fairly soon) that will allow you to specify a config entry that lets you write a search/replace regexp pattern that gets applied to all module names before flow attempts to resolve them.

It's mostly geared towards supporting things like module aliases, but depending on how complex your lookup algorithm is compared to the built-in one this might achieve what you want?

An example of the feature I'm describing would be that you could write a config entry like:

module.name_mapper="(.*)", "/some/path/to/where/my/modules/are/\1"

This would match all module names and rewrite them to an absolute path somewhere that contains all of your modules. You'll be able to specify multiple of these entries in order to write up different rewrites for different patterns.

Would this do what you need by chance?

@lencioni
Copy link

What is the behavior if a module name is matched by multiple entries?

@jeffmo
Copy link
Contributor

jeffmo commented Apr 25, 2015

There are a couple of options for how it could work, but at the moment it seems to me that the least surprising would be that the first one that matches (in order of specification) wins. Thoughts?

@lencioni
Copy link

My preference would be that the first one that matches that can actually resolve the file wins. That would allow you to have global defaults with reasonable fallbacks. It would also more closely match the strategy that webpack uses which could help for familiarity and adoption.

@meirish
Copy link
Author

meirish commented Apr 28, 2015

Sorry I'm late in replying, but this sounds like this would work swimmingly! +1 to first match that resolves a file wins.

@jeffmo
Copy link
Contributor

jeffmo commented Apr 28, 2015

Ok, I'll take a wack at first-to-match-and-stat. I'm slightly unsure about stating the fs so often (read: perf for big projects), but I might be able to make it work.

Thanks for the feedback, this was helpful!

@jeffmo
Copy link
Contributor

jeffmo commented May 15, 2015

I just landed this config option yesterday and summarized how it works over toward the end of the following issue. Thanks for the report!

#345

@AdamBrodzinski
Copy link

@jeffmo would this solve my use case for the webpack resolve path? If I use ../../components/Foo flow works but if I use components/Foo I get the following error:

src/pages/DashLayout.js:4
  4: import Header from 'components/Header';
                        ^^^^^^^^^^^^^^^^^^^ components/Header. Required module not found

@kompot
Copy link

kompot commented Oct 24, 2015

@AdamBrodzinski Yes, it will help, take a look here.
cdebotton/react-universal@e57aadb

@AdamBrodzinski
Copy link

@kompot Thanks you're the best! working great now 😄

@dlindenkreuz
Copy link

dlindenkreuz commented Dec 13, 2016

For everyone looking at this issue in 2016, module.system.node.resolve_dirname was added in 0.18.1.

I solved this by adding these lines to my .flowconfig options (spares you the dreadfully long regex):

module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=src

The relevant part of my webpack config is:

resolve: {
  modules: ["src", "node_modules"]
}

@ghost
Copy link

ghost commented Jan 19, 2017

@dlindenkreuz what did you do exactly? Problem remains for me.

E.g. for src/foo/bar.js, import stuff from 'foo/bar' doesnt work. Flow tells me
Required module not found.

@dlindenkreuz
Copy link

@MoeSattler The important part is that resolve_dirname in .flowconfig and the resolve paths for modules in the webpack config are the same.

Are you on a recent flow version (>= 0.18.1)? Sometimes, the locally installed flow version in node_modules/.bin/flow is different from the globally installed in /usr/bin/flow.

@renarsvilnis
Copy link

@MoeSattler Did you manage to set it up?
I have the same issue as you are having right now.

@turadg
Copy link

turadg commented Nov 18, 2017

Note that this solutions no longer works in Flow 0.57.

In Flow 0.57 they introduced an optimization to,

only check the files in node_modules/ which are direct or transitive dependencies of the non-node_modules code.

Because the trick above made Flow think your app modules were another source of node_modules,
now the app source would not be focused either. You could see this in Flow logs by almost all app files being omitted from the "focused" count.

A working alternative is to explicitly define module.name_mapper directives for each child of your root directory, to tell Flow how to resolve those files. (Thanks to cdebotton/react-universal@e57aadb#commitcomment-19062820 for the idea.)

This script generates the directives you need in [options] of your .flowconfig:
https://gist.github.com/turadg/4c11ef9ac1af7b41276d0acc4d4bf8c8

@kentcdodds
Copy link

Hmmm... the solution above seemed to work fine for me in flow@0.66.0

@JohnDDuncanIII
Copy link

JohnDDuncanIII commented Jul 2, 2018

@kentcdodds I can't get this to work in flow 0.72.0

in .flowconfig:

[options]
module.system.node.resolve_dirname=node_modules

in webpack config:

resolve: {
  modules: ["node_modules"]
}

I'm trying to do something like import moment from 'moment' but I'm given Cannot resolve module moment. Do you know if this solution is supposed to work in this version of flow, or is there a new workaround?

@TrySound
Copy link
Contributor

TrySound commented Jul 2, 2018

@JohnDDuncanIII Do you use monorepo?

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

No branches or pull requests

12 participants