How do I include a plain old JS file without any exports or modern shebangs inside my resulting app.js? The file is just a collection of JS functions.
Just started a Phoenix 1.5.1 project, tried putting the file in assets/static/ seeing as Webpack has a rule for it (but I might misunderstand it) and then tried calling one of the functions from the dev console, but they are not found (and searching inside the generated app.js doesn’t show them either). Also tried putting the vile in vendor/js/ and still no go.
What’s a quick and efficient way to do this?
Disclaimer: I really am not looking to become a webpack expert. The project will not mandate it, I am just stumbling at the first few basic steps (or I thought they were basic).
It gets loaded in the browser just fine but looking at the generated JS I am noticing the functions are wrapped by a JS closure so I suppose I have to try the old-fashioned way indeed…
So, removed the Webpack entry and kept the <script> tag. The file gets loaded just fine in this setup as well but still no functions accessible in the dev console.
It depends. Discarding module bundlers, imports, etc. and going back to “vanilla” JS, if you want to call a function globally it must be defined on the global object (window in the browser).
Globals can be defined in several ways:
function myGlobalFunction () { /* ... */ }
var myGlobalFunction = function () { /* ... */ }
window.myGlobalFunction = function () { /* ... */ }
// Not recommended, but omitting `var` also defines a global, although is not allowed in strict mode:
myGlobalFunction = function () { /* ... */ }
The first two ways above have local scope, so if they appear inside another function they won’t define a global.
Alternatively, it’s possible that your file defines a global object inside which the functions are defined:
This still doesn’t give access to the functions in the dev console but at least it works on the pages where it’s needed and they are indeed included in the final app.js. So I suppose my benchmark was all wrong.
Good that you managed to solve it JavaScript bundlers, transpilers, tooling, etc. are indeed really complicated and add quite a bit of cognitive overhead.
Just to add to what I said before, note that defining variables with let or const will not define them as globals on the window object (as opposed to var when used at the top level). In general, if you want a global that you can use from the console, defining it explicitly as window.myGlobal = ... is usually the best way.
This is what’s supposed to happen. Binding variables into the global scope by default is considered bad practice because it often lead to things overriding each other. This is why modern bundlers properly scope code to stop it from tripping over other code. If you want things to still be bound to the global window object do it explicitly using e.g. window.myFunc = myFunc.
In the current Phoenix deployment environment, after examining webpackconfig.js in the assets folder, I learned to drop the third party js files directly into the assets/vendor folder, in order to be compiled into the app.js file by webpack