0

Normally I use a javascript file to import different JS files. Now having a difficulty to do it so. The project I am working on went wild and have a 5Mb JS file and I only want a piece of it to load on one page. In a Javascript file I could import the Module like this,

import {Module} from "./components/Module";

and use as Module.init();

But what I am trying to do is load this Module in a blade file. The js file lives under resources/assets/js/components/Module.js

Because this folder is not public I am not sure if I use something like

<script src="../../../resources/assets/js/components/Module.js"></script> on the page. Well, tried it but did not work as well. All I want to be able to init the JS like Module.init(); inside my blade file.

I would really appreciate and directions as I am a bit confused right now:(

Jerry Li
  • 97
  • 8

1 Answers1

1

You cant import your files from resources directory, the only visible part of your application to users should be public folder so you should put your file into the public folder. for example
public/js/modjule.js and import it like this

<script src="{{ asset('js/module.js') }}"

if your site is on a https protocol you can use secure_asset()

<script src="{{ secure_asset('js/module.js') }}"

AH.Pooladvand
  • 1,944
  • 2
  • 12
  • 26
  • Thanks for the reply @AH.Pooladvand, the methods of Module is inside the `index.js` which is under public. This `index.js` is also included on the page but I cannot do anything like `Module.init()` which runs some scripts and AJAX codes. Looks like I'll need to find a different way :) thanks for making it clear. – Jerry Li Jan 19 '19 at 15:08
  • @JerryLi I'm not really a js expert but I think to be able to do a thing like `Module.init()` you should make your `Module` as a window variable like jquery's `$` variable for more info read this link https://stackoverflow.com/questions/7082886/window-vs-var-to-declare-variable – AH.Pooladvand Jan 19 '19 at 15:17