【问题标题】:Remove server side from boilerplate (React) project从样板(React)项目中删除服务器端
【发布时间】:2018-12-17 08:55:48
【问题描述】:

我已经克隆并安装了https://github.com/react-boilerplate/react-boilerplate 项目,但我只需要项目的客户端,因为我将使用另一个已经创建的服务器(快递)。 这是我的 package.json

<code>


  "scripts": {  enter code here
       "analyze:clean":"rimraf stats.json",
       "preanalyze":"npm run analyze:clean",
       "analyze":"node ./internals/scripts/analyze.js",
       "extract-intl":"node ./internals/scripts/extract-intl.js",
       "npmcheckversion":"node ./internals/scripts/npmcheckversion.js",
       "preinstall":"npm run npmcheckversion",
       "prebuild":"npm run build:clean",
       "build":"cross-env NODE_ENV=production webpack --config internals/webpack/webpack.prod.babel.js --color -p --progress --hide-modules --display-optimization-bailout",
       "build:clean":"rimraf ./build",
       "start":"cross-env NODE_ENV=development node server",
       "start:tunnel":"cross-env NODE_ENV=development ENABLE_TUNNEL=true node server",
       "start:production":"npm run test && npm run build && npm run start:prod",
       "start:prod":"cross-env NODE_ENV=production node server",
       "presetup":"npm i chalk shelljs"
    }
</code>    

这是我的项目文件结构:[在此处输入图像描述][1]

  [1]: https://i.stack.imgur.com/oEZuw.jpg

【问题讨论】:

    标签: javascript node.js reactjs express react-redux


    【解决方案1】:

    如果您只需要从头开始一个前端 React 项目,您可以使用 ,create-react-app 工具创建一个。

    如果是,请使用这样的工具

    yarn create react-app my-app

    【讨论】:

      【解决方案2】:

      将此代码用于 Package.json。这适用于带有 webpack、Redux、babel 的简单 CSR React 项目。

      {
        "name": "startup",
        "version": "0.1.0",
        "private": true,
        "homepage": "http://localhost:3000",
        "dependencies": {
          "@babel/polyfill": "^7.7.0",
          "axios": "^0.19.0",
          "babel-plugin-transform-class-properties": "^6.24.1",
          "env-cmd": "^10.0.1",
          "prop-types": "^15.7.2",
          "react": "^16.8.6",
          "react-data-components": "^1.2.0",
          "react-dom": "^16.8.6",
          "react-hot-loader": "^4.12.18",
          "react-redux": "^7.1.0",
          "react-router-dom": "^5.0.1",
          "react-scripts": "3.0.1",
          "redux": "^4.0.4",
          "redux-logger": "^3.0.6",
          "redux-saga": "^1.1.3",
          "styled-components": "^4.4.1"
        },
        "devDependencies": {
          "babel-cli": "^6.26.0",
          "babel-core": "^6.26.3",
          "babel-loader": "^8.0.6",
          "babel-plugin-lodash": "^3.3.2",
          "babel-plugin-react-transform": "^3.0.0",
          "babel-plugin-transform-object-rest-spread": "^6.26.0",
          "babel-plugin-transform-react-jsx": "^6.24.1",
          "babel-preset-env": "^1.7.0",
          "babel-preset-react": "^6.24.1",
          "clean-webpack-plugin": "^3.0.0",
          "html-webpack-plugin": "^3.2.0",
          "prettier": "1.19.1",
          "webpack": "^4.41.2",
          "webpack-cli": "^3.3.10",
          "webpack-dev-server": "^3.9.0"
        },
        "scripts": {
          "dev": "webpack-dev-server --mode development --open --hot",
          "build": "webpack --mode production"
        },
        "eslintConfig": {
          "extends": "react-app"
        },
        "browserslist": {
          "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
          ],
          "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
          ]
        }
      }
      

      并使用它来配置您的 webpack,将以下代码添加到您的新/现有 webpack.config.js 以运行您的 prod-build 和 dev(localhost) 代码。

      const path = require('path');
      const HWP = require('html-webpack-plugin');
      const webpack = require('webpack');
      require("@babel/polyfill");
      var node_modules_dir = path.resolve(__dirname, 'node_modules');
      const { CleanWebpackPlugin } = require('clean-webpack-plugin');
      
      module.exports = {
          entry: ['babel-polyfill', './src/index.js'],
          output: {
              filename: 'build.js',
              path: path.join(__dirname, '/dist/')
          },
          optimization: {
              splitChunks: {
                // include all types of chunks
                chunks: 'all'
              }
          },
          module: {
              rules: [{
                  test: /\.(js|jsx)$/,
                  exclude: node_modules_dir,
                  loader: 'babel-loader',
                  query: {                    
                      plugins: ['transform-class-properties']
                  }
              }, {
                  test: /\.*css$/,
                  use: ['style-loader', 'css-loader']
      
              }, {
                  test: /.(png|jpe?g|gif|svg|ico)$/,
                  use: 'file-loader?name=assets/images/[hash].[ext]'
              }, {
                  test: /.(woff|woff2|otf|ttf|eot)$/,
                  use: 'file-loader?name=assets/fonts/[hash].[ext]'
              }]
          },
          devServer: {
              publicPath: '/',
              historyApiFallback: true,
          },
          plugins: [
              new CleanWebpackPlugin(),
              new webpack.ProgressPlugin(),
              new HWP({
                  template: './src/index.html',
                  filename: 'index.html'
              })
          ]
      }
      

      【讨论】:

      • 请在您的代码中添加说明:它是做什么的,它是如何工作的,它是如何解决 OPs 问题的。仅代码答案可能会导致cargo cult programming,并且您可能会被否决。
      • 好的,先生!谢谢你让我知道。对于您的好消息,没有什么大不了的。我认为对这个堆栈有基本知识的人可以很容易地理解我分享的配置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多