Dom is not detected error after transforming jsx to js

I created a jsx file as below

  /** @jsx dom */
(function(){
'use strict';

define([
        'jquery',
        'react',
        'react-dom'
    ],

    function($, React, ReactDOM){

        var AppView = React.createClass({
            render: function() {
                return <div>Hello World</div>;
            }
        });

        ReactDOM.render(<AppView />, document.getElementById('dsl-application'));

    });
})();

And converted jsx to js using below command

babel --watch src --out-dir bundle --preset react

And the source file looks below

(function () {
'use strict';

define(['jquery', 'react', 'react-dom'], function ($, React, ReactDOM) {

    var AppView = React.createClass({
        displayName: 'AppView',

        render: function () {
            return dom(
                'div',
                null,
                'Hello World'
            );
        }
    });


    ReactDOM.render(dom(AppView, null), document.getElementById('dsl-application'));
});
})();

The problem is that I got an Uncaught ReferenceError: dom not defined when running the js file in the browser. dom is added after the conversion. Any clue how to fix this?

+4
source share
2 answers

To convert JSX to JS in a browser, you need browser.jsfrom babel. For a simple example, see https://github.com/ruanyf/react-demos/blob/master/demo01/index.html

[[UPDATE BASED ON MORE INFORMATION]]

, npm install webpack, index.html .

index.html

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/javascript" src="bundle.js" charset="utf-8"></script>
  </body>
</html>

package.json

{
  "name": "another",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^0.14.7",
    "react-dom": "^0.14.7"
  },
  "devDependencies": {
    "babel-core": "^6.5.1",
    "babel-loader": "^6.2.2",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-react": "^6.5.0",
    "webpack": "^1.12.13",
    "webpack-dev-server": "^1.14.1"
  }
}

main.jsx

'use strict'

var React = require('react')
var ReactDOM = require('react-dom')
var AppView = require('./component.jsx')

ReactDOM.render(
    <AppView />,
    document.getElementById('example')
);

component.jsx

'use strict'

var React = require('react')

var AppView = React.createClass({
    render: function(){
        return(<h1>Hello, world!</h1>)
    }
});

module.exports = AppView

webpack.config.js

module.exports = {
    entry: './main.jsx',
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          exclude: /(node_modules|bower_components)/,
          loader: 'babel', // 'babel-loader' is also a legal name to reference
          query: {
            presets: ['react', 'es2015']
          }
        }
      ]
    }
}
0

React.

, webpack.config.js. :

plugins: [ 
    ["transform-react-jsx", {
        "pragma": "dom" 
    }]
]

babel default pragma - React.createElement(https://babeljs.io/docs/plugins/transform-react-jsx/).

, , / .

"dom", , babel-plugin-jsx-.

https://www.npmjs.com/package/babel-plugin-jsx-pragmatic .

, "", ( webpack.config.js, ): - webpack 2.6.1

...
module: {
    rules: [{
      //a regexp that tells webpack to use the following loaders on all
      //.js and .jsx files
      test: /\.js$/,
      loader: 'babel-loader',
      options: {
        babelrc: false,
        presets: ["es2016", "react", "stage-0"],
        plugins: [
          ["transform-react-jsx"]
        ]
      },
      //we definitely don't want babel to transpile all the files in
      //node_modules. That would take a long time.
      exclude: /node_modules/
    },

...
0

Source: https://habr.com/ru/post/1627884/


All Articles