如何在不使用Create-react-app的情况下创建React应用程序

9
我是一名React初学者,我已经使用create-react-app制作了几个应用程序,以帮助我入门(按照许多教程的指导)。我听说现在需要知道如何从头开始创建一个React应用程序,而不使用create-react-app样板文件生成器。
我知道我必须添加一些部分,例如:
- React - React-dom - Webpack - Babel
以下是我的package.json文件中依赖项列表。
"dependencies": {
    "prop-types": "^15.6.1",
    "react": "^16.4.0",
    "react-dom": "^16.4.0"
  },
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^3.8.3",
    "webpack-dev-server": "^3.1.4"
  }
}

webpack.config.js

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'cheap-module-eval-source-map',
    entry: {
        app:'./src/index',
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/static/'
    },
    plugins: [
        new HtmlWebpackPlugin(['app'])
    ],
    module: {
        loaders: [{
            test:/\.js/ + /\.jsx?$/, 
            loader: 'babel-loader',
            include: path.join(__dirname, 'src'),
        }]
    }
}

我的 .babelrc 文件

{
    "presents":["env","react"]
}

我曾经跟随一些关于如何从头开始制作React应用的教程,但都没有成功。我觉得我的Webpack配置不正确。 不幸的是,我仍然无法运行这个项目。
代码库的Git链接: https://github.com/tony2tones/scratch-react/tree/master

你正在使用哪个版本的Webpack? - PlayMa256
4.9.1. 尽管我被建议使用版本3,但我不太确定这是否会加重我的问题。 - tony2tones
“我听说现在需要知道如何在没有使用create-react-app样板制造程序的情况下从头开始创建React应用”,你是在哪里听到的呢?使用create-react-app来构建“严肃”的网站没有任何问题。 - Patrick Hund
请查看此教程,了解如何在不使用create-react-app的情况下设置React v17项目:https://frontendguruji.com/blog/how-to-setup-a-react-js-project-from-scratch-without-create-react-app/ - Mandeep Pasbola
3个回答

4

如前所述,你需要一个

index.js

在其中,您至少需要包含以下内容:import React from 'react';

import ReactDOM from 'react-dom';

并且

`ReactDOM.render(whatever you want to render, wherever you want to render(preferably document.getElementById('root')))

这显示了应用程序在收到请求时应呈现的内容。 希望这能有所帮助。这是我的第一个答案 :)

0

看起来你缺少一些东西。

  1. 构建 你的项目,你可以调用 npm run build
  2. 你需要添加一个 index.html 文件以在浏览器中呈现。
  3. 你需要在 package.json 中的脚本对象中添加 start 并再次执行 npm install

对于第三点,你可以添加 "start": "webpack-dev-server"。 然后运行你的 node 服务器,你可以通过 npm run start 来实现。

希望这能帮到你。


-1
基本上,Create-react-app 抽象了 babelwebpack 的设置。如果你想直接设置这些东西并理解每个部分的作用,我建议阅读this blog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接