question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-primitives is not transpiling when bundling for react-native production

See original GitHub issue

In my react-native project

node ./node_modules/react-native/local-cli/cli.js bundle --entry-file index.ios.js --platform ios --dev false --reset-cache --bundle-output /tmp/main.jsbundle --assets-dest /tmp/
**TypeError: /Users/peterp/Personal/xxx/node_modules/react-primitives/lib/modules/Platform.js: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "StringLiteral"**
transform[stderr]:     at Object.validate (/Users/peterp/Personal/xxx/node_modules/babel-types/lib/definitions/index.js:109:13)
transform[stderr]:     at Object.validate (/Users/peterp/Personal/xxx/node_modules/babel-types/lib/index.js:505:9)
transform[stderr]:     at NodePath._replaceWith (/Users/peterp/Personal/xxx/node_modules/babel-traverse/lib/path/replacement.js:176:7)
transform[stderr]:     at NodePath.replaceWith (/Users/peterp/Personal/xxx/node_modules/babel-traverse/lib/path/replacement.js:160:8)
transform[stderr]:     at PluginPass.MemberExpression (/Users/peterp/Personal/xxx/node_modules/metro-bundler/build/JSTransformer/worker/inline.js:126:14)

I’ve narrowed it down the assignment here: https://github.com/lelandrichardson/react-primitives/blob/master/src/modules/Platform.js#L13

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jevakalliocommented, Aug 23, 2017

Note, the string accessor fix works for me when applied to lib/modules/Platform.js (note lib, not src as linked by @peterp’s original post. The index.js imports directly from lib.)

The issue is caused by Metro bundler inlining Platform.OS declaration during the JS transform phase, as described in these tests, so that Platform.OS = platform.OS; becomes "ios" = platform.OS, which is obviously an invalid expression.

If you want the change to be patched in permanently until this gets fixed, I can recommend patch-package.

  1. Install patch-package with npm install --dev patch-package.

  2. Add prepare npm script:

scripts: {
  //snip...
  "prepare": "patch-package"
}
  1. Replace the dot notation property access with brackets in lib/modules/Platform.js (and in src as well, if you want to be cool about it):
-    Platform.OS = platform.OS;
-    Platform.OS = platform.Version;
+    Platform['OS'] = platform.OS;
+    Platform['Version'] = platform.Version;
  1. Run patch-package to generate patch:
npm run prepare -- react-primitives
  1. Commit the created patches/ folder to source control.

The patch should now be applied every time you npm install or yarn

2reactions
peterpcommented, Jul 17, 2017

@blargity I’ve created a very hacky release that you could test: npm install peterp/react-primitives.

It fixes the touchable and this bundling issue. I’m using this on react-native 0.46.3. If it does work, then please do not use this long term… It’s a stopgap whilst I was trying to figure this out.

I’m fundamentally interested in sharing a design system between react-dom and react-native. It feels like we’re so close!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object Dot notation doesn't transpile when bundling ... - GitHub
When bundling for production I'm getting an error when using dot notation over bracket notation whilst mutating an object:.
Read more >
Drastically Faster Bundling in React Native | by Evan Bacon
In Expo CLI, we configure Metro to transpile our untransformed Node modules with Sucrase, resulting in substantially faster React Native ...
Read more >
Tree Shaking React Native Apps - Klarna Engineering
It all comes down to ensuring that you don't transpile ES modules to CommonJS before Webpack bundles all modules. If you're using the...
Read more >
Why can't React Native (RN) convert the main.bundle.js to ...
I was wondering why do we need the RN Bridge in the React Native engine which connects the Javascript VM (JavascriptCore) with the...
Read more >
Vite 3.0 vs. Create React App: Comparison and migration guide
Learn how to build React applications with Vite and how to migrate from Create React App to Vite. Compare the core concepts of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found