Video/mp4 not loading using Webpack, Laravel Mix in Laravel 8 & reactjs - php

I'm new to Laravel and I'm trying to compile and upload my resources of video using Laravel Mix.
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.react()
.sass('resources/sass/app.scss', 'public/css');
And this is the code frontend with the react
import React from 'react';
import Video from '../../videos/video.mp4';
import {HeroContainer, HeroBg, VideoBg} from './HeroElements';
const HeroSection = () => {
return (
<HeroContainer id="home">
<HeroBg>
<VideoBg autoPlay loop muted src={Video} type='video/mp4' />
</HeroBg>
</HeroContainer>
)
}
export default HeroSection;
And this is the result of the output and error
Module parse failed: Unexpected character ' ' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

If you are experiencing the error in the screenshot below, try the following:
Install the raw_loader module:
npm install raw-loader --save-dev
In the webpack.mix.js file use the raw_loader module in a rule that captures the files you want:
const mix = require('laravel-mix');
mix.webpackConfig({ module: { rules: [{ test: /\.mp4$/i, use: 'raw-loader' }] } })
.ts('resources/ts/app.ts', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
For the regular expression to capture more file you can use it as:
/\.(mp4|avi|pdf|mp3)$/i
The error that occurred before using the raw_loader module:
Success using raw_loader module:
Ps: Looks like you're using React. In my example I used Vue.Js 3... but the problem doesn't seem to relate to that.

Related

Uncaught ReferenceError: Vue is not defined in Laravel

in laravel application using vue 3. when I try to load welcome.blade.php file it is not visible vue file content and console got following error Uncaught ReferenceError: Vue is not defined <anonymous> http://localhost:8000/js/app.js:37486 app.js code line 37486 is as var _Vue = Vue,
my app.js
require('./bootstrap');
const { createApp } = Vue;
Vue.component('mainapp', require('./components/mainapp.vue').dafault)
const app = createApp({
/* root component options */
});
app.mount('#app');
and welcome.blade.php
<html>
<body>
<div id="app">
<mainapp></mainapp>
</div>
</body>
<script src="{{mix('/js/app.js')}}"></script>
</html>
how could I fix this?
I need some answers to solve this prob
I Think you will need something like this in your app.js file:
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
import Vue from 'vue';
require('./bootstrap');
Vue.component('mainapp', require('./components/mainapp.vue').dafault)
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
const app = new Vue({
el: '#app'
});
Or do it in this way for Vue 3.0 +:
require('./bootstrap');
import { createApp } from 'vue';
import mainapp from './components/mainapp.vue';
createApp({
components: {
mainapp,
}
}).mount('#app');

reactJS , accessing URL with daynamic segments says 'page not found'

The issue is pretty obvious. I have declared a route with 2 dynamic parts in my react.js app with the following line :
<Route path='/products/:cat_id/:subcat_id' component={ProductsWise} />
But when I try to access the url: http://127.0.0.1:8000/products/4/1 , it simply says :
404 Not Found. The code follows :
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import ProductsWise from './ProductsWise'
class App extends Component {
render () {
return (
<BrowserRouter>
<div>
<Header />
<Switch>
<Route path='/products/:cat_id/:subcat_id' component={ProductsWise} />
</Switch>
</div>
</BrowserRouter>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'))
I am not putting the content inside ProductsWise.js here as I think that is not necessary to solve the problem.
How to get rid of the problem ?
P.S: I am actually building an app with PHP Laravel framework in server side and react.js in client side.
The problem is that the SPA is not rendered for the products/4/1 path from the backend perspective.
I believe Route::view('/{path?}', 'app') is not relevant.
As far as there is no information about wildcard routing in the official Laravel routing documentation - you can render your SPA for / and for /products/{catId}/{subcatId} paths separately:
Route::view('/', 'app');
Route::view('/products/{catId}/{subcatId}', 'app');

How can I Integrate Vue into a legacy PHP app and gain access to specific Vue components

I have a server-side-rendered legacy PHP 5.6 web app that I want to modernise using VueJS
Within the project structure I have set up a new Vue project and set up Vue Router and Webpack with an entry point of main.js
Main.js
Vue.config.productionTip = false
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router/router'
new Vue({
render: h => h(App),
router
}).$mount('#app')
I am currently inserting VueJS into the legacy app by inserting this code at the bottom of a php file:
<div id="app">
</div>
<script src="http://localhost:8080/js/main.js"></script>
The url of this page is https://www.example.com/user_portal/employee_holiday_info.php#/
However Vue Router recognises it as / because it is the point of entry.
At the moment I have to insert the above code on every PHP file that I want to use VueJS on. Is there a better way of doing this?
If I insert the above code at the bottom of say, index.php then Vue Router would also recognise that page as /. In which case both urls:
https://www.example.com/user_portal/employee_holiday_info.php#/
https://www.example.com/user_portal/index.phpwould render the same Vue component, HelloWorld.
Vue Router
import HelloWorld from "../components/HelloWorld";
Vue.use(Router)
const router = new Router({
routes: [
{
path: '/',
name: 'hello-world',
component: HelloWorld,
}
]
});
export default router;
How can I set this up so I can on multiple Vue components throughout a single php file and share data between them using Vuex.

FOSJsRoutingBundle integration with Symfony Flex [duplicate]

This question already has answers here:
Webpack FOSJsRoutingBundle integration with Symfony Flex and Angular
(2 answers)
Closed 4 years ago.
The issue is that I cannot get FOSJsRoutingBundle to work with Symfony Flex and Webpack.
I've been using this bundle in Symfony 3 for a long time and there's never been an issue but with the introduction of webpack, the setup has become more complex. Unfortunately the documentation for version 2.2 isn't clear.
You can find the current documentation for the bundle here: https://symfony.com/doc/master/bundles/FOSJsRoutingBundle/index.html
As you can see it recommends the following approach for Symfony Flex, so I'm writing this in code/assets/js/fos_js_routes.js:
const routes = require('../../public/js/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js';
Routing.setRoutingData(routes);
with a test route in: code/assets/js/fos_routing_test.js
Routing.generate('test_route');
and then including it in webpack.config.js as:
.addEntry('app_fos_routing', [
'./assets/js/fos_js_router.js'
])
.addEntry('app_fos_generate_routes', [
'./assets/js/fos_routing_test.js'
])
and my code/templates/index.html.twig:
{{ encore_entry_script_tags('app_fos_routing') }}
{{ encore_entry_script_tags('app_fos_generate_routes') }}
However when I implement this webpack creates the following which causes a reference error ReferenceError: Routing is not defined
/***/ "./assets/js/fos_js_router.js":
/*!************************************!*\
!*** ./assets/js/fos_js_router.js ***!
\************************************/
/*! no exports provided */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _vendor_friendsofsymfony_jsrouting_bundle_Resources_public_js_router_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js */ "./vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js");
/* harmony import */ var _vendor_friendsofsymfony_jsrouting_bundle_Resources_public_js_router_js__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_vendor_friendsofsymfony_jsrouting_bundle_Resources_public_js_router_js__WEBPACK_IMPORTED_MODULE_0__);
var routes = __webpack_require__(/*! ../../public/js/fos_js_routes.json */ "./public/js/fos_js_routes.json");
_vendor_friendsofsymfony_jsrouting_bundle_Resources_public_js_router_js__WEBPACK_IMPORTED_MODULE_0___default.a.setRoutingData(routes);
/***/ }),
I had the same issue in my Flex application. Here's the steps I took to make it work:
Download FOSJSRoutingBundle and make sure that the generated file in public/bundles/fosjsrouting/js/router.js looks like this.
Generate your routes with the command fos:js-routing:dump --format=json --target=public/assets/js/fos_js_routes.json
Create a JS file where you'll have to use the generated route.
test.js:
import Routing from '../../../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
const routes = require('../fos_js_routes.json'); //file with generated routes, created after executing the command above.
Routing.setRoutingData(routes);
Routing.generate('booking_data') //use generated route
//... the rest of your JS code
Make sure that those relative paths are correct.
Add your test.js to your webpack.config.js file:
webpack:
const Encore = require('#symfony/webpack-encore');
Encore
.setOutputPath('public/build')
.setPublicPath('/build')
.setManifestKeyPrefix('')
.splitEntryChunks()
.addEntry('js/test', './public/assets/js/test.js')// JAVASCRIPT
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.enableSassLoader()
.enablePostCssLoader()
.enableSingleRuntimeChunk()
;
module.exports = Encore.getWebpackConfig();
Add test.js to your template
twig:
{{ encore_entry_script_tags('js/test') }}
This is everything that I have done to make it work.
I suggest to change your import to
const routes = require('../../public/js/fos_js_routes.json');
const Routing = require('../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.js');
Routing.setRoutingData(routes);

Compiling assets subdirectories in Gulp with Laravel Elixir

I am working on what will end up being a very large application, which means my asssets/whatever folders are going to contain possibly a few hundred files (SCSS, js etc).
Obviously this will make managing them a little tricky, so I would like to arrange them into subfolders for each module of my application.
e.g. The SAP part of my application would have its style and js stored like the following:
resources/assets/js/SAP/myjs.js
resources/assets/sass/SAP/mysass.scss
It looks like in laravel-elixars config that you can only specify one assetsDir path:
var config = {
production: !! util.env.production,
srcDir: 'app',
assetsDir: 'resources/assets/',
cssOutput: 'public/css/_raw',
jsOutput: 'public/js/_raw',
bowerDir: 'vendor/bower_components',
tasks: [],
watchers: { default: {} },
duplicate: [],
concatenate: { css: [], js: [] }
};
It would be good if you could at least do the following:
...
assetsDir: ['resources/assets/SAP', 'resources/assets/Diary', ...],
...
Or even better, have it automatically map the paths for output based on the directories in the assets directory.
Found a solution to my question.
For the css part of it I can use SASS's #import in a master.scss file which just #imports all the scss files for various bits of my project.
I guess for the JS I would do something similar with requirejs(?).

Categories