-
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
Fix react-native-web example by adding babel plugin #9078
Conversation
Stats from current PRDefault Server ModeGeneral
Client Bundles (main, webpack, commons)
Client Bundles (main, webpack, commons) Modern
Client Pages
Client Pages Modern
Client Build Manifests
Rendered Page Sizes
Serverless ModeGeneral
Client Bundles (main, webpack, commons)
Client Bundles (main, webpack, commons) Modern
Client Pages
Client Pages Modern
Client Build Manifests
Serverless bundles
Commit: 0caaf60 |
This seems unneeded based on your description. |
@timneutkens I think it's a rare case where RNW Next won't be used alongside a full react-native app. So I think most people are going to have this problem. At the very least, info on the babel plugin should be clearly documented (it took me many hours to track down this issue). |
The reason I'm saying it doesn't sound correct is that you're saying that Babel somehow resolves modules which is not a feature that Babel has. webpack resolves modules, Babel only transforms code. Meaning that the webpack alias that is already in the example should be enough to make it work. |
Ok yeah, I admit I not an expert at webpack and babel, so not sure exactly what's going on. Changing The error without the babel plugin is:
As you can see, for some reason something is loading files inside the |
Fyi, it's confirmed this PR does fix #7276 |
The reason why it works with the Babel plugin is that the plugin rewrites the imports. Technically using the plugin is good because it enables tree shaking. |
config.resolve.alias = { | ||
...(config.resolve.alias || {}), | ||
// Transform all direct `react-native` imports to `react-native-web` | ||
'react-native$': 'react-native-web' | ||
} | ||
defaultLoaders.babel.options.plugins = [ | ||
['react-native-web', { commonjs: true }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this to a .babelrc
instead. Then it'll be fine to merge 👍
I moved the babel config out to it's own file, and I also added resolve extensions for I just also ran into that issue. Most RNW projects will likely make use of the platform imports: |
Stats from current PRDefault Server ModeGeneral
Client Bundles (main, webpack, commons)
Client Bundles (main, webpack, commons) Modern
Client Pages
Client Pages Modern
Client Build Manifests
Rendered Page Sizes
Serverless ModeGeneral
Client Bundles (main, webpack, commons)
Client Bundles (main, webpack, commons) Modern
Client Pages
Client Pages Modern
Client Build Manifests
Serverless bundles
Commit: a1a750b |
Problem
This example will not compile if
react-native
is also installed in the same directory (you can test by addingreact-native
to this package.json.It won't compile because babel also needs an alias config for react-native to react-native-web
Compile error without the babel plugin:
Solution
Add
babel-plugin-react-native-web
which properly configures babel to use react-native-web instead of react-nativeEdit: This does fix #7276