-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 non-JS requires like require('image!Kitten')
#345
Comments
cc @amasad, perhaps the packager could autogenerate a Flow interface definition for the image files it fines. |
@jeffmo is working on this, I think. We want to add a rule to flowconfig to redirect certain require patterns to a predefined mock. |
Super. Looking forward to that. |
Thanks for the ping. I'm currently focused on building out 'import type'/'export type' -- but I will begin working on this module-path redirection stuff right after that. |
👍 also very interested in this |
+1 |
I think this is for Webpack users, if I've inferred correctly. |
It's for anyone using the require.js spec's extensions incl RN. |
Ok. Makes sense. |
…ving them Summary: This adds a new config option `module.name_mapper` that allows users to specify a regexp -> replacement template tuple to be applied to any matching module names before the Flow system resolves the name and looks it up. This has been asked for in many contexts (Webpack and Browserify allow users to specify a means of normalization of module names, for example). However, the most immediate use case is React Native, where they wish to have special kinds of require()s such as `require('image!kitty.png')`. These module names don't really exist, but a name mapper would be able to map them over to a mocked module interface (let's say `MockPNGImageModule`) so that the returned value can still be typed as it will actually work at runtime. For the react native request, see: #345 Feel free to bikeshed the name of this config entry -- I'm not super tied to it. Reviewed By: @gabelevi Differential Revision: D2019856
Update: 625c25a just landed which adds a config option of the form:
(This should go out to opam/brew/etc with the next release) When using Flow with it's [default] "node" module system, the mapper that wins is the first mapper whose regexp pattern matches and whose generated replacement results in module identifier string that represents an actual/known module in the system. For anyone who happens to use the "haste" module system, due to the way Flow lazily matches haste module definitions to module imports/requires, the config option isn't able to consider whether a replacement candidate is a valid module at the time the conversion happens -- so the haste module system will use the first mapper whose regexp string matches. The regexp string is just passed directly to ocaml's built-in regexp utilities, so that can serve as the reference for various supported regexp tokens and replacement templates: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html#VALregexp |
@jeffmo: can @ide: Should be pretty simple. Just making this object a module: https://github.com/facebook/react-native/blob/master/packager/react-packager/src/Packager/index.js#L178-L186 |
@amasad: Sure. It's a string match that happens just before the lookup, so the result of running the replacement should be the same as if you put the replacement directly into the code |
@jeffmo could you please provide an example of how |
@thealjey - there are a couple tests checked in to exercise Taking the first example that, shows
This means it will treat |
Is there any way to simply ignore all imports of e.g. |
backing up what @jedwards1211 said - it's a little cumbersome as is. for instance, in my use case, i'm using webpack and css modules and i'm pulling in local yaml and local css like so: import fields from '../_fields.yaml'
import classes from './reference.css'
// react down here understandably , both of those imports throw flow errors pretend both of those files are in the screenshot Looking up how to deal with this issue leads me to this url, and the flow docs When I attempt to use the solution mentioned in the docs module.name_mapper.extension= 'css' -> '<PROJECT_ROOT>/CSSFlowStub.js.flow' I get this error (was mapper.extension removed?): i also tried the other doc example, the non shorthand approach, and while it doesn't error out, it also doesn't appear to do anything for instance, I would expect this to throw a flow error: // my undeclared, potentially null/undefined require
import classes from './myClasses.css'
// react down here
return <div className={classes.div} /> But it doesnt So, yeah. The only way I can think to handle my particular case is to declare every single local require manually in my interface file, or use a flow ignore comment (which is what I currently do) or to manually re-assign and type all my non js imports like below // this works, but still requires manually declaring in my interface file
const classes: Object = require('./reference.css')
// react
return <div className={classes.div} /> Is there an official way to handle non js / local imports? Or should I just continue to use edit: forgot to mention my flow version. i'm on |
@rossPatton , pulling an example from the react-native codebase: Where this is the implementation of RelativeImageStub: Since Flow loads all modules and uses that |
https://blog.iansinnott.com/getting-started-with-flow-and-webpack/ helped me to solve this problem. |
like @AlexanderTserkovniy said this linked helped. This is what I ended up with: In your
In /* @flow */
declare export default { [key: string]: string } // example usage
import Main from 'main.vue'; I still have to explicitly specify the file extension when importing but it works good enough for now. |
I using this on my .flowconfig
but flow keeps complaining about |
@sibelius Instead of matching the whole module name, you can use extension matcher as suggested in previous posts. This works for me: Change: module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub' To: module.name_mapper.extension='\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)' -> 'RelativeImageStub' |
Do we have to reset some cache or something maybe? I have never seen any of these things work. Just tried a bunch of stuff on a new project again, and again with no luck unfortunately. Using 0.53.1.
|
Requiring images of kittens should always be allowed 😸 |
@jslz @sibelius Are you using react-native?
Then, the relevant portion of my .flowconfig:
This got rid of |
this one worked for me #345 (comment) in the latest version of flow |
@mikaello It works for me. Thanks! |
All of this actually didn't work for me. Btw I also have
|
No description provided.
The text was updated successfully, but these errors were encountered: