


import React from "react"; function App(){ return( <div> <b>Hello world!</b> </div> ) } export default App; <div> element, which contains the text Hello world! in bold. And here is an example of code doing the same thing in which JSX is not used: "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _react = require("react"); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function App(props) { return _react2.default.createElement( "div", null, _react2.default.createElement( "b", null, "Hello world!" ) ); } exports.default = App; react-scratch folder using npm ( npm init ) and install some of the above packages with the following command: npm install --save-dev @babel/core @babel/preset-env @babel/preset-react babel-loader css-loader webpack webpack-cli style-loader webpack-dev-server index.js file, which is contained in the src folder located in the root directory of the project. The structure of this project is shown below. Some files and folders are created automatically, you will need to create some yourself.
component folder will contain the project components (in our case there is only one component here). The dist folder, in the main.js file, will contain the compiled code, and index.html is, as already mentioned, the main HTML file of our application.webpack.config.js . It is necessary to describe and export the object containing the settings. We will start setting up this file with a description of the object that looks like this (we will consider it in parts, and below we will give its full code): { entry: "./src/index.js", mode: "development", output: { filename: "./main.js" }, } entry property specifies the main file with the project source code. The value of the mode property indicates the type of environment to compile (in our case, the development environment is development ) and where to place the compiled file.index.html file of our project located in the dist folder: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>React From Scratch</title> </head> <body> <div id="root"> </div> </body> <script type="text/javascript" src="main.js"> </script> </html> script tag present in this file. It points to the main.js file, which will be obtained during the compilation of the project. We will use the <div> element with the root identifier to display the React application. npm install react react-dom index.js : import React, { Component } from "react"; import ReactDOM from "react-dom"; import App from "./component/app.component"; ReactDOM.render(<App />, document.querySelector("#root")); <div> with the root identifier.app.component.js file: import React, { Component } from "react"; import s from "./app.component.css"; class MyComponent extends Component { render() { return <div className={s.intro}>Hello World</div>; } } export default MyComponent; app.component.css file: .intro { background-color: yellow; } package.json file. We are satisfied with the last option. Add the following section to package.json : "babel": { "presets": [ "@babel/env", "@babel/react" ] } webpack.config.js ), they represent one of the properties of an object with settings exported by this file: module: { rules: [ { test: /\.m?js$/, exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader" } }, ] } rules property of the object presented here contains an array of rules, according to which the file specified by the regular expression described in the test property should be processed. In this case, the rule will apply to all files with the .m and .js node_modules and we don’t want to node_modules files from the node_modules and bower_components folders. Further, here we indicate that we want to use the babel-loader. After that, our JS-files will be processed first by Babel tools, and then packed using Webpack.rules array of the module object the settings for processing CSS files: module: { rules: [ { test: /\.m?js$/, exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader" } }, { test: /\.css$/, use: [ "style-loader", { loader: "css-loader", options: { modules: true } } ] }, ] } use property can accept an array of objects or strings. The loaders are called starting from the last, so our files will first be processed using the css-loader. We set up this tool by writing true modules property of the options object. Thanks to this, CSS styles will only be applied to the components into which they are imported. Css-loader will allow import commands in CSS files, after which the style-loader will add what you get, in the form of the style tag, in the <head> section of the page: <style> <-- ваш css --> </style> module settings object, describing the rules for processing static resources: module: { rules: [ { test: /\.m?js$/, exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader" } }, { test: /\.css$/, use: [ "style-loader", { loader: "css-loader", options: { modules: true } } ] }, { test: /\.(png|svg|jpg|gif)$/, use: ["file-loader"] } ] } webpack.config.js file, webpack.config.js 's configure the development server: devServer: { contentBase: path.join(__dirname, "dist"), compress: true, port: 9000, watchContentBase: true, progress: true }, contentBase property of the contentBase settings devServer points to the folder where our resources are located and the index.html file. The port property allows you to specify the port that the server will listen on. The watchContentBase property allows watchContentBase to monitor the changes of files in the folder specified by the contentBase property.webpack.config.js file: const path = require("path"); module.exports = { entry: "./src/index.js", mode: "development", output: { filename: "./main.js" }, devServer: { contentBase: path.join(__dirname, "dist"), compress: true, port: 9000, watchContentBase: true, progress: true }, module: { rules: [ { test: /\.m?js$/, exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader" } }, { test: /\.css$/, use: [ "style-loader", { loader: "css-loader", options: { modules: true } } ] }, { test: /\.(png|svg|jpg|gif)$/, use: ["file-loader"] } ] } }; package.json , to the scripts section, the command to start the development server and the command to start the project build: "scripts": { "dev": "webpack-dev-server", "start": "webpack" }, npm run dev 
npm run start index.html file in the browser and see the same thing that could be seen by launching the development server and going to http: // localhost: 9000 .Source: https://habr.com/ru/post/436886/