Laravel (5.5) - Third party JS libraries in production version - laravel

I'm using third party JS libraries in my Laravel 5.5 project (leaflet and leaflet-draw). I installed the libraries using npm:
npm install leaflet
npm install leaflet-draw
I added the folowing lines in resources/assets/js/app.js:
require('leaflet');
require('leaflet-draw');
and in resources/assets/sass/app.scss:
#import "~leaflet/dist/leaflet.css";
#import "~leaflet-draw/dist/leaflet.draw.css";
after that, I run:
npm run dev
Assets compile and my application works fine on my localhost (I run basic laravel server with php artisan serve).
I tried deploying this application to server. I deployed it to a subfolder, so the address is:
https://example.com/laravelapplication
I followed this instruction:
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04
All routes and urls work fine. I don't get any code errors. But, leaflet and leaflet draw libraries have trouble fetching their icons and fonts. I get these errors:
GET https://example.com/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg?fd5728f... 404 (Not found)
GET https://example.com/images/vendor/leaflet/dist/images/layers.png?a613745... 404 (Not found)
I'm not sure, but it looks like the library is searching for fonts and icons in the wrong place. It should look in https://example.com/laravelapplication instead of https://example.com
Does anyone know why this is happening?

If you know the actual URL these images are located in once your app is deployed, then a workaround would be to simply hard-code these locations:
For Leaflet, you can use the L.Icon.Default's imagePath option: (in your JavaScript)
L.Icon.Default.prototype.options.imagePath =
"https://example.com/laravelapplication/images/vendor/leaflet/dist/images/";
For Leaflet.draw plugin, you have to override the CSS rules: (in your CSS, make sure it is evaluated after Leaflet.draw's CSS)
.leaflet-draw-toolbar a {
background-image: url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg');
background-image: linear-gradient(transparent, transparent), url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg');
}
.leaflet-retina .leaflet-draw-toolbar a {
background-image: url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet-2x.png');
background-image: linear-gradient(transparent, transparent), url('https://example.com/laravelapplication/fonts/vendor/leaflet-draw/dist/images/spritesheet.svg');
}

Related

Heroku not compiling webpack node dependences

I have a laravel app that compiles all assets in localhost, but in heroku it says app.js not found and app.css not found (It does not compile js, css and images assets in Heroku).
I have add the following line in AppService Provider
if($this->app->environment('production')) {
URL::forceScheme('https');
}
but it still does not work, and I do not understand why.
I have also add the env variable APP_FORCE_HTTPS but still does not work.
What could be the problem and how can i solve it?

How to use Bootstrap 4 icons in Laravel with VueJS

I have installed Bootstrap icons into a Laravel/VueJS application using NPM, according to the instructions here, https://icons.getbootstrap.com/. What is the next step?
If I want to use the svg element in a blade template, do I need to compile it with webpack? Do I import it into a css file?
And how do I use it in single file VueJS components?
I am able to install bootstrap icons version 1.5.0 to Laravel 8 project and using laravel mix, by following these steps.
run this to install bootstrap icons into your project
npm i bootstrap-icons
add this to resources\sass\app.scss
#import '~bootstrap-icons/font/bootstrap-icons';
check webpack.mix.js file has this
mix.sass('resources/sass/app.scss', 'public/css')
run this to compile your change to public folder
npm run dev
then you can use icons anywhere in the page (either .blade or .vue file)
<i class="bi-alarm"></i>
Happy coding!!!
There is an error in the answer of mili, because if
#import '~bootstrap-icons/font/bootstrap-icons';
is included in resources\sass\app.scss then no #font-face directive is included in my app.css file when building with npm run dev. To make appear the directive I had to add the «css» extension so the correct #import would be:
#import '~bootstrap-icons/font/bootstrap-icons.css';
But there is another problem, in this case a problem of the builder, that produces that the icons are not showed by the browser (instead appear an square meaning that the broswer could not render the svg icon). The builder generates the following directive in public/css/app.css
#font-face {
font-family: "bootstrap-icons";
src: url(/fonts/vendor/bootstrap-icons/bootstrap-icons.woff2?dfd0ea122577eb61795f175e0347fa2c) format("woff2"),
url(/fonts/vendor/bootstrap-icons/bootstrap-icons.woff?94eeade15e6b7fbed35b18ff32f0c112) format("woff");
}
Laravel does not find the files because url() does not understand the absolute path (in my development environment). If you add «..» before the two paths, then all works (the icons appear in the page):
#font-face {
font-family: "bootstrap-icons";
src: url(../fonts/vendor/bootstrap-icons/bootstrap-icons.woff2?dfd0ea122577eb61795f175e0347fa2c) format("woff2"),
url(../fonts/vendor/bootstrap-icons/bootstrap-icons.woff?94eeade15e6b7fbed35b18ff32f0c112) format("woff");
}
The problem is that every time that you run npm run dev for any other package, the ".." are automatically overwritten, so the icons are not seen any more.
I have seen other people out there with the same problem, but I do not know where should be notified the issue and the solution. I will try to find a feedback window in https://getbootstrap.com/
Install Bootstrap in your Node.js powered apps with the npm package:
Copynpm install bootstrap
require('bootstrap') will load all of Bootstrap’s jQuery plugins onto the jQuery object.
After that use npm install bootstrap-icons to install bootstrap icons onto your project and include it in your app.js from node-modules.
After that, if npm run dev runs successfully, Then add the SVG element in your Vue components.
<svg class="bi bi-chevron-right" width="32" height="32" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M6.646 3.646a.5.5 0 01.708 0l6 6a.5.5 0 010 .708l-6 6a.5.5 0 01-.708-.708L12.293 10 6.646 4.354a.5.5 0 010-.708z" clip-rule="evenodd"/></svg>
For demo purposes
Q> If I want to use the svg element in a blade template, do I need to compile it with webpack? Do I import it into a css file?
Ans.> No, you need not to compile while using in blade provided you import all the bootstrap files such as js files and css files.
For me the problem was fixed removing in the webpack on the sass instruction :
.options({ processCssUrls: false })
There is no need to declare a font face.

how to compile js to es5 with laravel mix?

I have laravel Vue app and it works perfectly with chrome and firefox. but it doesn't work on Edge or IE11 and the console shows error on arrow function!?
How to compile or transpile to es5 with laravel mix and webpack?
could you show the correct configuration for webpack.mix.js?
tnx alot
UPDATE February 2020
If anyone still need help with this, mix already provide a babel compilation to es5:
A slight variation of mix.scripts() is mix.babel(). Its method
signature is identical to scripts; however, the concatenated file will
receive Babel compilation, which translates any ES2015 code to vanilla
JavaScript that all browsers will understand.
You can use it like this:
mix.babel(['public/js/es6file.js'], 'public/js/app.es5.js')
DOCS
In order to compile your es6 code to es5 follow the following steps:
1) install the babel-env preset
npm install #babel/preset-env --save
And then declare it in your .babelrc in the root directory:
{
"presets": ["#babel/preset-env"]
}
2) compile your code using
npm run dev //for dev environment
or
npm run prod // for production environment
after a lot of search, I've found out that this Vuecomponent causes the error "https://github.com/godbasin/vue-select2" how can I compile it to es5.
the edge console error:
Expected identifier, string or number
and the corresponding line that it shows is this:
setOption(val = []) {
this.select2.empty();
this.select2.select2({
-----> ...this.settings,
data: val
});
this.setValue(this.value);
},
sorry for taking your time

Laravel 5.4 - Bootstrap glyphicons don't work after sass integration

Well, I just installed fresh Laravel 5.4. Then installed npm and decided first time to use webpack instead of gulp.js. As you know, Laravel default provides sass Bootstrap integration. Used this command to generate my css from sass.
npm run dev
Bootstrap, Jquery worked perfect, but Glyphicons weren't displayed. Checking my public/css/app.css I saw, that Glyphicons font-face path are not suitable.
src: url(/fonts/glyphicons-halflings-regular.eot?f4769f9bdb7466be65088239c12046d1);
If I, manually use ../fonts instead of /fonts it will work. I tried to figure out and edit the default path. In _variables.css I set:
$icon-font-path = "../fonts" - but npm gives error.
By default it is: "~bootstrap-sass/assets/fonts/bootstrap/"
Copied fonts folder inside puclic/css folder, didn't work.
Added options to the webpack.mix.js file:
options({processCssUrls: false})
and in _variables.css again set:
$icon-font-path = "../fonts"
Run npm-run-dev and it worked, glyphicons were displayed. But, I don't want to set false for processCssUrls. Because, in this case I will not able to minimize css files using command: npm run production.
Also, I followed this question, but couldn't find any answer, all solutions didn't work.
glyphicons not showing with sass bootstrap integration
Finally, found the solution. In webpack.config.js set:
publicPath: '../'
instead of Mix.resourceRoot
{
test: /\.(woff2?|ttf|eot|svg|otf)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]?[hash]',
publicPath: Mix.resourceRoot
}
},

Webpack and SASS hot reloading

I'm making a simple html and CSS app. I need to my SASS changes to show up on the website LIVE as im developing.
And once all the SASS and development is complete I will run webpack -b and webpack should convert the SASS to CSS.
I'm sure someone has done this before. Thanks.
It looks like the problem stems from wrong output.publicPath config in webpack.config.js file. In order for live-reload to work, requests going to webpack bundles (e.g. app.css, app.js) should be handled by webpack-dev-server. For the repository you're referring to, this could be done by setting output.publicPath to "/build".
Run webpack-dev-server --inline on the command line after changing config file and then go to http://localhost:8080. Now the page should reload everytime you change and save a file.

Resources