Using .env constants with Laravel Mix and React - laravel

I'm trying to integrate an existing React project with Laravel. I'm using Laravel Mix to compile the assets.
I managed to get everything going, besides one thing: I do not know how to make Mix read constants from the .env file, which are needed for the Webpack build process.
I'm using Firebase, and the various keys are kept in this .env file. Of course i could work around it, but i would like to make it properly.
I do not see anything in the webpack.mix.js file that implies this functionality. How can it be done?

You can prefix your keys with MIX_ and access them via process.env once you recompile your js.
So having mix.js('resources/assets/js/app.js', 'js'); or mix.react('resources/assets/js/app.js', 'js'); will work fine.
You will also need to restart any running watch tasks for changes to take effect.
https://github.com/JeffreyWay/laravel-mix/issues/1096#issuecomment-320328087
https://laravel.com/docs/5.4/mix#environment-variables

Related

Vue files on Laravel does not reflect changes after pulling changes from repository on production server

im workin with LARAVEL and vue. i'have launched a project on digital ocean using ubuntun and nginx. Since the launch i'have add more features. Today i pulled those change on the production server, but they do not appear on the browser..
Using nano i'have chechked all the files, and yes, the files have changed but they do not reflect on the browser.
I'have used
npm run dev, npm run watch
php artisan config:cache, php artisan cache:clear but yet it doesnt seems to work
any idea ?
Before you push your updated Vue files you need to run npm run prod to prepare your assets as production ready.
Laravel uses mix to compile assets: https://laravel.com/docs/8.x/mix
Now you have your freshly compiled assets in your public/ directory, therefore you can pull to digitalocean machine. But now the browser might still be using cached asset (if name stays same, browser doesn't fetch same asset again for a while)
So, Mix Versioning comes to help: https://laravel.com/docs/8.x/mix#versioning-and-cache-busting
In your webmack.mix.js file you need to add .version() at the end of mix piping. End result will be something like this;
mix.js('resources/js/app.js', 'public/js')
.version();
As you want to use versioning, now you need to resolve asset urls with mix(...) instead of asset(...) in your blade files;
<script src="{{ mix('/js/app.js') }}"></script>
Now whenever you compile your assets, mix will assign them new version numbers and will at them to end of assets' urls in your blade. Browser will understand there is a update in file (because of a different version number) and will fetch updated asset.
Did you clear your browser's cache?
If you are working with Vue and/or Sass and therefore your files are changing from time to time, I suggest you to use mix() instead of asset() for cache busting. Otherwise all of your users will have to delete their cache (this is obv. not a good approach)
https://laravel.com/docs/8.x/mix#versioning-and-cache-busting

Live Edit in PhpStorm for Laravel projects

I like to use Live Edit feature, however, it seems to me it only works with regular .html files, however, none of the known to me ways work with Laravel .blade.php files. Google didn't answer.
Is there really no way to do so?
For Laravel projects, you can use BrowserSync to automatically refresh websites when you make changes to your template files. Support for BrowserSync comes out of the box with Laravel Mix. To use this feature, all you have to do is go to your webpack.mix.js file and add the mix.browserSync() function call. If you're using Laravel Valet or something similar to get pretty URLs, you can pass in a URL as an argument to proxy.
mix.browserSync();
// OR
mix.browserSync('my-domain.test');
Documentation: Compiling Assets (Mix)

React app on deployment server

I have an Ubuntu VPS with Laravel v5.6 served with nginx, and I am trying to use React on the front-end. I don't know if this is possible what I want to do is:
use node and npm to build my front-end app locally,
generate the CSS and JS files and upload those already generated files to my VPS server,
the output should not change since as far as I know npm generates at JS and CSS files with your whole code in it
Thanks for the help, and if there is any suggestion for a different approach just let me know.
You should start to use a frontend bundling solution like webpack or parcel, they do exactly the same
If you generate your app with create-react-app it comes with webpack built-in

Webpack getting started with Laravel. How to setup workflow?

I am currently using Webpack with Laravel to manage dependencies on a new project. My current workflow looks like this:
npm install whatever-package
require('whatever-package'); (in frontend.js)
GulpFile.JS >
elixir(mix => {
//the core scss file. contains bootstrap and vendor stuff.
mix.sass('frontend.scss')
.webpack('frontend.js');
}
run gulp
Frontend.js is then included in my PHP template. This would work great for a single page application, but if I want to have one javascript file, and multiple web pages what technique should I be using to pull in javascript for other pages?
Should I have multiple JS files all webpacked with Elixir? Or what is the next step to require in the right files?
i.e. does Webpack support a single entry point where I can start to load other scripts via data attributes? I've used this technique with requireJS previously, and just trying to wrap my head around the best way to do this with Webpack.

Load css/js files in Laravel 4 with Composer?

I'm working on creating a new project in Laravel 4. I have a tiny bit of experience in Laravel 3, in which I got used to the assets system. I'm now having a lot of trouble figuring out how to load CSS and JS files in the new system.
I believe I should be using Composer PHP, but I'm not sure how. Where do I put the actual .css and .js files? And then how do I load them with Composer to be utilized in the Laravel project?
I know there are outside plugins like Best Asset, etc. that are written to replicate the Assets system of Laravel 3, but I was hoping to understand the new system and not have to use an outside plugin.
Any help? Thanks.
You don't need Composer for your css/js assets. You can use assets pretty much the same as with Laravel 3
You can just place them in your public folder and reference them in your views. You can use plain html, or use the HtmlBuilder: HTML::style('style.css') and HTML::script('script.js')
You must take care of case sensitive.
This class must write with uppercase.
HTML::style('style.css');
HTML::script('script.js');
Laravel stylesheets and javascript don't load for non-base routes

Resources