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 Switch mode

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.

Last Post!

peerreynders

peerreynders

Even “just” using jQuery can be a polarizing topic.

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement