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.
  1. 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

  2. restart next, open the page, no more build error, instead the content is spinning screen shot 2018-09-25 at 23 09 13

  3. add a newline to index.js or mutate it somehow to cause next.js to hot reload the content

  4. see the components in almost fully functional state, but styles are broken screen shot 2018-09-25 at 23 13 23

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) screen shot 2018-09-25 at 23 14 46

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)

Most upvoted comments

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 .babelrc to include the @babel/transform-flow-strip-types plugin, tried using a bunch of different presets. Nothing seemed to solve the problem.

Now, the solution I found was to change the defaultLoaders.babel in the Next config file, and include the @babel/transform-flow-strip-types plugin in the .options.plugins field.

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.js file that is working just fine – hope it might help others. Also, might be nice to make sure that there’s an example/ 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.js that 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.

const internalNodeModulesRegExp = /(?:mylib)(?!.*node_modules)/;
const externalNodeModulesRegExp = /node_modules(?!\/(?:mylib)(?!.*node_modules))/;

module.exports = {
  webpack: (config, { dev, isServer, defaultLoaders }) => {
    // This is the fix that worked for me:
    if (!defaultLoaders.babel.options.plugins) {
      defaultLoaders.babel.options.plugins = []
    }
    defaultLoaders.babel.options.plugins.push("@babel/transform-flow-strip-types")

    config.resolve.symlinks = false;

    // No idea why we use config.externals here, copied from snippet above.
    // Without it, the build fails.
    config.externals = config.externals.map(external => {
      console.log('external', JSON.stringify(external, null, 2))
      if (typeof external !== 'function') return external;
      return (ctx, req, cb) =>
        internalNodeModulesRegExp.test(req) ? cb() : external(ctx, req, cb);
    });

    // Adding the rule to match my library
    config.module.rules.push({
      test: /\.+(js|jsx)$/,
      use: defaultLoaders.babel,
      include: [internalNodeModulesRegExp]
    });

    return config;
  },

  // Not sure why we add the "external" regexp to the middleware, but keeping
  // it from the snippet above.
  webpackDevMiddleware: config => {
    config.watchOptions.ignored = [
      ...config.watchOptions.ignored,
      externalNodeModulesRegExp,
    ];
    return config;
  },
};

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 .babelrc file, I tried applying the transpileModules config key and the next-plugin-transpile-modules plugin.

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.externals array is empty. Also, during my debug, I could see that Next.js is properly loading my custom .babelrc that includes the transform-flow-strip-types plugin. Tried multiple import paths of the plugin btw. Also, I tested just writing garbage in my custom .babelrc to 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 🤔