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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoinstalling react: Unexpected token #2519

Closed
mischnic opened this issue Jan 7, 2019 · 10 comments
Closed

Autoinstalling react: Unexpected token #2519

mischnic opened this issue Jan 7, 2019 · 10 comments

Comments

@mischnic
Copy link
Member

mischnic commented Jan 7, 2019

馃悰 bug report

Autoinstalling react doesn't work in a "completely clean" project.

馃帥 Configuration (.babelrc, package.json, cli command)

No babelrc, no package.json

馃 Expected Behavior

Parcel should install react, react-dom and then bundle.

馃槸 Current Behavior

Parcel doesn't recognize the JSX syntax?

馃毃  ...../index.js:5:1: Unexpected token (5:1)
  3 |
  4 | const MyComponent = () => (
> 5 | 	<button>Hello</button>
    | ^
  6 | );
  7 |
  8 | ReactDOM.render(<MyComponent />, document.getElementById("root"));

馃拋 Possible Solution

Workaround: install react as a dependency. Running parcel index.html will then corretly install react-domand build successfully.

馃捇 Code Sample

index.js

import React from "react";
import ReactDOM from "react-dom";

const MyComponent = () => (
	<button>Hello</button>
);

ReactDOM.render(<MyComponent />, document.getElementById("root"));

Command:

parcel index.js

馃實 Your Environment

Software Version(s)
Parcel 38df513
Node 11.6.0
npm/Yarn
Operating System macOS 10.13.6
@mischnic mischnic changed the title Autoinstall of react Autoinstalling react Jan 7, 2019
@mischnic mischnic changed the title Autoinstalling react Autoinstalling react: Unexpected token Jan 7, 2019
@lustoykov
Copy link
Contributor

I think if it was index.jsx as opposed to index.js it should work. Having that in mind - looks ok to me?

@DeMoorJasper
Copy link
Member

@lustoykov this should also work with .js

Sent with GitHawk

@lustoykov
Copy link
Contributor

For the protocol, using a normal .js extension, without JSX syntax, works fine too.

import React from 'react';
import ReactDOM from 'react-dom';

const MyComponent = () => (
  React.createElement('div', null, 'Hello')
);

ReactDOM.render(
  React.createElement(MyComponent, null, null), 
  document.getElementById('root')
);

@lustoykov
Copy link
Contributor

lustoykov commented Jan 10, 2019

I think babel is not even able to generate AST currently because the condition to add @babel/plugin-transform-react-jsx fails.

if (pragma || JSX_EXTENSIONS[path.extname(asset.name)]) {

So one possible (but pretty ugly) solution would be to add an additional check if the contents of the asset include ...from 'react/preact/etc' or ...require('react/preact/etc') to the condition above.

This would enable jsx compilation. If I interpret correctly, as a result, babel would be able to generate AST, parcel collect dependencies and set the correct pragma.

Do you see some better options?

@mischnic
Copy link
Member Author

Do you see some better options?

Always using @babel/plugin-syntax-jsx ?

@DeMoorJasper
Copy link
Member

Dependencies have been collected before transform runs, so in theory the jsx check could check if react or preact is a dependency and enable the correct babel plugin.

This would probably even make the jsx extension check obsolete as react should be imported on every file using jsx afaik

@lustoykov
Copy link
Contributor

lustoykov commented Jan 11, 2019

@DeMoorJasper,

Dependencies have been collected before transform runs

my bad, that's correct, however parcel fails on pretransform due to missing plugin, not on transform. Since collectDependencies is a visitor, it relies on AST being generated (but babel ast generation fails).


Here is what I see - an asset starts with process()

There we can see in order

await this.pretransform(); // Fails, See (*)
await this.getDependencies(); // Only then parcel would collects dependencies
await this.transform();

pretransform() // fails due to missing plugin (*) 馃憞

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jan 11, 2019

@lustoykov you're right forgot for a second Babel can't create an ast with jsx by default.

Than I'm not sure how to fix this, besides from catching errors and retrying after the install finished which parcel should kinda already though.
Perhaps it doesn't reload the package.json?

Should be a fairly easy fix I think

@mischnic
Copy link
Member Author

The PR added autoinstall (that part works), but on the invocation where react is installed, several similar errors are printed (console and server):

馃毃  /Users/niklas/.../react/Users/niklas/.../react/node_modules/react/index.js: ENOENT: no such file or directory, open '/Users/niklas/.../react/Users/niklas/.../react/node_modules/react/index.js'
Error: ENOENT: no such file or directory, open '/Users/niklas/.../react/Users/niklas/.../react/node_modules/react/index.js'

The path is somehow duplicated.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

@github-actions github-actions bot added the Stale Inactive issues label Jan 17, 2020
@github-actions github-actions bot closed this as completed Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants