flisboac/react-native-monorepo-helper

Error: Expected file to be absolute path but got stream at new Modul when using styled-components

Closed this issue · 5 comments

  • I'm submitting a ...
    [x] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary

I stumbled upon what seems like a bug. When I try to add styled-components into my project I've setup with this library. I get the following error during a app bundling: error: bundling failed: Error: Expected file to be absolute path but got stream at new Module

  • Other information
    You can replicate this issue with this repo (it was firstly created for some other issue. Sorry for the name confusion).

Am I doing something wrong or using this helper the bad way? I'll be greatfull for any help :).

Hello, @kaladivo!

It seems my script has correctly resolved styled-components to a package.json module, but it's deducing the main executable from the wrong main property. It seems the correct property in this case is react-native (resolve uses main by default, unless you override the default behaviour via packageFilter).

I stumbled upon some other errors here and there, but I really don't have any more time to investigate this right now. Perhaps later, or tomorrow. But I got the gist of what needs to be done. Something along the lines of (at index.ts:326):

    resolveInProject(...) {
        /* ... */
        let packageFilter = (pkg, pkgpath) => {
            if (typeof pkg["react-native"] === 'string') {
                pkg["main"] = pkg["react-native"];
            }
            return packageFilter;
        };
        /* ... */
        if (!resolvedName) {
            try {
                resolvedName = resolve.sync(moduleName, {
                    basedir,
                    extensions,
                    package: packageJson,
                    packageFilter,
                    paths,
                });
            }
            catch (error) { }
        }
        /* ... (rest of the function) ... */
    }

Will give it a go as soon as possible.

Thank you for your reply. I've tried your fix (in my clonned repo of this library). There seems to be another issue with loading index files. Imports that should resolve to index.js files are not resolved correctly. For example lets say that I have file src/index.js and in some other file I import that file import test from './src' I always get the following error Module ./src does not exist in the Haste module map. I think this issue could be related.

I have no experiences with writing my own resolvers but I might find the time to look into this also and maybe help fix it. I really like this library and I think it is much needed (because of the poor rn support of monorepos).

Thank you, @kaladivo!

I made some fixes in master. Some were overdue, others were found while I was trying to resolve this issue.

  • The way the script searches for Yarn/Lerna was changed. Now it properly handles globs (like in your case, packages/**; it was resolving into a lot of packages in node_modules);
  • The CustomResolver (the one that's passed into module.exports of rn-cli.config.js) now searches with two base directories: the monorepo's root and the "origin module"'s root ("origin module" is the script/module importing a dependency). Hopefully, this should fix the problem you described (about not being able to resolve ./src)
  • Applied the fix from my previous comment (the packageFilter thing);

I tested with your test repo, and all seemed well. If you can, please test it yourself, just in case I missed something. If you have any suggestion or additional correction to make, please feel free to create a PR, I'll be more than happy to accept it! Once we're done, I will release a new patch version.

Thank you! It works now. I've tried it with one of my bigger projects though and find out that the index.js files are not loaded correctly. I've created a new issue for it (#4). I believe that this bug is resolved. Thank you once again :).

Version 0.2.3 was just released with the fixes for this issue. Gonna check #4 in a moment. Closing!