0

I'm using Laravel and in my bootstrap.js file I have

window.ProgressBar = require('progressbar.js');

and then I require the bootstrap.js file into app.js

require('./bootstrap');

And I use webpack.mix.js to compile it

mix.js('resources/assets/js/app.js', 'public/js')

If I attempt to access progressbar.js inside of app.js it works perfectly.

However, If I attempt to use it outside of app.js in script tag that comes after app.js on my pages, ProgressBar is undefined.

How can I access ProgressBar outside of my app.js but keep it imported via app.js ?

Example on index.blade.php

<script src="app.js" defer></script>
<script>
  const progress = new ProgressBar; //undefined...
</script>
6
  • Are you sure you get undefined? Doesn't seem reproducible to me. You really shouldn't be relying on const implementations in the browser either. Commented Sep 18, 2018 at 1:13
  • @Devon Have you tried to reproduce it? Secondly, why shouldn't I use const ? Are you saying I should use let or var instead? Commented Sep 18, 2018 at 1:21
  • Yes, it works fine. const and let are both es6, es6 features may not be implemented in all browsers. Commented Sep 18, 2018 at 1:23
  • @Devon Finally figured it out. I had defer on the <script> tag and for some reason that stopped it from working. As for const and let - caniuse.com/#search=const caniuse.com/#search=let Based off of that, I'm happy to use it Commented Sep 18, 2018 at 1:31
  • Missed that, yes, that would cause it because you're asynchronously loading your script so the new ProgressBar line would execute before your app.js has loaded. I've found many people still on Win 7 using IE, but if you're happy not supporting them, that's up to you. Commented Sep 18, 2018 at 1:37

2 Answers 2

1

I had defer on the script tag and for some reason that stopped it from working.

Solution:

<script src="app.js" defer></script>

change to

<script src="app.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

0

I think, that Webpack hides ProgressBar from you, even if you have ProgressBar assigned to the window. Because you know, Webpack thinks that the bundle has all the code of your app. Solution? Not to bundle it! Instead use minified version of ProgressBar and just load it like in good old times. It is perfectly OK, few nanoseconds better or worse, not noticeable.

2 Comments

But what if I want to use it inside of app.js as well? That would mean I've now loaded 2 instances of progressbar.js so I don't think this is a good solution
I've thinked about it, though. It appears it is the case, unfortunately. Well, then I'd dig webpack docs about sharing data with outside of the bundle (with window object). Too bad, I am no Webpack expert (I hate it, frankly). Good news: it should be a very simple task. You only need to find a way to assign somehow the property to window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.