-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Build problems with babel 7 for react-native(-web) dependencies using flow #5288
Comments
While investigating this, I've found that if I add this prepare step (available from the "workaround" branch of the repo): And, import the built version of the dependency, it works as expected and without touching the Next.js internals. Thus eliminating the babel, react-native-web and react version changes as potential candidates for causing the symptoms. Thus, the error most likely resides in the babel/webpack/Next.js configuration of the setup. Could anyone give suggestions for what to try next? Thanks! |
How would this not be broken in v6 🤔 We never transpiled node_modules |
@steida Have you seen/solved any similar issue? Seems Este hasn't upgraded to v7 of Next.js and Babel yet, nor have any dependencies on react-native libraries, but thought you might know quite a lot about Flow + Babel and Next.js and similar setups to this. |
@timneutkens I have a custom next.config.js to compile the dependency, I thought this was the standard way to achieve this with Next.js: |
I guess that would probably work 🤔 |
Found a different workaround: msand/repro-native-web-bug@70d4198 .babelrc {
"presets": ["next/babel"],
"plugins": [["react-native-web", { "commonjs": true }]]
} next.config.js // Update these to match your package scope name.
const internalNodeModulesRegExp = /(?:react-native-tab-view)(?!.*node_modules)/;
const externalNodeModulesRegExp = /node_modules(?!\/(?:react-native-tab-view)(?!.*node_modules))/;
module.exports = {
webpack: (config, { dev, isServer, defaultLoaders }) => {
config.resolve.symlinks = false;
config.externals = config.externals.map(external => {
if (typeof external !== 'function') return external;
return (ctx, req, cb) =>
internalNodeModulesRegExp.test(req) ? cb() : external(ctx, req, cb);
});
config.module.rules.push({
test: /\.+(js|jsx)$/,
use: {
loader: 'next-babel-loader',
options: {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
['react-native-web', { commonjs: true }],
],
},
},
include: [internalNodeModulesRegExp],
});
config.resolve.alias = {
...config.resolve.alias,
'react-native$': 'react-native-web',
};
config.resolve.extensions = ['.web.js', '.js'];
return config;
},
webpackDevMiddleware: config => {
config.watchOptions.ignored = [
...config.watchOptions.ignored,
externalNodeModulesRegExp,
];
return config;
},
}; @timneutkens Is this the correct way to handle this? |
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 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 |
@msand I just updated http://github.com/este/este, and it seems it works. |
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 I tried using the "withTranspileModule" plugin, tried using configuration based on the snippet above shared by @msand , tried customizing my Now, the solution I found was to change the 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 Here's the 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'm seeing this too — I'm using yarn workspaces and flow for a web project (not react native). I was using flow in this project with Next 5 and Next 6, but am now encountering this issue while upgrading to Next 7.
Interestingly, I'm able to work around the error by renaming my |
I think this is a more generic bug (feature?). It seems cf martpie/next-transpile-modules#1 (comment) I am not sure who is responsible for this change (Babel, Next.js...) |
I'm experiencing this as well after upgrading to 7.0.2 its clearly a regression @timneutkens |
Moving to the babel.config.js with the exact same setup works for me. Hence the .babelrc are not being picked up correctly. |
Looks like RNW does not support Babel 7 yet? necolas/react-native-web#1191 |
Webpack is somehow broken, Babel works. |
Closing as this is the intended behavior of Babel 7 (upgraded from Babel 6). You'll have to use Also, be sure to add flow syntax support to your |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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:
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
The text was updated successfully, but these errors were encountered: