How to access a js file outside the public folder? - laravel

I was trying to register a vue component in the app.js file from a laravel project but I just can't seem to find the right path for the app.js, there are two files with app.js filenames. Here is my file structure:
public
css
js
app.js
resources
js
components
Articles.vue
ExampleComponent.Vue
app.js
bootstrap.js
Here is where I imported my app.js file to my blade file:
<body>
<div id="app">
<articles>
</articles>
</div>
<script type="text/javascript" src="{{ assets('js/app.js') }}"></script>
</body>
The problem is I want to access the second app.js file which is in the resources folder, but the above code keeps giving me the first app.js file which is in the public folder.
I tried giving it the absolute path to the file using this code URL::asset('resources/js/app.js')
This gives me a url like localhost:8000/resources/js/app.js which is the correct file location but somehow this link leads to a 404 page. So is there any other way to access the file?
Thanks

Firstly
localhost:8000/something always refer to something in the public directory. So, you cannot reference your resource/js/app.js in your blade file as js file.
Secondly
Actually you don't need to use the resource/js/app.js file in your blade. If you even do so, the code won't work. Because the codes in resources/js/app.js are un-compiled. You need to compile them and use them in blade files. So, where is the compiled file? It's actually the public/js/app.js file. This file is the compiled version of your resource/js/app.js.
The problem is every time you change a js file inside resource, you need to re-compile the js codes. All those codes will be merged together and will be copied in public/js/app.js.
How to compile?
Before compiling your js code you need to make sure you have installed your frontend dependencies correctly by running the npm install command.
Then if all dependencies are installed you can compile your code using the command
npm run dev for development level compile
npm run production for production level(minified and secured) compile.
If you don't want to run the npm run dev command every time you change a file, then you can run npm run watch command. This command will watch if any js file changed. If changed it will compile automatically.

Related

SCSS - usage of image files with relative path are not working

I have the following 4 files:
/src/images/image.png
/src/scss/main.scss
/src/scss/test/test.scss
/src/js/entry.js
In main.scss I have the following code:
#import './test/test.scss';
In 'test.scss' I have the following code:
.test {
background-image: url(../../images/image.png);
}
And in 'entry.js' I have the following code:
import './../scss/main.scss'
When I try to build this project with webpack, I get an error, that the image.png file can not be resolved. It appears, that when I build this project, test.scss thinks that it's relative folder is /src/scss/. If I change ../../images/image.png to ../images/image.png everything works correctly.
Is there any way to tell webpack or sass-loader to threat every imported file, as a separate file, and always use paths relative to the currently opened file? Or is there any way to specify paths from the root folder of my application (/src in this case)?

Laravel Webpack mix.js Cannot Compile with extenstion .vue

I'm new to webpack in Laravel. I already managed to compile the default scripts into one. However, when I tried to add a new Vue Controller in a separate folder, it seems it will not be included during npm run dev.
Currently I have this set-up
-assets
--js
---app.js
---test.vue
mix.js([
'resources/assets/js/app.js',
'resources/assets/js/test.vue'
], 'public/js/app.js');
This will work. However when I put test.vue inside a folder.
-assets
--js
---app.js
--controllers
--test.vue
mix.js([
'resources/assets/js/app.js',
'resources/assets/js/controllers/test.vue'
], 'public/js/app.js');
However, when I changed it from test.vue to test.js it will compile. Does the file extension matter? when compiling mix.js?
Can someone help me out on this? Thanks!
If i am not mistaken you can't compile single .vue files using webpack without some sort of loader
Using Vue.component() and importing a vue file into a .js file and then compiling the .js will work.
This might be of help: https://github.com/vuejs/vue-loader
Vue Single-File Component (SFC) Spec
A *.vue file is a custom file format that uses HTML-like syntax to
describe a Vue component. Each *.vue file consists of three types of
top-level language blocks: , , and , and
optionally additional custom blocks:
vue-loader will parse the file, extract each language block, pipe them
through other loaders if necessary, and finally assemble them back
into an ES Module whose default export is a Vue.js component options
object.

Laravel Mix js combining all js files

How do I combine all of my js files to a single file with laravel mix? and is it a good practice to directly keep the downloaded external plugins js files and CSS files in public directory? because we have a js folder already in resource folder and it's getting compiled and saved in public directory when we do npm run production, not sure if this is correct or not.
This is how my resource folder looks like :
And my app.js file looks like this :
You can include those libraries by installing them to NPM with npm install jquery for instance. Then in your bootstrap.js file you would have something like:
window.jQuery = window.$ = require('jquery');
That would make it available in the window, and then you still have just one js file to include on the client (your compiled app.js file).
If you do have a file you want separate from your main bundle, and it isn't one you have pulled in through NPM, it's not a bad thing to commit it straight to the public folder. I would keep them in /resources/assets/js/vendor and then use mix.copy to move them into /public/js/vendor. Thats just because I prefer all of my work to be in resources and to have public be all compiled or copied files.
you can add them from npm , or if these are external plugins like my case you can do this
mix.js('resources/assets/js/app.js', 'public/js')
.scripts([
"path/plugin1.js",
"path/plugin2.js",
.......
], 'public/js/all.js').styles([
"path/plugin1.css",
"path/plugin2.css",
.....
],'public/css/all.css');
for more useful information you can visit : https://laravel.com/docs/5.6/mix
You can use npm to install jquery and do it like this same with jeff
window.$ = require('jquery');
OR but if you have your own script want to include then
just put it top on vue instance
require('./jquery.js') //based on your picture it jquery and app.js are in same level
window.vue = require('Vue')
this will automatically read and being compiled by the npm.

Use Bootstrap 4 theme with Laravel 5.5

Only recently jumped on to the Laravel and Bootstrap 4 bandwagons, alas I have got Laravel 5.5 running with Bootstrap 4.0 no problem.
Sooo, I have getbootstrap.com Dashboard theme but I'm unclear on how to compile the themes assets and dependancies, i.e. what do I do with the gulpfile.js and package.json?
Note: I am using Laravel 5.5.34 and Bootstrap 4.0.0. I would rather learn how to compile assets using Laravel Mix and migrate from Gulp to Webpack, than simply copy the dist CSS to the public directory or link to the CDN in blade templates.
I was going through the same dilemma and this answer from Jeffrey Way has helped me.
I have created a css folder in resources/assets folder ( now it looks like resources/assets/css ). I copied all the css files that came with the theme to this folder.
And i updated the webpack.mix.js file in the root of the application
mix.js('resources/assets/js/app.js', 'public/js')
mix.styles([
'resources/assets/css/app.min.css',
'resources/assets/css/primary.css',
'resources/assets/css/style.css'
], 'public/css/app.css');
Now when i run npm run dev all the css is compiled into a single file in pubilc/css/app.css and js in public/js/app.js
Note: My theme also came with a js file which is named app.min.js
So i inluded it in public/js/app.min.js as the main compiled file name is app.js. If your theme has a file app.js rename it to theme.js and place in the public/js folder.
You can call these the normal way you do after compiling them with {{asset('css/app.css') }}

yeoman, SCSS, bower server, styles/main.css not found

I wanted today to try scss support of yeoman.
I followed the procedure :
$ yo angular
include Twitter Bootstrap? Yes
use the SCSS version of Twitter Bootstrap with the Compass CSS Authoring Framework? Yes
$ grunt server
And then the default view load but without style formatting. In the console I can see that it cannot find /styles/main.css file.
I have seen that compass put the file in .tmp/styles/main.css, so I tried to change it in index.html. But the same. Moreover there is no .tmp directory in my project folder.
So I ran "grunt build" and loaded the index.html in dist directory in a MAMP server. Same, no css formatting, moreover no error in the console
You shouldn't be putting the '.tmp' path anywhere in your index.html, it should be left pointing to 'styles/main.css' like so:
<!-- build:css(.tmp) styles/main.css -->
<link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->
You should also have your actual style file at 'app/styles/main.scss', which Yeoman should have created for you. When you run 'grunt server' it uses compass to compile all the scss files into a single css file which it temporarily puts under '.tmp', but as long as your scss files are in place you should not need to worry about this. My guess would be that you've somehow deleted or renamed the main.scss file; you should not be renaming this to 'main.css' for example.
I had the same issue, your main.scss file has issues with bootstrap css
I deleted the line ( 2. line )
#import 'bootstrap-sass-official/vendor/assets/stylesheets/bootstrap';
and have no more issues. it compiles to main.css

Resources