简体   繁体   English

反应 js 和现有的 Laravel 项目

[英]react js and existing Laravel project

I've searched internet and no where I've found a complete solution for what I'm trying to achieve.我在互联网上搜索过,但没有找到我想要实现的完整解决方案。 I'm trying to:我正在尝试:

  1. Add react related modules to existing Laravel project.将 react 相关模块添加到现有的 Laravel 项目中。
  2. Use existing gulp file to make all react related code available to my Laravel blade layouts.使用现有的 gulp 文件使所有与响应相关的代码可用于我的 Laravel 刀片布局。
  3. Then put JSX code to my Laravel's public folder and use those components in the blade templates.然后将 JSX 代码放到我的 Laravel 的公共文件夹中,并在刀片模板中使用这些组件。

Here's how the structure of the existing gulp.js file looks like:现有gulp.js文件的结构如下所示:

var elixir = require('laravel-elixir');

elixir(function (mix) {
    var cssFilesList = [
        ...
    ];

    var jsFooterFilesList = [
        ...
    ];

    var afterLoginjsFilesList = [
        ...
    ];

    elixir.config.assetsDir = 'custom/path';

    mix.styles(...);
    mix.scripts(...).scripts(more...);
    mix.version(...all of the above...)
});

I've tried to follow different tutorials like: this tutorial and this one我尝试遵循不同的教程,例如: 本教程教程

which more or less suggested me to update react related dependencies using npm , which I did.这或多或少建议我使用npm更新反应相关的依赖项,我做到了。 My package.json looks like:我的package.json看起来像:

{
  "private": true,
  "devDependencies": {
    "gulp": "^3.8.8"
  },
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "bootstrap-sass": "^3.0.0",
    "express": "^4.15.3",
    "ioredis": "^3.0.0",
    "laravel-elixir": "^2.0.0",
    "laravel-elixir-webpack-official": "^1.0.10",
    "laravel-elixir-webpack-react": "^1.0.1",
    "react": "^16.1.1",
    "react-dom": "^16.1.1",
    "require-dir": "^0.3.2",
    "socket.io": "^2.0.1"
  }
}

Now that the react related dependencies are installed, I've changed my gulp.js file so that it looks like the following:现在安装了与 react 相关的依赖项,我已经更改了我的gulp.js文件,使其看起来如下所示:

var elixir = require('laravel-elixir');
require('laravel-elixir-webpack-official');

// configure webpack
Elixir.webpack.mergeConfig({
    babel: {
        presets: [
            'react',
            'es2015',
            'stage-0',
        ]
    }
})

elixir(function (mix) {
    var cssFilesList = [
        ...
    ];

    var jsFooterFilesList = [
        ...
    ];

    var afterLoginjsFilesList = [
        ...
    ];

    elixir.config.assetsDir = 'custom/path';

    mix.webpack('reactcode.js');

    mix.styles(...);
    mix.scripts(...).scripts(more...);
    mix.version(...all of the above...)
});

Now I'm trying to compile this by:现在我正在尝试通过以下方式编译它:

gulp

Which throws two of the following errors depending on what I'm including on the top of the gulp file.根据我在 gulp 文件顶部包含的内容,会引发以下两个错误。 If I include the line:如果我包括这一行:

require('laravel-elixir-webpack-official');

It says:它说:

ReferenceError: Elixir is not defined
    at Object.<anonymous> (/path_to_project/node_modules/laravel-elixir-webpack-official/dist/WebpackTask.js:110:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/path_to_project/node_modules/laravel-elixir-webpack-official/dist/index.js:5:20)
    at Module._compile (module.js:570:32)

If I include the line:如果我包括这一行:

require('laravel-elixir-webpack-react');

It says:它说:

/path_to_project/node_modules/laravel-elixir-webpack-react/main.js:3
Elixir.ready(function() {
       ^

TypeError: Elixir.ready is not a function
    at Object.<anonymous> (/path_to_project/node_modules/laravel-elixir-webpack-react/main.js:3:8)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/path_to_project/gulpfile.js:4:1)
    at Module._compile (module.js:570:32)

All of these attempts were sort of hit and trial approaches as suggested by the above stated links.正如上述链接所建议的那样,所有这些尝试都是一种命中和试验方法。

Does anyone know a step by step method of exactly how this can be done?有谁知道具体如何做到这一点的分步方法? Any link or resource.任何链接或资源。 Also if anyone can rectify what I might be doing wrong.另外,如果有人可以纠正我可能做错的事情。

Any help is greatly appreciated.任何帮助是极大的赞赏。

EDIT 2021编辑 2021

Now that I have been working with React over the last 3.5 years, I believe I can answer my own question.现在我在过去的 3.5 年里一直在使用 React,我相信我可以回答我自己的问题。 In fact the question (or approach) itself is wrong.事实上,问题(或方法)本身是错误的。

I should have bundled React code using Webpack.我应该使用 Webpack 捆绑 React 代码。 Here are the steps -以下是步骤——

  1. Create a folder for your React sub project inside the Laravel directory, you can organize it as you wish.在 Laravel 目录下为你的 React 子项目创建一个文件夹,你可以随意组织它。

  2. Setup React with Webpack (out of scope for this edit), there are 100s of tutorials on net.使用 Webpack 设置 React(超出此编辑的范围),网上有 100 多个教程。

  3. In development mode spin up dev server from Webpack.在开发模式下,从 Webpack 启动开发服务器。 Typically localhost:8080 .通常localhost:8080 Front end will communicate to the Laravel base using API/Json bridge.前端将使用 API/Json 桥接器与 Laravel 基础进行通信。

  4. Once you are satisfied with the dev version, generate production version of the React code.一旦您对开发版本感到满意,请生成 React 代码的生产版本。 This will spit out JS bundles, CSS bundles and assets in the path you configured.这将在您配置的路径中吐出 JS 包、CSS 包和资产。

  5. Pull these bundles in your normal Laravel template and boom - you have your React app running with the main framework being Laravel.将这些包拉到你的普通 Laravel 模板中,然后繁荣 - 你的 React 应用程序在主要框架是 Laravel 的情况下运行。

SOLVED解决了

It eventually worked with the version of gulp.js where I had:它最终与我拥有的 gulp.js 版本一起使用:

require('laravel-elixir-webpack-official');

This was initially throwing error like:这最初是抛出错误,如:

ReferenceError: Elixir is not defined

I upgraded the package laravel-elixir to 6.0.x, such that now it looks like the following in my package.json:我将包laravel-elixir升级到 6.0.x,现在它在我的 package.json 中看起来如下所示:

"laravel-elixir": "^6.0.0-0"

To produce a compiled JS file which includes React related code and your components, add something like the following to your gulp file:要生成包含 React 相关代码和组件的已编译 JS 文件,请将如下内容添加到您的 gulp 文件中:

mix.webpack('comp1.js', 'destination/to/public/directory', 'source/of/your/components/');

Please note I kept the source JS file name as comp1.js which had my react component code.请注意,我将源 JS 文件名保留为comp1.js ,其中包含我的反应组件代码。 I'll experiment soon to see if they could have different names.我很快就会试验一下,看看它们是否可以有不同的名字。

Compile from the root of the project using gulp .使用gulp从项目的根目录编译。 This will generate your compiled file, which when included in your blade layout or view will let you render your component in your Laravel page.这将生成您的编译文件,当包含在您的刀片布局或视图中时,您将可以在 Laravel 页面中呈现您的组件。

And there we go, I now have a way to use React inside Laravel.好了,现在我有了在 Laravel 中使用 React 的方法。

Notes笔记

My npm version is:我的 npm 版本是:

3.10.10

And node version is:节点版本是:

6.12.0

Add this because I had issues updating packages when those versions were different, particularly when node was not LTS.添加这个是因为当这些版本不同时我在更新包时遇到问题,特别是当 node 不是 LTS 时。 Hope this helps.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM