Skip to content
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

Merged
merged 2 commits into from
Oct 27, 2019
Merged

Fix react-native-web example by adding babel plugin #9078

merged 2 commits into from
Oct 27, 2019

Conversation

flybayer
Copy link
Contributor

@flybayer flybayer commented Oct 15, 2019

Problem

This example will not compile if react-native is also installed in the same directory (you can test by adding react-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:

/Users/b/c/forks/next.js/examples/with-react-native-web/node_modules/react-native/Libraries/Utilities/warnOnce.js:15
const warnedKeys: {[string]: boolean} = {};
      ^^^^^^^^^^

SyntaxError: Missing initializer in const declaration
    at Module._compile (internal/modules/cjs/loader.js:760:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/Users/b/c/forks/next.js/examples/with-react-native-web/node_modules/react-native/Libraries/react-native/react-native-implementation.js:14:18)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.react-native (/Users/b/c/forks/next.js/examples/with-react-native-web/.next/server/static/development/pages/_document.js:8315:18)
    at __webpack_require__ (/Users/b/c/forks/next.js/examples/with-react-native-web/.next/server/static/development/pages/_document.js:23:31)
    at Module../pages/_document.js (/Users/b/c/forks/next.js/examples/with-react-native-web/.next/server/static/development/pages/_document.js:8145:70)

Solution

Add babel-plugin-react-native-web which properly configures babel to use react-native-web instead of react-native

Edit: This does fix #7276

@ijjk
Copy link
Member

ijjk commented Oct 15, 2019

Stats from current PR

Default Server Mode
General
zeit/next.js canary flybayer/next.js fix-react-native-web Change
buildDuration 16.1s 16s -71ms
nodeModulesSize 48.5 MB 48.5 MB
Client Bundles (main, webpack, commons)
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.js 18.9 kB 18.9 kB
main-HASH.js gzip 6.79 kB 6.79 kB
webpack-HASH.js 1.53 kB 1.53 kB
webpack-HASH.js gzip 746 B 746 B
4952ddcd88e7..9dda6167a.js 21.9 kB 21.9 kB
4952ddcd88e7..167a.js gzip 7.81 kB 7.81 kB
de003c3a9d30..3a0560775.js 43.2 kB 43.2 kB
de003c3a9d30..0775.js gzip 15.5 kB 15.5 kB
framework.5b..dbaff70d3.js 125 kB 125 kB
framework.5b..70d3.js gzip 39.4 kB 39.4 kB
Overall change 211 kB 211 kB
Client Bundles (main, webpack, commons) Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.module.js 17.2 kB 17.2 kB
main-HASH.module.js gzip 6.52 kB 6.52 kB
webpack-HASH.module.js 1.53 kB 1.53 kB
webpack-HASH..dule.js gzip 746 B 746 B
de003c3a9d30..72.module.js 45.6 kB 45.6 kB
de003c3a9d30..dule.js gzip 16.5 kB 16.5 kB
framework.5b..d3.module.js 125 kB 125 kB
framework.5b..dule.js gzip 39.4 kB 39.4 kB
Overall change 190 kB 190 kB
Client Pages
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.js 1.81 kB 1.81 kB
_app.js gzip 873 B 873 B
_error.js 12 kB 12 kB
_error.js gzip 4.73 kB 4.73 kB
hooks.js 12.7 kB 12.7 kB
hooks.js gzip 4.79 kB 4.79 kB
index.js 318 B 318 B
index.js gzip 222 B 222 B
link.js 8.14 kB 8.14 kB
link.js gzip 3.5 kB 3.5 kB
routerDirect.js 408 B 408 B
routerDirect.js gzip 281 B 281 B
withRouter.js 419 B 419 B
withRouter.js gzip 280 B 280 B
Overall change 35.8 kB 35.8 kB
Client Pages Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.module.js 1.7 kB 1.7 kB
_app.module.js gzip 832 B 832 B
_error.module.js 23.3 kB 23.3 kB
_error.module.js gzip 8.59 kB 8.59 kB
hooks.module.js 1.52 kB 1.52 kB
hooks.module.js gzip 793 B 793 B
index.module.js 294 B 294 B
index.module.js gzip 223 B 223 B
link.module.js 8.53 kB 8.53 kB
link.module.js gzip 3.68 kB 3.68 kB
routerDirect.module.js 394 B 394 B
routerDirect..dule.js gzip 281 B 281 B
withRouter.module.js 404 B 404 B
withRouter.m..dule.js gzip 278 B 278 B
Overall change 36.1 kB 36.1 kB
Client Build Manifests
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_buildManifest.js 81 B 81 B
_buildManifest.js gzip 61 B 61 B
_buildManifest.module.js 81 B 81 B
_buildManife..dule.js gzip 61 B 61 B
Overall change 162 B 162 B
Rendered Page Sizes
zeit/next.js canary flybayer/next.js fix-react-native-web Change
index.html 3.62 kB 3.62 kB
index.html gzip 946 B 946 B
link.html 3.66 kB 3.66 kB
link.html gzip 954 B 954 B
withRouter.html 3.67 kB 3.67 kB
withRouter.html gzip 941 B 941 B
Overall change 10.9 kB 10.9 kB

Serverless Mode
General
zeit/next.js canary flybayer/next.js fix-react-native-web Change
buildDuration 16.6s 16.5s -90ms
nodeModulesSize 48.5 MB 48.5 MB
Client Bundles (main, webpack, commons)
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.js 18.9 kB 18.9 kB
main-HASH.js gzip 6.79 kB 6.79 kB
webpack-HASH.js 1.53 kB 1.53 kB
webpack-HASH.js gzip 746 B 746 B
4952ddcd88e7..9dda6167a.js 21.9 kB 21.9 kB
4952ddcd88e7..167a.js gzip 7.81 kB 7.81 kB
de003c3a9d30..3a0560775.js 43.2 kB 43.2 kB
de003c3a9d30..0775.js gzip 15.5 kB 15.5 kB
framework.5b..dbaff70d3.js 125 kB 125 kB
framework.5b..70d3.js gzip 39.4 kB 39.4 kB
Overall change 211 kB 211 kB
Client Bundles (main, webpack, commons) Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.module.js 17.2 kB 17.2 kB
main-HASH.module.js gzip 6.52 kB 6.52 kB
webpack-HASH.module.js 1.53 kB 1.53 kB
webpack-HASH..dule.js gzip 746 B 746 B
de003c3a9d30..72.module.js 45.6 kB 45.6 kB
de003c3a9d30..dule.js gzip 16.5 kB 16.5 kB
framework.5b..d3.module.js 125 kB 125 kB
framework.5b..dule.js gzip 39.4 kB 39.4 kB
Overall change 190 kB 190 kB
Client Pages
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.js 1.81 kB 1.81 kB
_app.js gzip 873 B 873 B
_error.js 12 kB 12 kB
_error.js gzip 4.73 kB 4.73 kB
hooks.js 12.7 kB 12.7 kB
hooks.js gzip 4.79 kB 4.79 kB
index.js 318 B 318 B
index.js gzip 222 B 222 B
link.js 8.14 kB 8.14 kB
link.js gzip 3.5 kB 3.5 kB
routerDirect.js 408 B 408 B
routerDirect.js gzip 281 B 281 B
withRouter.js 419 B 419 B
withRouter.js gzip 280 B 280 B
Overall change 35.8 kB 35.8 kB
Client Pages Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.module.js 1.7 kB 1.7 kB
_app.module.js gzip 832 B 832 B
_error.module.js 23.3 kB 23.3 kB
_error.module.js gzip 8.59 kB 8.59 kB
hooks.module.js 1.52 kB 1.52 kB
hooks.module.js gzip 793 B 793 B
index.module.js 294 B 294 B
index.module.js gzip 223 B 223 B
link.module.js 8.53 kB 8.53 kB
link.module.js gzip 3.68 kB 3.68 kB
routerDirect.module.js 394 B 394 B
routerDirect..dule.js gzip 281 B 281 B
withRouter.module.js 404 B 404 B
withRouter.m..dule.js gzip 278 B 278 B
Overall change 36.1 kB 36.1 kB
Client Build Manifests
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_buildManifest.js 81 B 81 B
_buildManifest.js gzip 61 B 61 B
_buildManifest.module.js 81 B 81 B
_buildManife..dule.js gzip 61 B 61 B
Overall change 162 B 162 B
Serverless bundles
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_error.js 254 kB 254 kB
_error.js gzip 68 kB 68 kB
hooks.html 3.75 kB 3.75 kB
hooks.html gzip 979 B 979 B
index.js 254 kB 254 kB
index.js gzip 68.4 kB 68.4 kB
link.js 262 kB 262 kB
link.js gzip 70.4 kB 70.4 kB
routerDirect.js 255 kB 255 kB
routerDirect.js gzip 68.4 kB 68.4 kB
withRouter.js 255 kB 255 kB
withRouter.js gzip 68.5 kB 68.5 kB
Overall change 1.28 MB 1.28 MB

Commit: 0caaf60

@timneutkens
Copy link
Member

This seems unneeded based on your description.

@flybayer
Copy link
Contributor Author

@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).

@timneutkens
Copy link
Member

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.

@flybayer
Copy link
Contributor Author

Ok yeah, I admit I not an expert at webpack and babel, so not sure exactly what's going on.

Changing react-native$ to react-native doesn't change anything.

The error without the babel plugin is:

/Users/b/c/forks/next.js/examples/with-react-native-web/node_modules/react-native/Libraries/Utilities/warnOnce.js:15
const warnedKeys: {[string]: boolean} = {};
      ^^^^^^^^^^

SyntaxError: Missing initializer in const declaration
    at Module._compile (internal/modules/cjs/loader.js:760:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/Users/b/c/forks/next.js/examples/with-react-native-web/node_modules/react-native/Libraries/react-native/react-native-implementation.js:14:18)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.react-native (/Users/b/c/forks/next.js/examples/with-react-native-web/.next/server/static/development/pages/_document.js:8315:18)
    at __webpack_require__ (/Users/b/c/forks/next.js/examples/with-react-native-web/.next/server/static/development/pages/_document.js:23:31)
    at Module../pages/_document.js (/Users/b/c/forks/next.js/examples/with-react-native-web/.next/server/static/development/pages/_document.js:8145:70)

As you can see, for some reason something is loading files inside the react-native which should never happen based on the webpack alias.

@Timer Timer added the examples Issue/PR related to examples label Oct 18, 2019
@flybayer
Copy link
Contributor Author

Fyi, it's confirmed this PR does fix #7276

@giuseppeg
Copy link
Contributor

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.
That said honestly I don't have idea why the alias is being ignored, it must be a conflict between webpack and node's resolution mechanisms on the server.

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 }]
Copy link
Member

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 👍

@flybayer
Copy link
Contributor Author

I moved the babel config out to it's own file, and I also added resolve extensions for .web.js, .web.ts, etc.

I just also ran into that issue. Most RNW projects will likely make use of the platform imports: .ios.js, .android.js, .web.js.

@ijjk
Copy link
Member

ijjk commented Oct 27, 2019

Stats from current PR

Default Server Mode
General
zeit/next.js canary flybayer/next.js fix-react-native-web Change
buildDuration 16s 16.3s ⚠️ +280ms
nodeModulesSize 48.7 MB 48.7 MB
Client Bundles (main, webpack, commons)
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.js 16.6 kB 16.6 kB
main-HASH.js gzip 5.86 kB 5.86 kB
webpack-HASH.js 1.53 kB 1.53 kB
webpack-HASH.js gzip 746 B 746 B
16b1a7225520..b6ebb586e.js 19.8 kB 19.8 kB
16b1a7225520..586e.js gzip 7.25 kB 7.25 kB
4952ddcd88e7..bfe360fe6.js 10.5 kB 10.5 kB
4952ddcd88e7..0fe6.js gzip 4.06 kB 4.06 kB
commons.HASH.js 11.3 kB 11.3 kB
commons.HASH.js gzip 4.23 kB 4.23 kB
de003c3a9d30..9e9c1d310.js 28.6 kB 28.6 kB
de003c3a9d30..d310.js gzip 10.8 kB 10.8 kB
framework.1b..cdcfe1283.js 125 kB 125 kB
framework.1b..1283.js gzip 39.4 kB 39.4 kB
Overall change 214 kB 214 kB
Client Bundles (main, webpack, commons) Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.module.js 14 kB 14 kB
main-HASH.module.js gzip 5.25 kB 5.25 kB
webpack-HASH.module.js 1.53 kB 1.53 kB
webpack-HASH..dule.js gzip 746 B 746 B
16b1a7225520..4c.module.js 15.9 kB 15.9 kB
16b1a7225520..dule.js gzip 6.08 kB 6.08 kB
4952ddcd88e7..4a.module.js 12.8 kB 12.8 kB
4952ddcd88e7..dule.js gzip 4.75 kB 4.75 kB
de003c3a9d30..c0.module.js 22.1 kB 22.1 kB
de003c3a9d30..dule.js gzip 8.67 kB 8.67 kB
framework.5b..d3.module.js 125 kB 125 kB
framework.5b..dule.js gzip 39.4 kB 39.4 kB
Overall change 192 kB 192 kB
Client Pages
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.js 1.63 kB 1.63 kB
_app.js gzip 788 B 788 B
_error.js 12.3 kB 12.3 kB
_error.js gzip 4.79 kB 4.79 kB
hooks.js 1.92 kB 1.92 kB
hooks.js gzip 941 B 941 B
index.js 318 B 318 B
index.js gzip 222 B 222 B
link.js 5.81 kB 5.81 kB
link.js gzip 2.56 kB 2.56 kB
routerDirect.js 413 B 413 B
routerDirect.js gzip 285 B 285 B
withRouter.js 423 B 423 B
withRouter.js gzip 284 B 284 B
Overall change 22.8 kB 22.8 kB
Client Pages Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.module.js 1.53 kB 1.53 kB
_app.module.js gzip 749 B 749 B
_error.module.js 10.5 kB 10.5 kB
_error.module.js gzip 4.26 kB 4.26 kB
hooks.module.js 1.52 kB 1.52 kB
hooks.module.js gzip 792 B 792 B
index.module.js 292 B 292 B
index.module.js gzip 223 B 223 B
link.module.js 5.47 kB 5.47 kB
link.module.js gzip 2.46 kB 2.46 kB
routerDirect.module.js 399 B 399 B
routerDirect..dule.js gzip 285 B 285 B
withRouter.module.js 409 B 409 B
withRouter.m..dule.js gzip 282 B 282 B
Overall change 20.1 kB 20.1 kB
Client Build Manifests
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_buildManifest.js 244 B 244 B
_buildManifest.js gzip 200 B 200 B
_buildManifest.module.js 251 B 251 B
_buildManife..dule.js gzip 209 B 209 B
Overall change 495 B 495 B
Rendered Page Sizes
zeit/next.js canary flybayer/next.js fix-react-native-web Change
index.html 3.94 kB 3.94 kB
index.html gzip 1 kB 1 kB
link.html 4.47 kB 4.47 kB
link.html gzip 1.08 kB 1.08 kB
withRouter.html 4.47 kB 4.47 kB
withRouter.html gzip 1.07 kB 1.07 kB
Overall change 12.9 kB 12.9 kB

Serverless Mode
General
zeit/next.js canary flybayer/next.js fix-react-native-web Change
buildDuration 17.1s 16.4s -625ms
nodeModulesSize 48.7 MB 48.7 MB
Client Bundles (main, webpack, commons)
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.js 16.6 kB 16.6 kB
main-HASH.js gzip 5.86 kB 5.86 kB
webpack-HASH.js 1.53 kB 1.53 kB
webpack-HASH.js gzip 746 B 746 B
16b1a7225520..b6ebb586e.js 19.8 kB 19.8 kB
16b1a7225520..586e.js gzip 7.25 kB 7.25 kB
4952ddcd88e7..bfe360fe6.js 10.5 kB 10.5 kB
4952ddcd88e7..0fe6.js gzip 4.06 kB 4.06 kB
commons.HASH.js 11.3 kB 11.3 kB
commons.HASH.js gzip 4.23 kB 4.23 kB
de003c3a9d30..9e9c1d310.js 28.6 kB 28.6 kB
de003c3a9d30..d310.js gzip 10.8 kB 10.8 kB
framework.1b..cdcfe1283.js 125 kB 125 kB
framework.1b..1283.js gzip 39.4 kB 39.4 kB
Overall change 214 kB 214 kB
Client Bundles (main, webpack, commons) Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
main-HASH.module.js 14 kB 14 kB
main-HASH.module.js gzip 5.25 kB 5.25 kB
webpack-HASH.module.js 1.53 kB 1.53 kB
webpack-HASH..dule.js gzip 746 B 746 B
16b1a7225520..4c.module.js 15.9 kB 15.9 kB
16b1a7225520..dule.js gzip 6.08 kB 6.08 kB
4952ddcd88e7..4a.module.js 12.8 kB 12.8 kB
4952ddcd88e7..dule.js gzip 4.75 kB 4.75 kB
de003c3a9d30..c0.module.js 22.1 kB 22.1 kB
de003c3a9d30..dule.js gzip 8.67 kB 8.67 kB
framework.5b..d3.module.js 125 kB 125 kB
framework.5b..dule.js gzip 39.4 kB 39.4 kB
Overall change 192 kB 192 kB
Client Pages
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.js 1.63 kB 1.63 kB
_app.js gzip 788 B 788 B
_error.js 12.3 kB 12.3 kB
_error.js gzip 4.79 kB 4.79 kB
hooks.js 1.92 kB 1.92 kB
hooks.js gzip 941 B 941 B
index.js 318 B 318 B
index.js gzip 222 B 222 B
link.js 5.81 kB 5.81 kB
link.js gzip 2.56 kB 2.56 kB
routerDirect.js 413 B 413 B
routerDirect.js gzip 285 B 285 B
withRouter.js 423 B 423 B
withRouter.js gzip 284 B 284 B
Overall change 22.8 kB 22.8 kB
Client Pages Modern
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_app.module.js 1.53 kB 1.53 kB
_app.module.js gzip 749 B 749 B
_error.module.js 10.5 kB 10.5 kB
_error.module.js gzip 4.26 kB 4.26 kB
hooks.module.js 1.52 kB 1.52 kB
hooks.module.js gzip 792 B 792 B
index.module.js 292 B 292 B
index.module.js gzip 223 B 223 B
link.module.js 5.47 kB 5.47 kB
link.module.js gzip 2.46 kB 2.46 kB
routerDirect.module.js 399 B 399 B
routerDirect..dule.js gzip 285 B 285 B
withRouter.module.js 409 B 409 B
withRouter.m..dule.js gzip 282 B 282 B
Overall change 20.1 kB 20.1 kB
Client Build Manifests
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_buildManifest.js 244 B 244 B
_buildManifest.js gzip 200 B 200 B
_buildManifest.module.js 251 B 251 B
_buildManife..dule.js gzip 209 B 209 B
Overall change 495 B 495 B
Serverless bundles
zeit/next.js canary flybayer/next.js fix-react-native-web Change
_error.js 241 kB 241 kB
_error.js gzip 64.3 kB 64.3 kB
hooks.html 4.08 kB 4.08 kB
hooks.html gzip 1.03 kB 1.03 kB
index.js 241 kB 241 kB
index.js gzip 64.4 kB 64.4 kB
link.js 273 kB 273 kB
link.js gzip 73.4 kB 73.4 kB
routerDirect.js 267 kB 267 kB
routerDirect.js gzip 71.4 kB 71.4 kB
withRouter.js 266 kB 266 kB
withRouter.js gzip 71.5 kB 71.5 kB
Overall change 1.29 MB 1.29 MB

Commit: a1a750b

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Next.js 8 and RNW Transpilation Issues
6 participants