Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run SystemJS/React demo locally with JSX?

I'm following this video tutorial at the moment and I'm stuck with the simple HelloWorld App. At the time position 12m:31s is where I'm stuck it should show HelloWorld but it doesn't.

The App is using SystemJs, React and JSX.

To create the app do these steps in your terminal (node and jspm required):

  • npm init (enter to all)
  • jspm init (enter to almost all, except use babel)
  • jspm install fetch=npm:whatwg-fetch
  • jspm install react
  • create an app sub-folder create main.js and copy my code into it
  • create index.html into root dir.
  • Then run it with serve

I think the problem is my local server. I'm running it with nodejs http-server and I think the JSX is not transpiled to JS. Also the mentioned serve server is not working.

I'm getting this error message in the browser console:

Potentially unhandled rejection [3] SyntaxError: Error loading "app/main"
[...] Unexpected token <

How do I make it work?

Here is my code, exactly the code from the video (it doesn't run here because no js files added):

//app/main.js
import 'fetch';

import React from 'react';

console.log('Hello world');

class HelloWorld extends React.Component {
	render() {
		return <p>hello world</p>;
	}
}

React.render(<HelloWorld />, document.body);
<!-- index.html -->
<!doctype html>
<html>
<head>
	<title>First jspm</title>
	<script type="text/javascript" src="jspm_packages/system.js"></script>
	<script type="text/javascript" src="config.js"></script>
	<script>
		System.import('app/main');
	</script>
</head>
<body>

</body>
</html>
like image 396
AWolf Avatar asked May 14 '15 16:05

AWolf


2 Answers

get similar issue when i work on a es6+react demo on browser, figure out finally

<!doctype html>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.20.12/system.js"></script>

<script>
SystemJS.config({
    baseURL:'https://unpkg.com/',
    defaultExtension: true,
    meta: {
        '*.jsx': {
            'babelOptions': {
                react: true
            }
        }
    },
    map: {
        'plugin-babel': 'systemjs-plugin-babel@latest/plugin-babel.js',
        'systemjs-babel-build': 'systemjs-plugin-babel@latest/systemjs-babel-browser.js',
        'react': '[email protected]/dist/react.min.js',
        'react-dom': '[email protected]/dist/react-dom.min.js'
    },
    transpiler: 'plugin-babel'
});


SystemJS.import('./comp-a.jsx').then(function (m) {
    console.log(m);
});
</script>
like image 191
aufula Avatar answered Oct 06 '22 00:10

aufula


Ok, I've figured it out.

It was mentioned in the video tutorial but I thought it is not needed. Anyway, it is required.

Add blacklist: [] to the configuration of babelconfig inside of config.js!!

From Babel homepage:

JSX support is currently disabled by jspm. To re-enable it, add "blacklist": [] to babelOptions in the jspm configuration file.

With this added Babel will automatically check the imports / exports and transpils the jsx.

like image 39
AWolf Avatar answered Oct 05 '22 23:10

AWolf