next.js: Build problems with babel 7 for react-native(-web) dependencies using flow
Bug report
Using react-native dependencies which contain Flow code is broken with Next.js 7.0.0
Describe the bug
Depending on e.g. react-native-tab-view worked fine with Next.js 6.1.2 / React 16.4.2 / RNW ^0.8.8 / Babel 6 But is giving very strange behaviour with Next.js 7.0.0 / React 16.5.2 / RNW ^0.9.0 / Babel 7
A clear and concise description of what the bug is. Build error, which when worked around produces very strangely behaving components.
To Reproduce
git clone https://github.com/msand/repro-native-web-bug.git cd repro-native-web-bug yarn yarn dev open localhost:3000
see build error:
Failed to compile
./node_modules/react-native-tab-view/src/index.js
Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js):
SyntaxError: /Users/jannegylling/Micke/repro-native-web-bug/node_modules/react-native-tab-view/src/index.js: Support for the experimental syntax 'exportDefaultFrom' isn't currently enabled (12:8):
10 | export { default as SceneMap } from './SceneMap';
11 |
> 12 | export type { Route } from './TypeDefinitions';
| ^
13 | export type { Scene } from './TypeDefinitions';
14 | export type { NavigationState } from './TypeDefinitions';
15 | export type { SceneRendererProps } from './TypeDefinitions';
Add @babel/plugin-proposal-export-default-from (https://git.io/vb4yH) to the 'plugins' section of your Babel config to enable transformation.
This error occurred during the build time and cannot be dismissed.
-
add require(“@babel/preset-flow”), to beginning of the presets property of the returned babel config in node_modules/next/dist/build/babel/preset.js
-
restart next, open the page, no more build error, instead the content is spinning

-
add a newline to index.js or mutate it somehow to cause next.js to hot reload the content
-
see the components in almost fully functional state, but styles are broken

Expected behavior
The code builds without error, allowing to style react-native components
Expected looks can be seen in the initial commit (second commit only upgrades deps and babel conf to reproduce the bugs)

System information
- OS: macOS
- Browser: chrome
- Version of Next.js: 7.0.0
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (9 by maintainers)
Good news.
After investigating this a bit further, I think I found a solution to the issue – at least as it was happening to me.
What I have is a Yarn Workspace with 2 packages – one is a “web” package using Next.js, the other is just a library. I added flow type annotations to the library. This would make impossible to import the library in the web package – it would complain about the
type Xxx = {...}definitions.I tried using the “withTranspileModule” plugin, tried using configuration based on the snippet above shared by @msand , tried customizing my
.babelrcto include the@babel/transform-flow-strip-typesplugin, tried using a bunch of different presets. Nothing seemed to solve the problem.Now, the solution I found was to change the
defaultLoaders.babelin the Next config file, and include the@babel/transform-flow-strip-typesplugin in the.options.pluginsfield.Also, I have no idea if this would break any other parts of the build. In a tiny little test scenario it seems to be working OK.
Below is the final version of my
next.config.jsfile that is working just fine – hope it might help others. Also, might be nice to make sure that there’s anexample/that shows (and can be used as a test case) how to setup Yarn Workspaces + Flow across multiple packages. Today, in the examples there’s one showing that Yarn Workspaces can be set up, and another one that shows Flow being set up. But no example showed Yarn Workspaces + Flow at the same time.Here’s the
next.config.jsthat works for me. Btw, I have very limited knowledge on Webpack configuration, so I have no clue what the “externals” thing is; I just copied it from the snippet above. It seems like if I remove it, it breaks the config again.I was about to report a similar problem. That is, flow types not being removed; I’m not using react-native(-web) though.
I haven’t used next.js before v7 in monorepos, but I’m trying and I cannot import files from other packages in my Yarn Workspaces that use flow types.
No matter what I do, I cannot seem to have the strip-flow-types plugin be applied. I tried manually creating the
.babelrcfile, I tried applying thetranspileModulesconfig key and thenext-plugin-transpile-modulesplugin.EDIT: I tried using the configuration example above, adapting it to my own packages, with no luck. I also tried to debug it a bit - it seems like the
config.externalsarray is empty. Also, during my debug, I could see that Next.js is properly loading my custom.babelrcthat includes thetransform-flow-strip-typesplugin. Tried multiple import paths of the plugin btw. Also, I tested just writing garbage in my custom.babelrcto verify that it is indeed being used, and it is (ie, garbage in babel config breaks the build completely).@msand I just updated http://github.com/este/este, and it seems it works.
I guess that would probably work 🤔