shotleybuilder

shotleybuilder

Js libraries - global variables - brunch-config

From the Phoenix 1.3 User Guide: “If we already have code that assumes jQuery is available as a global variable, we’ll either need to migrate our code (which is a must-do in the long run), or leave jQuery as a non-wrapped codebase (which is acceptable as a transition hack).” https://hexdocs.pm/phoenix/static_assets.html

What does that mean? I’ve installed material-design-lite and mdl-ext. The way I have these working is to add them as globals:

npm: {
enabled: true,
styles: {
  'material-design-lite': ['dist/material.min.css'],
  'mdl-ext': ['lib/mdl-ext.min.css']
},
globals: {
  material: 'material-design-lite',
  'mdl-ext': 'mdl-ext'
}
}

And then to import them inside app.js

import "material-design-lite/material";
import "mdl-ext";

They’re just providing some classes in the html. Simple stuff. Just confused by the docs.

Most Liked

peerreynders

peerreynders

See Globals; because, reasons.

In the case of jQuery the early practice was to simply attach the jquery object to window:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

That is what the sample globals configuration will do automatically for you inside the JS bundle that brunch creates. However current practice is to keep everything in a JavaScript module. Then the globals are no longer necessary because jQuery is accessed explicitly through the module mechanism:

const $ = require('jquery'); // CommonJS module - e.g. used inside node.js
import {$,jQuery} from 'jquery'; // ES2015 module syntax

See also ECMAScript 6 modules: the final syntax

However browsers are only now starting to support ES2015 modules on an experimental level. To get the benefit of modules in the browser it has been necessary to use “bundlers” like browserify - and brunch is a bundler.

Now import statements are also used with CSS files because CSS modules try to move beyond the CSS Block Element Modifier naming convention as a means for avoiding naming collisions. These styles are applied inside JavaScript code; but sometimes such an import is simply used to communicate to the bundler that the CSS is being used and needs to be bundled (e.g. Webpack).

It is my impression that mdl never supported CSS modules - that only started to be supported in Material Components for the web - which is why there is a warning that mdl is in limited support.

Brunch can bundle CSS files - but that is simply a matter of configuration to create a bundled app.css file. So I’m not even sure if you are even getting any benefit from the mdl globals configuration or the import statements in connection with your styles. However brunch does need to know where to find the CSS to put into the CSS bundle.

The Brunch fundamentals are covered in The Brunch.io Guide.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement